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?
Advertisement
Answer
This could work:
JavaScript
x
10
10
1
import pylab
2
fig = pylab.figure()
3
figlegend = pylab.figure(figsize=(3,2))
4
ax = fig.add_subplot(111)
5
lines = ax.plot(range(10), pylab.randn(10), range(10), pylab.randn(10))
6
figlegend.legend(lines, ('one', 'two'), 'center')
7
fig.show()
8
figlegend.show()
9
figlegend.savefig('legend.png')
10