Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is set_xlim() not setting the x-limits in my figure?

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?

like image 380
Dan Avatar asked Jul 18 '13 21:07

Dan


People also ask

How do you set limits on the X-axis?

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.

How do you limit the X-axis in Pyplot?

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.

How do I change the X-axis scale in Python?

MatPlotLib with Python To change the range of X and Y axes, we can use xlim() and ylim() methods.


1 Answers

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) 
like image 66
esmit Avatar answered Sep 29 '22 22:09

esmit