7

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.

2 Answers 2

13
+50

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()

enter image description here

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Very nice. I actually tried the exact same thing, but didn't see anything because I wasn't redrawing properly.
Note that this only works for the caps. If the bar itelf is being cut in half (as you'll see when you turn the axes off), you need to do the same for all b in e[2] as well.
0

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
(source: stevetjoa.com)

1 Comment

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.