Doug
Doug

Reputation: 417

Matplotlib offset errorbar bug workaround?

The bug is documented here:

Matplotlib errorbar not centered on marker

and here: https://github.com/matplotlib/matplotlib/issues/3400

Basically, the markers are plotted off by 1 pixel all the time.. You can even see this on Matplotlib's own tutorial page if you look closely at the second plot: http://matplotlib.org/1.2.1/examples/pylab_examples/errorbar_demo.html

This is very frustrating as I cannot produce publication-quality plots from matplotlib, and I'm very surprised this has not been fixed.

In any case, I have too much time and code invested into matplotlib to switch to a different package. So my question is how would you go about making a workaround? I suppose one solution is to plot the markers 1 pixel to the left/right from the errorbars. I don't know how to do this. I figured out how to get the display coordinates of my plot points, but how can I make an interactive plot that preserves the 1-pixel offset? I can plot them with 1-pixel offsets, but then you can't zoom or manipulate the plot.

Upvotes: 5

Views: 1304

Answers (1)

jxz12
jxz12

Reputation: 133

It seems like the Matplotlib team have fixed the issue when calling savefig() using .svg or .pdf, but for .png I've found that you can circumvent this issue by using an odd number for the error line thickness. Using the first example on the Matplotlib tutorial, if we use

plt.errorbar(x, y, yerr=0.4, marker='X', markersize=15)

then the bars are offset like this:

enter image description here

However if we use a line width of 3

plt.errorbar(x, y, yerr=0.4, marker='X', markersize=15, elinewidth=3)

then the bars are centred like this:

enter image description here

This isn't a perfect solution, but it does the job if you don't mind having slightly thicker lines.

Upvotes: 4

Related Questions