15

I am trying over-plot some empirical data with error bars on top of my modelled data. The error bars seem to be rendering first and are consequently getting over written (see below)

I have tried using zorder but I still get the same result. The code I am using is

    for i in range(1,len(pf)):
            pf[i,:] = av_pf_scale * pf[i,:]
            pylab.semilogy(pf[0,0:180],pf[i,0:180],color='0.75')

    pylab.semilogy(av_pf[0:180],color='r')
    pylab.semilogy(av_mie[0:180],color='g', linestyle='-')

    pylab.draw()
    f = pylab.errorbar(ang,data[j],
                            yerr = delta_data[j],
                            fmt = 'o',
                            markersize = 3,
                            color = 'b',
                            zorder = 300,
                            antialiased = True)

I would appreciate if anyone can tell me how to make the errorbars render on top.

Mulitplot

Caustic
  • 940
  • 3
  • 12
  • 29
  • Could you provide a clearer example? I'm looking hard at your plot and I can see blue error line bars above the other data (they are above the grey mess and the red and green lines). The values seem to be relatively small and so don't have visible verticals, but that's in the data not matplotlib. – danodonovan Dec 22 '12 at 15:15
  • 1
    Have you tried increasing the zorder beyond 300? Also it's odd that the ends of the errorbars are being rendered above the gray lines, but not the lines. (also watch for negative lower error bounds on your first and last data points.) – ebarr Dec 22 '12 at 16:11
  • alternatively, try setting the zorder of the gray lines to be `-100`. – tacaswell Dec 22 '12 at 22:23
  • +1 Good question! In the future, it is better to give examples of your problem that other people can run without needing access to your data (see my examples in my answer) http://sscce.org/ . – tacaswell Dec 22 '12 at 23:05
  • Thanks, that is good advice. In the future I will do. – Caustic Dec 24 '12 at 03:26

2 Answers2

16

This looks like it is a bug in matplotlib where the zorder argument of the errorbar is not correctly passed to the vertical lines part of error bars.

replicates your problem :

import matplotlib.pyplot as plt
fig = plt.figure()
ax = plt.gca()
[ax.plot(rand(50),color='0.75') for j in range(122)];
ax.errorbar(range(50),rand(50),yerr=.3*rand(50))
plt.draw()

error bar fail Hacky work around:

fig = plt.figure()
ax = plt.gca()
[ax.plot(rand(50),color='0.75',zorder=-32) for j in range(122)];
ax.errorbar(range(50),rand(50),yerr=.3*rand(50))
plt.draw()

error bar hack

report as an issue to matploblib https://github.com/matplotlib/matplotlib/issues/1622 (now patched and closed)

tacaswell
  • 84,579
  • 22
  • 210
  • 199
  • @tcaswell Thank you very much. You've helped me yet again. I had tried setting zorder to be positive, but never considered forcing the underlying plots to be negative. Brilliant. – Blink May 16 '13 at 02:14
  • @William Glad it helped. You should really update your mpl though as I think that patch is 1.2.1 – tacaswell May 16 '13 at 02:51
  • @tcaswell I just encountered what seems to be the same problem in 1.4.2, reproducing your buggy 1.2.0 (?) plot from above. Comment added to the long-closed github ticket... – andybuckley Sep 22 '15 at 19:35
  • 4
    This bug is also back in matplotlib 2.0.0. I have created / reopened the ticket. There was also a better "hack" mentioned with `zorder=3` to `plt.errorbar`. I will edit the answer accordingly. – Frank Breitling Feb 16 '17 at 22:39
1

This is a known bug in matplotlib. Link to github issue

Hacky solution: Add the argument zorder=3 when you call plt.errorbar, like plt.errorbar(..., zorder=3)

Alex Lamson
  • 479
  • 5
  • 14