Matplotlib Errorbars Weirdness

Some time ago, I was having trouble with using error bars with a legend in matplotlib. I did some hack to fix it that time.
Today, I came across the same issue again at the scikit-learn sprint.
Obviously this time we wanted to do it right^TM.

The problem is that when doing a plot with error bars, the colors in the legend don't correspond to the colors of the lines.

The problem seems to be solved in never versions, though.

After fiddling a bit with it I found the problem:
The errorbars command actually returns a list of artists, corresponding to the lines AND the individual error bars. That seems to really confuse the legend.

You can solve the problem by doing
errorbar  = pl.errorbars(x, y, err)
line = errorbar[0]
legend(line, 'the line')
taking the zeroth element of the tuple extracts just the line from the collection of
artists and everything works out :)


  1. Thanks, was driving me mad! My legend wasn't displaying the correct markers and lines after the initial one, this sorted it.

  2. It was driving me crazy, too. Glad I could help you with it.


Post a Comment

Popular posts from this blog

Machine Learning Cheat Sheet (for scikit-learn)

A Wordcloud in Python

MNIST for ever....

Python things you never need: Empty lambda functions