Yotam
Yotam

Reputation: 10685

Combining a line from plot to marks from errorbar in matplotlib legend

When plotting data on matplotlib, I use errorbar to place the scatter marks and plot to draw the linear fit result. I store the errorbar output in an array I feed to fig.ax.legend() to tell it what to place in the legend (I follow matplotlib conventions). Is there a way to tell it to add the fit line in the same legend line?

Upvotes: 2

Views: 1024

Answers (1)

esmit
esmit

Reputation: 1797

In matplotlib at least v1.2.1, you can add a tuple of lines that should be overlayed for the legend entry:

from pylab import *
fig = figure(1)
fig.clear()
x = linspace(0,2*pi,100)
y = sin(x)
yerr = y*.1
e = errorbar(x,y,yerr=yerr,label='data')
l, = plot(x,y,lw=3)
legend([(e,l)],['data & fit'])

combined line and errorbar legend

Upvotes: 2

Related Questions