Saturday 15 January 2011

matplotlib - Combined legend entry for plot and fill_between -



matplotlib - Combined legend entry for plot and fill_between -

this similar matlab: combine legends of shaded error , solid line mean, except matplotlib. illustration code:

import numpy np import matplotlib.pyplot plt x = np.array([0,1]) y = x + 1 f,a = plt.subplots() a.fill_between(x,y+0.5,y-0.5,alpha=0.5,color='b') a.plot(x,y,color='b',label='stuff',linewidth=3) a.legend() plt.show()

the above code produces legend looks this:

how can create legend entry combines shading fill_between , line plot, looks (mockup made in gimp):

mpl supports tuple inputs legend can create composite legend entries (see lastly figure on this page). however, of polycollections--which fill_between creates/returns--are not supported legend, supplying polycollection entry in tuple legend won't work (a prepare anticipated mpl 1.5.x).

until prepare arrives recommend using proxy artist in conjunction 'tuple' legend entry functionality. utilize mpl.patches.patch interface (as demonstrated on proxy artist page) or utilize fill. e.g.:

import numpy np import matplotlib.pyplot plt x = np.array([0, 1]) y = x + 1 f, = plt.subplots() a.fill_between(x, y + 0.5, y - 0.5, alpha=0.5, color='b') p1 = a.plot(x, y, color='b', linewidth=3) p2 = a.fill(np.nan, np.nan, 'b', alpha=0.5) a.legend([(p2[0], p1[0]), ], ['stuff']) plt.show()

matplotlib legend

No comments:

Post a Comment