I'm plotting some data with matplotlib. I want the plot to focus on a specific range of x-values, so I'm using set_xlim().
Roughly, my code looks like this:
fig=plt.figure() ax=fig.add_subplot(111) for ydata in ydatalist: ax.plot(x_data,y_data[0],label=ydata[1]) ax.set_xlim(left=0.0,right=1000) plt.savefig(filename)
When I look at the plot, the x range ends up being from 0 to 12000. This occurs whether set_xlim() occurs before or after plot(). Why is set_xlim() not working in this situation?
To set the limits for X-axis only, We could use xlim() and set_xlim() methods. Similarly to set the limits for Y-axis, we could use ylim() and set_ylim() methods. We can also use axis() method which can control the range of both axes.
To plot the graph, use the plot() function. To set the limit of the x-axis, use the xlim() function. To set the limit of the y-axis, use the ylim() function. plt.
MatPlotLib with Python To change the range of X and Y axes, we can use xlim() and ylim() methods.
Out of curiosity, what about switching in the old xmin
and xmax
?
fig=plt.figure() ax=fig.add_subplot(111) ax.plot(x_data,y_data) ax.set_xlim(xmin=0.0, xmax=1000) plt.savefig(filename)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With