Matplotlib incorrect display of data on the y-axis. Bars are displayed from coordinate 0.5 to 1.0, but this is not what I would like to see. Coordinates 1.0 are not in the data (height
and bottom
)
Code for example:
fig, ax = plt.subplots()
x = [0, 1, 2, 3, 4]
height = [0.5047, 0.4999, 0.4985, 0.4999, 0.4987]
bottom = [0.5002, 0.4969, 0.4956, 0.4969, 0.4967]
ax.set_ylim(0, 2)
ax.bar(x=x, height=height, width=0.2, bottom=bottom)
plt.show()
CodePudding user response:
Matplotlib Pyplot Bars use the
- bottom parameter to define the y-coordinate of the bottom of the bar
- height parameter to define the hight of the bar
Consequently, your bars start at bottom
and end at bottom height
.
If you calculate the sum of each of coordinates, you will see that they all end roughly at about 1.0:
print([sum(x) for x in zip(height, bottom)])
results in
[1.0049000000000001, 0.9968, 0.9941, 0.9968]
If you aim at drawing bars, whose y-coordinate start at bottom and end at height, then subtract them first
fig, ax = plt.subplots()
x = [0, 1, 2, 3, 4]
height = [0.5047, 0.4999, 0.4985, 0.4999, 0.4987]
bottom = [0.5002, 0.4969, 0.4956, 0.4969, 0.4967]
tmpHeight = tuple(map(lambda i, j: i - j, height, bottom))
ax.bar(x=x, height=tmpHeight , width=0.2, bottom=bottom)
plt.show()