SourceForge logo
SourceForge logo
Menu

matplotlib-users

From: Jonathan B. <jdt...@gm...> - 2012年04月12日 06:06:14
Hi all,
Forgive me as this is the first time I've posted here. I've asked a
question on StackOverFlow:
http://stackoverflow.com/questions/10101700/moving-matplotlib-legend-outside-of-the-axis-makes-it-cutoff-by-the-figure-box#comment12952803_10101700
The question relates to adjusting the size of the figure box to accommodate
a large legend when the legend is placed below instead of on top of the
axes.
I thought I'd post here to see if there are any other answers to avoiding
having the figure box cut off the bottom of the legend.
Thanks
Jonathan
-- 
There are no passengers on Spaceship Earth. We are all crew.
E: jdt...@gm...
From: Benjamin R. <ben...@ou...> - 2012年04月12日 13:51:55
On Thu, Apr 12, 2012 at 2:05 AM, Jonathan Bruck <jdt...@gm...> wrote:
> Hi all,
>
> Forgive me as this is the first time I've posted here. I've asked a
> question on StackOverFlow:
>
>
> http://stackoverflow.com/questions/10101700/moving-matplotlib-legend-outside-of-the-axis-makes-it-cutoff-by-the-figure-box#comment12952803_10101700
>
> The question relates to adjusting the size of the figure box to
> accommodate a large legend when the legend is placed below instead of on
> top of the axes.
>
> I thought I'd post here to see if there are any other answers to avoiding
> having the figure box cut off the bottom of the legend.
>
> Thanks
>
> Jonathan
> --
> There are no passengers on Spaceship Earth. We are all crew.
>
> E: jdt...@gm...
>
>
If you only care about saving the figure, the savefig() method can take
bbox='tight' and bbox_extra_artists=[legnd_obj] arguments (assuming you
save the legend to such a variable. As for on-screen displays, I have yet
to find a solution.
Ben Root
From: Jonathan B. <jdt...@gm...> - 2012年04月13日 06:21:53
Thanks for a response, but unfortunately it doesn't seem to work. I have
some sample code on pastebin http://pastebin.com/W6JmbCsz in case the
following does not email out well
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 12 11:16:03 2012
Using the current stable version of pythonxy on Windows 7 32bit
Author: Jonathan
Notice how the figure box is not placed correctly, effectively missing the
legend entirely
"""
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(-2*np.pi, 2*np.pi, 0.1)
fig = plt.figure(1, figsize=(8,6))
ax = fig.add_subplot(111)
ax.plot(x, np.sin(x), label='Sine')
ax.plot(x, np.cos(x), label='Cosine')
ax.plot(x, np.arctan(x), label='Inverse tan')
handles, labels = ax.get_legend_handles_labels()
lgd = ax.legend(handles, labels, loc=9, bbox_to_anchor=(0.5,-0.1))
ax.grid('on')
#fig.tight_layout()
fig.savefig('samplefigure', bbox_extra_artists=(lgd,), bbox='tight')
On Thu, Apr 12, 2012 at 11:51 PM, Benjamin Root <ben...@ou...> wrote:
>
>
> On Thu, Apr 12, 2012 at 2:05 AM, Jonathan Bruck <jdt...@gm...>wrote:
>
>> Hi all,
>>
>> Forgive me as this is the first time I've posted here. I've asked a
>> question on StackOverFlow:
>>
>>
>> http://stackoverflow.com/questions/10101700/moving-matplotlib-legend-outside-of-the-axis-makes-it-cutoff-by-the-figure-box#comment12952803_10101700
>>
>> The question relates to adjusting the size of the figure box to
>> accommodate a large legend when the legend is placed below instead of on
>> top of the axes.
>>
>> I thought I'd post here to see if there are any other answers to avoiding
>> having the figure box cut off the bottom of the legend.
>>
>> Thanks
>>
>> Jonathan
>> --
>> There are no passengers on Spaceship Earth. We are all crew.
>>
>> E: jdt...@gm...
>>
>>
> If you only care about saving the figure, the savefig() method can take
> bbox='tight' and bbox_extra_artists=[legnd_obj] arguments (assuming you
> save the legend to such a variable. As for on-screen displays, I have yet
> to find a solution.
>
> Ben Root
>
>
-- 
There are no passengers on Spaceship Earth. We are all crew.
Jonathan Bruck
E: jdt...@gm...
Mob: 0421188951
Bachelor of Engineering (Mechanical, Biomedical), Bachelor of Medical
Science
--
From: Benjamin R. <ben...@ou...> - 2012年04月13日 13:10:00
On Fri, Apr 13, 2012 at 2:21 AM, Jonathan Bruck <jdt...@gm...> wrote:
> Thanks for a response, but unfortunately it doesn't seem to work. I have
> some sample code on pastebin http://pastebin.com/W6JmbCsz in case the
> following does not email out well
>
> # -*- coding: utf-8 -*-
> """
> Created on Thu Apr 12 11:16:03 2012
>
> Using the current stable version of pythonxy on Windows 7 32bit
> Author: Jonathan
>
>
> Notice how the figure box is not placed correctly, effectively missing the
> legend entirely
> """
>
> import matplotlib.pyplot as plt
> import numpy as np
>
> x = np.arange(-2*np.pi, 2*np.pi, 0.1)
> fig = plt.figure(1, figsize=(8,6))
> ax = fig.add_subplot(111)
> ax.plot(x, np.sin(x), label='Sine')
> ax.plot(x, np.cos(x), label='Cosine')
> ax.plot(x, np.arctan(x), label='Inverse tan')
> handles, labels = ax.get_legend_handles_labels()
> lgd = ax.legend(handles, labels, loc=9, bbox_to_anchor=(0.5,-0.1))
> ax.grid('on')
> #fig.tight_layout()
> fig.savefig('samplefigure', bbox_extra_artists=(lgd,), bbox='tight')
>
>
>
I apologize, it was bbox_inches='tight', not bbox='tight'. Because savefig
has to be able to take arbitrary kwargs that get passed down to the
backends, it does not check for incorrect kwargs.
Ben Root
From: solarg <sol...@gm...> - 2012年05月31日 04:28:52
hello all,
i've tried it on my laptop, but got this error at the last line:
 >>> fig.savefig('samplefigure', bbox_extra_artists=(lgd,), 
bbox_inches='tight')
Traceback (most recent call last):
 File "<console>", line 1, in <module>
 File 
"/Users/me/python/virtualenv/bmfvca6/lib/python2.6/site-packages/matplotlib/figure.py", 
line 1084, in savefig
 self.canvas.print_figure(*args, **kwargs)
 File 
"/Users/me/python/virtualenv/bmfvca6/lib/python2.6/site-packages/matplotlib/backend_bases.py", 
line 1894, in print_figure
 in kwargs.pop("bbox_extra_artists", [])]
TypeError: get_window_extent() takes exactly 1 argument (2 given)
does it mean that i need to upgrade to python 2.7 ?
thanks in advance for help,
gerard
From: Benjamin R. <ben...@ou...> - 2012年06月01日 13:59:46
On Thursday, May 31, 2012, solarg wrote:
> hello all,
>
> i've tried it on my laptop, but got this error at the last line:
>
> >>> fig.savefig('samplefigure', bbox_extra_artists=(lgd,),
> bbox_inches='tight')
> Traceback (most recent call last):
> File "<console>", line 1, in <module>
> File
>
> "/Users/me/python/virtualenv/bmfvca6/lib/python2.6/site-packages/matplotlib/figure.py",
> line 1084, in savefig
> self.canvas.print_figure(*args, **kwargs)
> File
>
> "/Users/me/python/virtualenv/bmfvca6/lib/python2.6/site-packages/matplotlib/backend_bases.py",
> line 1894, in print_figure
> in kwargs.pop("bbox_extra_artists", [])]
> TypeError: get_window_extent() takes exactly 1 argument (2 given)
>
> does it mean that i need to upgrade to python 2.7 ?
>
> thanks in advance for help,
>
> gerard
>
>
Unlikely. Which version of matplotlib? It might be a bug in an older
version of mpl.
Ben Root
From: solarg <sol...@gm...> - 2012年06月04日 05:25:42
On 06/01/12 03:59 PM, Benjamin Root wrote:
>
>
>
> Unlikely. Which version of matplotlib? It might be a bug in an older 
> version of mpl.
>
> Ben Root
i'm using 1.0.1 onto macosx.
thanks for your help,
gerard
From: solarg <sol...@gm...> - 2012年06月04日 05:39:25
On 06/04/12 07:25 AM, solarg wrote:
> On 06/01/12 03:59 PM, Benjamin Root wrote:
>>
>>
>>
>> Unlikely. Which version of matplotlib? It might be a bug in an older 
>> version of mpl.
>>
>> Ben Root
>
> i'm using 1.0.1 onto macosx.
>
> thanks for your help,
>
> gerard
i've just tried with 1.1.0 on solaris 11 and it works. So the problem is 
related with 1.0.1
thanks for help,
gerard
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.
Thanks for helping keep SourceForge clean.
X





Briefly describe the problem (required):
Upload screenshot of ad (required):
Select a file, or drag & drop file here.
Screenshot instructions:

Click URL instructions:
Right-click on the ad, choose "Copy Link", then paste here →
(This may not be possible with some types of ads)

More information about our ad policies

Ad destination/click URL:

AltStyle によって変換されたページ (->オリジナル) /