简体   繁体   中英

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?

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'])

组合的线和错误栏图例

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM