Using bbox_to_anchor to get the legend off of my graph

Hi,

I am graphing several time series together on the same graph,

and so it is important to have a legend. I am also stacking two graphs

on top of one another, because some of the data is of a different type.

I wrote the graphing code as follows (pardon the newbie code, this is my first matplotlib day…)

fig = plt.figure()

axU = fig.add_subplot(211)

axU.set_ylabel(‘price,$’, color=‘r’)

axU.grid(True)

axL = fig.add_subplot(212)

axL.set_ylabel(‘size, shs’, color=‘b’)

axL.grid(True)

stepPlotsU = []

stepPlotsL = []

for i in range( len( plotvalues ) ) :

if axisSide[i] == ‘U’ :

stepPlotsU.append(axU.step( T,plotvalues[i], where=‘post’ ))

else :

stepPlotsL.append(axL.step( T,plotvalues[i], where=‘post’ ))

lU = axU.legend(tuple(stepPlotsU),tuple(legendArrayU),loc=‘upper right’)

lL = axL.legend( tuple(stepPlotsL),tuple(legendArrayL),loc=‘upper right’)

axU.axis([float(TMin),float(TMax),1.1 * minValueU -0.1 * maxValueU,1.1maxValueU - 0.1minValueU])

axL.axis([float(TMin),float(TMax),0.8minValueL,1.2maxValueL])

axU.set_xlabel(“Time, secs”)

fig.savefig(‘test.png’)

My code works, and produces an almost perfect graph, BUT…

My problem is that the legend is quite large, and covers up a lot of the graph.

The solution seems to be in the user guide, here

<http://matplotlib.sourceforge.net/users/plotting/legend.html#plotting-guide-legend>

where it shows how to call legend with the kwarg bbox_to_anchor=(1.05,1), and in the

example, the box is nicely moved.

In my case, though, I am calling

axU.legend(tuple(stepPlotsU),tuple(legendArrayU),loc=‘upper right’, bbox_to_anchor=(1.05,1))

but it says this is an “unexpected keyword)”, I assume it is saying that axis.legend() doesn’t use this

keyword, even if legend does.

lL = axL.legend( tuple(stepPlotsL),tuple(legendArrayL),loc=‘upper right’,bbox_to_anchor=(1.05,1))

File “/etg/source/Linux/pkg/Python-2.6.2/lib/python2.6/site-packages/matplotlib/axes.py”, line 3823, in legend

self.legend_ = mlegend.Legend(self, handles, labels, **kwargs)

TypeError: init() got an unexpected keyword argument ‘bbox_to_anchor’

Does anyone out there know what I can do to get the legend off my graph? And is there a way

to shrink the legend font size down? It is way too large, and I can’t find anything in the docs about this,

that axis.legend() will accept…

Dave

PS: I am using Python 2.6.2, matplotlib 0.98.5.3, numpy 1.3.0, scipy 0.7.1

DISCLAIMER:

This e-mail, and any attachments thereto, is intended only for use by the addressee(s) named herein and may contain legally privileged and/or confidential information. If you are
not the intended recipient of this e-mail, you are hereby notified that any dissemination, distribution or copying of this e-mail, and any attachments thereto, is strictly prohibited. If you have received this in error, please immediately notify me and permanently
delete the original and any copy of any e-mail and any printout thereof. E-mail transmission cannot be guaranteed to be secure or error-free. The sender therefore does not accept liability for any errors or omissions in the contents of this message which arise
as a result of e-mail transmission.

NOTICE REGARDING PRIVACY AND CONFIDENTIALITY Knight Capital Group may, at its discretion, monitor and review the content of all e-mail communications. http://www.knight.com

···

The guide is based on mpl 0.99.
You may upgrade your mpl,
or take a look at the thread below. it is a workaround that will work
with older version.

-JJ

···

On Fri, Aug 7, 2009 at 8:29 PM, Eliezer, David<deliezer@...2724...> wrote:

Hi,
I am graphing several time series together on the same graph,
and so it is important to have a legend. I am also stacking two graphs
on top of one another, because some of the data is of a different type.

I wrote the graphing code as follows (pardon the newbie code, this is my
first matplotlib day…)

fig = plt.figure()
axU = fig.add_subplot(211)
axU.set_ylabel('price,$', color='r')
axU.grid(True)
axL = fig.add_subplot(212)
axL.set_ylabel('size, shs', color='b')
axL.grid(True)
stepPlotsU = []
stepPlotsL = []
for i in range( len( plotvalues ) ) :
if axisSide[i] == 'U' :
stepPlotsU.append(axU.step( T,plotvalues[i], where='post' ))
else :
stepPlotsL.append(axL.step( T,plotvalues[i], where='post' ))

lU = axU.legend(tuple(stepPlotsU),tuple(legendArrayU),loc='upper right')
lL = axL.legend( tuple(stepPlotsL),tuple(legendArrayL),loc='upper right')
axU.axis([float(TMin),float(TMax),1.1 * minValueU -0.1 *
maxValueU,1.1*maxValueU - 0.1*minValueU])
axL.axis([float(TMin),float(TMax),0.8*minValueL,1.2*maxValueL])
axU.set_xlabel("Time, secs")
fig.savefig('test.png')

My code works, and produces an almost perfect graph, BUT…
My problem is that the legend is quite large, and covers up a lot of the
graph.
The solution seems to be in the user guide, here

<http://matplotlib.sourceforge.net/users/plotting/legend.html#plotting-guide-legend>

where it shows how to call legend with the kwarg bbox_to_anchor=(1.05,1),
and in the
example, the box is nicely moved.

In my case, though, I am calling

axU.legend(tuple(stepPlotsU),tuple(legendArrayU),loc='upper right',
bbox_to_anchor=(1.05,1))

but it says this is an “unexpected keyword)”, I assume it is saying that
axis.legend() doesn’t use this
keyword, even if legend does.

lL = axL\.legend\( tuple\(stepPlotsL\),tuple\(legendArrayL\),loc=&#39;upper

right',bbox_to_anchor=(1.05,1))
File
"/etg/source/Linux/pkg/Python-2.6.2/lib/python2.6/site-packages/matplotlib/axes.py",
line 3823, in legend
self.legend_ = mlegend.Legend(self, handles, labels, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'bbox_to_anchor'

Does anyone out there know what I can do to get the legend off my graph?
And is there a way
to shrink the legend font size down? It is way too large, and I can’t find
anything in the docs about this,
that axis.legend() will accept…

                                            Dave

PS: I am using Python 2.6.2, matplotlib 0.98.5.3, numpy 1.3.0, scipy 0.7.1

DISCLAIMER:
This e-mail, and any attachments thereto, is intended only for use by the
addressee(s) named herein and may contain legally privileged and/or
confidential information. If you are not the intended recipient of this
e-mail, you are hereby notified that any dissemination, distribution or
copying of this e-mail, and any attachments thereto, is strictly prohibited.
If you have received this in error, please immediately notify me and
permanently delete the original and any copy of any e-mail and any printout
thereof. E-mail transmission cannot be guaranteed to be secure or
error-free. The sender therefore does not accept liability for any errors or
omissions in the contents of this message which arise as a result of e-mail
transmission.
NOTICE REGARDING PRIVACY AND CONFIDENTIALITY Knight Capital Group may, at
its discretion, monitor and review the content of all e-mail communications.
http://www.knight.com

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus
on
what you do best, core application coding. Discover what's new with
Crystal Reports now. http://p.sf.net/sfu/bobj-july
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users