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:
-
PNG file of figure without legend.
-
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(2np.pix) , label=‘Curve1’)
ax.plot( x , np.sin(2np.pix+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”
Hi Peter,
just get the legend handlers and labels with
handles,labels = ax.get_legend_handles_labels()
then create an empty plot with axes axe
and do
axe.legend(handles, labels, loc=loc)
If you want to hide the axis:
axe.xaxis.set_visible(False)
axe.yaxis.set_visible(False)
and/or
for v in axe.spines.values():
v.set_visible(False)
Enjoy,
Fra
···
2014-01-31 Peter Van Wieren <p_vw@…4485…>
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:
- PNG file of figure without legend.
- 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(2np.pix) , label=‘Curve1’)
ax.plot( x , np.sin(2np.pix+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”
WatchGuard Dimension instantly turns raw network data into actionable
security intelligence. It gives you real-time visual feedback on key
security issues and trends. Skip the complicated setup - simply import
a virtual appliance and go from zero to informed in seconds.
http://pubads.g.doubleclick.net/gampad/clk?id=123612991&iu=/4140/ostg.clktrk
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
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.
As someone pointed out to me not long ago, you can call
my_legend.draggable(True)
then drag the legend where you want (in normal pointer mode). Then you
just need to save the figure and not worry about fiddling with it
later.
Skip Montanaro
I would also like to point out that you can specify “auto” for a location, and matplotlib will attempt to find a good location for you (within the plot area). It isn’t perfect, but it can be useful.
Cheers!
Ben Root
···
On Fri, Jan 31, 2014 at 7:02 AM, Skip Montanaro <skip@…789…> wrote:
- PNG file of figure without legend.
- 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.
As someone pointed out to me not long ago, you can call
my_legend.draggable(True)
then drag the legend where you want (in normal pointer mode). Then you
just need to save the figure and not worry about fiddling with it
later.
Skip Montanaro
WatchGuard Dimension instantly turns raw network data into actionable
security intelligence. It gives you real-time visual feedback on key
security issues and trends. Skip the complicated setup - simply import
a virtual appliance and go from zero to informed in seconds.
http://pubads.g.doubleclick.net/gampad/clk?id=123612991&iu=/4140/ostg.clktrk
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Is it legend(loc='auto') or legend(loc='best')? I always used the latter. I think that http://matplotlib.org/api/legend_api.html supports me here.
-Sterling
···
On Jan 31, 2014, at 8:31AM, Benjamin Root wrote:
I would also like to point out that you can specify "auto" for a location, and matplotlib will attempt to find a good location for you (within the plot area). It isn't perfect, but it can be useful.
Cheers!
Ben Root
On Fri, Jan 31, 2014 at 7:02 AM, Skip Montanaro <skip@...789...> wrote:
> 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.
As someone pointed out to me not long ago, you can call
my_legend.draggable(True)
then drag the legend where you want (in normal pointer mode). Then you
just need to save the figure and not worry about fiddling with it
later.
Skip Montanaro
------------------------------------------------------------------------------
WatchGuard Dimension instantly turns raw network data into actionable
security intelligence. It gives you real-time visual feedback on key
security issues and trends. Skip the complicated setup - simply import
a virtual appliance and go from zero to informed in seconds.
http://pubads.g.doubleclick.net/gampad/clk?id=123612991&iu=/4140/ostg.clktrk
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options
------------------------------------------------------------------------------
WatchGuard Dimension instantly turns raw network data into actionable
security intelligence. It gives you real-time visual feedback on key
security issues and trends. Skip the complicated setup - simply import
a virtual appliance and go from zero to informed in seconds.
http://pubads.g.doubleclick.net/gampad/clk?id=123612991&iu=/4140/ostg.clktrk_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options