views:

304

answers:

2

I am using matplotlib in Python to plot a line with errorbars as follows:

plt.errorbar(xvalues, up_densities, yerr=ctl_sds, fmt='-^', lw=1.2, markersize=markersize,
         markeredgecolor=up_color, color=up_color, label="My label", clip_on=False)
plt.xticks(xvalues)

I set the ticks on the x-axis using "xticks". However, the error bars of the last point in xvalues (i.e. xvalues[-1]) are clipped on the right -- meaning only half an error bar appears. This is true even with the clip_on=False option. How can I fix this, so that the error bars appear in full, even though their right side is technically outside xvalues[-1]?

thanks.

A: 

Is this what you mean? Do you want to redefine the horizontal limits of your plot?

plt.errorbar(range(5), [3,2,4,5,1], yerr=[0.1,0.2,0.3,0.4,0.5])
ax = plt.gca()
ax.set_xlim([-0.5,4.5])

Matplotlib errorbar

Steve
No, I want the last tick mark to be at 4 (in your example) and the horizontal x-axis to end at that, with nothing past it. The horizontal lines at the ends of the error bars will extend slightly passed the end of the x-axis but clip_on=False should make those visible nonetheless... though they don't. Any ideas how I can do this?
+5  A: 

In matplotlib, most of the detailed control needs to be done through the Artists. I think this should do what you want:

import matplotlib.pyplot as plt
from random import uniform as r

x = range(10)
e = plt.errorbar(x, [r(2,10) for i in x], [r(.1,1) for i in x], capsize=8, color='r')

for b in e[1]:
    b.set_clip_on(False)

plt.show()

alt text

The problem you were having is that the clip_on keyword was being used to control the markers and not the error bars. To control the errorbars, plt.errorbar returns a tuple, where the second item is a list of errorbars. So here I go through the list and turn the clipping off for each errorbar.

tom10
Very nice. I actually tried the exact same thing, but didn't see anything because I wasn't redrawing properly.
Steve