Ruggiero Spearman Ruggiero Spearman - 1 year ago 115
Python Question

Get legend as a separate picture in Matplotlib

I'm developing a Web application and want to display a figure and its legend in different locations on the page. Which means I need to save the legend as a separate png file. Is this possible in Matplotlib in a more or less straightforward way?

Answer Source

This could work:

import pylab
fig = pylab.figure()
figlegend = pylab.figure(figsize=(3,2))
ax = fig.add_subplot(111)
lines = ax.plot(range(10), pylab.randn(10), range(10), pylab.randn(10))
figlegend.legend(lines, ('one', 'two'), 'center')

alt text

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download