python 2.7 - How to get all legends from a plot? -
i have plot 2 or more legends. how can "get" legends change (for example) color , linestyle in legend?
handles, labels = ax.get_legend_handles_labels()
gives me "first" legend, added plot plt.legend()
. want others legends too, added plt.gca().add_artist(leg2)
how can that?
you can children axes , filter on legend type with:
legends = [c c in ax.get_children() if isinstance(c, mpl.legend.legend)]
but work @ all? if add more legends mention, see multiple legend
children, pointing same object.
edit:
the axes keeps last legend added, if add previous .add_artist()
, see multiple different legends:
for example:
fig, ax = plt.subplots() l1, = ax.plot(x,y, 'k', label='l1') leg1 = plt.legend([l1],['l1']) l2, = ax.plot(x,y, 'k', label='l2') leg2 = plt.legend([l2],['l2']) ax.add_artist(leg1) print(ax.get_children())
returns these objects:
[<matplotlib.axis.xaxis @ 0xd0e6eb8>, <matplotlib.axis.yaxis @ 0xd0ff7b8>, <matplotlib.lines.line2d @ 0xd0f73c8>, <matplotlib.lines.line2d @ 0xd5c1a58>, <matplotlib.legend.legend @ 0xd5c1860>, <matplotlib.legend.legend @ 0xd5c4b70>, <matplotlib.text.text @ 0xd5b1dd8>, <matplotlib.text.text @ 0xd5b1e10>, <matplotlib.text.text @ 0xd5b1e48>, <matplotlib.patches.rectangle @ 0xd5b1e80>, <matplotlib.spines.spine @ 0xd0e6da0>, <matplotlib.spines.spine @ 0xd0e6ba8>, <matplotlib.spines.spine @ 0xd0e6208>, <matplotlib.spines.spine @ 0xd0f10f0>]
it remains seen whether want do!? can store lines (or other types) separately axes.
Comments
Post a Comment