|
From: Peter V. W. <p_...@sb...> - 2014-01-31 11:45:07
|
I would like to ask if there is a way to print only the legend box of a figure.
The motiviation for wanting to do this is a work around to the problem of having the legend box obscuring data without resorting to "outside" placement of the legend. The idea here is that matplotlib would provide two images:
1. PNG file of figure without legend.
2. PNG file of legend only.
The end user would import both images into another tool (e.g. microsoft power point) and arrange figure and legend interactively for the final product.
Example follows:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 1)
fig, (ax) = plt.subplots(nrows=1)
ax.plot( x , np.sin(2*np.pi*x) , label='Curve1')
ax.plot( x , np.sin(2*np.pi*x+0.2) , label='Curve2')
ax.set_title('Set default color cycle to rgby')
plt.savefig('without_legend.png',dpi=75)
if True: # Difficult to automatically make a location choice robust
ax.legend(loc='upper left') # in this particular case, a poor choice for placement
else:
ax.legend(loc='upper right') # in this particular case, a good choice for placement
plt.savefig('with_legend.png',dpi=75)
# worst case solution could be post processing these files with imagemagick
# begin with "composite without_legend.png with_legend.png -compose difference alpha_channel.png"
# ... then filter with alpha_channel.png against with_legend.png
# ... finally crop this to get "legend_only.png" |