On Sun, Mar 18, 2012 at 9:14 AM, klo uo <klonuo@gmail.com> wrote:
On Sun, Mar 18, 2012 at 1:50 PM, Angus McMorland <amcmorl@gmail.com> wrote:

For inline ipython, you want to switch to the object-oriented use of
pylab. Something like this should work with xlim.

a = [0.1, 0.2, 0.1]
fig = plt.figure()
ax = fig.add_subplot(111)
ax.errorbar(arange(3), a, yerr=a-sum(a)/len(a), fmt='ro')
ax.set_xlim(-.5,2.5)
ax.show()

I'm not aware of automatic settings for padding, but with this
set_xlim, it's easy enough to roll your own using the data limits.


OK, thanks

It's not very elegant (assuming pylab freedom) but I take it as only way to correct clipping example (or differently put - to use custom range for axis)


You can roll out a utility function that can automatically adjust the limits with some specified padding (i.e. the function doesn't calculate the marker size, but you can just give it sufficient padding).

Here's an example where you specify a padding passed on the axes size (0.05 is 5% of axes height/width):

#~~~~
import numpy as np
import matplotlib.pyplot as plt

def pad_limits(pad_frac=0.05, ax=None):
    ax = ax if ax is not None else plt.gca()
    ax.set_xlim(_calc_limits(ax.xaxis, pad_frac))
    ax.set_ylim(_calc_limits(ax.yaxis, pad_frac))

def _calc_limits(axis, frac):
    limits = axis.get_data_interval()
    mag = np.diff(limits)[0]
    pad = np.array([-mag*frac, mag*frac])
    return limits + pad

a = np.array([0.1, 0.2, 0.1])

plt.errorbar(np.arange(3), a, yerr=a-sum(a)/len(a), fmt='ro')
pad_limits()

plt.show()
#~~~~

I've put this is my personal mpl toolkit with the added ability of handling log scales:
https://github.com/tonysyu/mpltools/blob/master/mpltools/layout.py#L80

Best,
-Tony