Issue
I am using matplotlib to plot data. Here's a code that does something similar:
import matplotlib.pyplot as plt
f, ax = plt.subplots(1)
xdata = [1, 4, 8]
ydata = [10, 20, 30]
ax.plot(xdata, ydata)
plt.show(f)
This shows a line in a graph with the y axis that goes from 10 to 30. While I am satisfied with the x range, I would like to change the y range to start from 0 and adjust on the ymax to show everything.
My current solution is to do:
ax.set_ylim(0, max(ydata))
However I am wondering if there is a way to just say: autoscale but starts from 0.
Solution
The range must be set after the plot.
import matplotlib.pyplot as plt
f, ax = plt.subplots(1)
xdata = [1, 4, 8]
ydata = [10, 20, 30]
ax.plot(xdata, ydata)
ax.set_ylim(ymin=0)
plt.show(f)
If ymin
is changed before plotting, this will result in a range of [0, 1].
Edit: the ymin
argument has been replaced by bottom
:
ax.set_ylim(bottom=0)
Documentation: https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.set_ylim.html
You can do the same on the x axis with left and right:
ax.set_xlim(left=0)
Documentation: https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.set_xlim.html
Answered By - Maxime Chéramy
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.