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

matplotlib xlim

subplot set x limit

module 'matplotlib.pyplot' has no attribute 'set_xlim'

matplotlib set axis range

plt x and y range

matplotlib set x axis values

set y range

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?

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)

**matplotlib.axes.Axes.set_xlim — Matplotlib 3.1.2 documentation,** Axes. set_xlim (self, left=None, right=None, emit=True, auto=False, *, xmin=None , The left xlim in data coordinates. Anatomy of a figure ../. The x-axis limits might be set like the following so 5000 years ago is on the left of the plot and the present is on the right. >>> set_xlim ( 5000 , 0 ) Examples using matplotlib.axes.Axes.set_xlim ¶

**The text of this answer was taken from an answer that was deleted almost immediately after it was posted.**

`set_xlim()`

limits the data that is displayed on the plot.

In order to change the bounds of the axis, use `set_xbound()`

.

fig=plt.figure() ax=fig.add_subplot(111) ax.plot(x_data,y_data) ax.set_xbound(lower=0.0, upper=1000) plt.savefig(filename)

**matplotlib.pyplot.xlim — Matplotlib 3.1.2 documentation,** xlim(right=3) # adjust the right leaving left unchanged xlim(left=1) # adjust Calling this function with no arguments (e.g. xlim() ) is the pyplot� If you do not specify args, you can pass left or right as kwargs, i.e.: xlim ( right = 3 ) # adjust the right leaving left unchanged xlim ( left = 1 ) # adjust the left leaving right unchanged Setting limits turns autoscaling off for the x-axis.

In my case the following solutions alone did not work:

ax.set_xlim([0, 5.00]) ax.set_xbound(lower=0.0, upper=5.00)

However, setting the aspect using `set_aspect`

worked, i.e:

ax.set_aspect('auto') ax.set_xlim([0, 5.00]) ax.set_xbound(lower=0.0, upper=5.00)

**Set or query x-axis limits - MATLAB xlim,** This MATLAB function sets the x-axis limits for the current axes or chart. If you do not specify this argument, then xlim sets the limits on the graphics object� Set the x -axis limits mode to manual so that the limits do not change. Use hold on to add a second plot to the axes. xlim manual hold on plot (2*x,2*y) hold off. The x -axis limits do not update to incorporate the new plot. Switch back to automatically updated limits by resetting the mode to automatic. xlim auto.

**xlim - matplotlib - Python documentation,** xlim(xmin,xmax) - Get or set the x limits of the current axes. xmin, xmax = xlim() # return the current If you do not specify args, you can pass the xmin and xmax as kwargs, e.g.: Setting limits turns autoscaling off for the x-axis. import matplotlib.pyplot as plt w = 4 h = 3 d = 70 plt.figure(figsize=(w, h), dpi=d) x = [1, 2, 4, 3, 0]. We then set the limit of the x coordinates from 0 to 5 using the set_xlim() function. Again, this function takes in a list composed of 2 values. The first value is the lower limit and the second value is the upper limit. Lastly, we show the figure with the show() function. This works if you're using a python IDE other than jupyter notebooks.

**set_ylim not working with plt.axis('equal') � Issue #8093 � matplotlib ,** for angle in range(0,180+step,step): . fig = plt.figure('Angle {0}'.format(angle)) plt. plot(x,y,'-') # plt.gca().set_adjustable('box') # this didn't help� You can set the ticks manually like this: x=c(1:15) y=c(-4:10) plot(x,y, xaxt="n", xlim = c(1,15)) axis(1, at = 1:15) axt="n" omits drawing a x-axis in plot, first.axis manually draws it afterwards - you got more control this way.

**[PDF] R Graphics Lectures,** Setting the Axis Scales. Next we set the scales on draws a box around the figure region (the region containing the plot and its plot.new(). > plot.window( xlim = c(0, 10), ylim = c(-2, 4), xaxs = "i"). > box(). > axis(1 There is no simple R function for drawing a circle. Circles must be title(main = "My Very Own Scatterplot")� i need to specify one "xlim" for my all multiple subplots. I read already answers here but i got some problems about my labels. It disappear. I think; because of "set(gca)" on my codes. I couldn't figure out, how to solve this. If you any idea i really appreciated that. Here my codes of a subplot. Thank you so much!

##### Comments

- You could try
`plt.xlim(…)`

… I remember scratching my head too, few months ago ;-) - I made a simple test example using random integers from 0 to 2000 and
`ax.set_xlim`

properly limits the x-axis from 0 to 1000 for me. - Two people left good answers that got deleted.
- @Dan - Do you have a particular aspect ratio set for the plot? (This happens automatically if you've plotted an image, by the way.) If so, matplotlib will rescale whatever you set to maintain that aspect ratio. Does calling
`ax.aspect('auto')`

before calling`set_xlim`

help? Also, if you want to have matplotlib resize the "outside" of the axes instead of the data limits to maintain a set data aspect ratio, use`ax.set_adjustable('box')`

. - It's odd that
`set_xbound`

works where`set_xlim`

wouldn't. I can't think of a situation where that would normally occur... Out of curiosity, can you post an example that reproduces it? Glad you got things to work, at any rate. - This works perfectly. Since I'm using an old version of matplotlib, this is almost certainly the problem.
- This helped me. I didn't need
`set_xbound`

, though.