32

From the help:

set_xlim: Set the data limits for the xaxis.

set_xbound: Set the lower and upper numerical bounds of the x-axis.

That is not very clear, so let’s say that I plot something:

import matplotlib.pylab as plt
fig, ax = plt.subplots(1, 1)
ax.plot(xrange(10), xrange(10))

Now, either I do:

ax.set_xlim(2, 7)

or:

ax.set_xbound(2, 7)

I do not see the difference. I can dragg the plot, all the line is ploted between 0 and 9.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
PhML
  • 1,190
  • 1
  • 11
  • 20
  • 1
    [Axes.set_xbound](http://matplotlib.sourceforge.net/api/axes_api.html#matplotlib.axes.Axes.set_xbound) uses [Axes.set_xlim](http://matplotlib.sourceforge.net/api/axes_api.html#matplotlib.axes.Axes.set_xlim) Take a look at the [code](https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/axes.py#L2355) – adchilds Jul 12 '12 at 20:38

2 Answers2

27

The bound can changes automatically, if you later plot something that is not within the bound. In contrast, limits are fixed and do not change automatically.

import pylab as p

t = p.arange(0.0, 2.0, 0.01)
s = p.sin(2*p.pi*t)

ax=p.subplot(111)
ax.plot(t, s, color='r',linewidth=1.0)
ax.set_ylim(-1,1)
ax.plot(t, s+1, color='g',linewidth=1.0, label="Graph2")
p.show()


ax=p.subplot(111)
ax.plot(t, s, color='r',linewidth=1.0)
ax.set_ybound(-1,1)
ax.plot(t, s+1, color='g',linewidth=1.0, label="Graph2")
p.show()

enter image description here enter image description here

imsc
  • 7,492
  • 7
  • 47
  • 69
1

After examining the source, we can see that set_xbound(lower, upper) makes a call to set_xlim with arguments passed like so, self.set_xlim(sorted((lower, upper), reverse=bool(self.xaxis_inverted())), auto=None)

So set_xbound is very similar to calling set_xlim with auto=None which will not change auto-scaling settings, the default when you call set_xlim will set auto=False which disables auto-scaling. This is why bounds may be altered later on by auto-scaling, as the default is to auto-scale, unless explicitly turned off (or implicitly by a method like set_xlim).

pseudovella
  • 209
  • 1
  • 8