How do I add a legend to a plot in Sage?

309 views
Skip to first unread message

Stan Schymanski

unread,
Jun 30, 2008, 8:41:05 AM6/30/08
to sage-support
I would like to be able to add legends to plots containing different
lines or functions. Could anyone give an example of how to achieve
this in SAGE?
For example, the following plot would be nice with a legend containing
the line colours and descriptions:

sage: P1=plot(x^2,0,3)
sage: P1=plot(x^2,0,3,rgbcolor="red")
sage: P2=plot(x^3,0,3,rgbcolor="blue")
sage: show(P1+P2,axes_labels=['$x$', '$y$'])

I read up a bit, but wasn't able to use any of the examples given for
Matplotlib directly in my SAGE notebook. Thanks already for your help!

Regards,
Stan

William Stein

unread,
Jun 30, 2008, 10:02:40 AM6/30/08
to sage-s...@googlegroups.com, emily kirkman

There is unfortunately no good way to add legens to Sage plots
yet. When I desparatly need to do this, I use the text command
and explicitly put text and a line on the plot, but of course that
is not optimal.

You can use the pylab/matplotlab plotting functionality which
does allow for legends. The following illustrates using pylab,
which comes with sage, but doesn't illustrate how to make
legends (though pylab does support that).

sage: from pylab import *
sage: t = arange(0.0, 2.0, 0.01)
sage: s = sin(2*pi*t)
sage: P = plot(t, s, linewidth=1.0)
sage: xl = xlabel('time (s)')
sage: yl = ylabel('voltage (mV)')
sage: t = title('About as simple as it gets, folks')
sage: grid(True)
sage: savefig('sage.png')

I really hope somebody will add support for legends
to sage plots soon. (Emily: hint hint :-)

William

Martin Albrecht

unread,
Jun 30, 2008, 10:13:37 AM6/30/08
to sage-s...@googlegroups.com
On Monday 30 June 2008, William Stein wrote:
> On Mon, Jun 30, 2008 at 5:41 AM, Stan Schymanski <schy...@gmail.com> wrote:
> > I would like to be able to add legends to plots containing different
> > lines or functions. Could anyone give an example of how to achieve
> > this in SAGE?
> > For example, the following plot would be nice with a legend containing
> > the line colours and descriptions:
> >
> > sage: P1=plot(x^2,0,3)
> > sage: P1=plot(x^2,0,3,rgbcolor="red")
> > sage: P2=plot(x^3,0,3,rgbcolor="blue")
> > sage: show(P1+P2,axes_labels=['$x$', '$y$'])
> >
> > I read up a bit, but wasn't able to use any of the examples given for
> > Matplotlib directly in my SAGE notebook. Thanks already for your help!
>
> There is unfortunately no good way to add legens to Sage plots
> yet. When I desparatly need to do this, I use the text command
> and explicitly put text and a line on the plot, but of course that
> is not optimal.
>
> You can use the pylab/matplotlab plotting functionality which
> does allow for legends. The following illustrates using pylab,
> which comes with sage, but doesn't illustrate how to make
> legends (though pylab does support that).

This one does:

# data
x = srange(10)
d1 = [e^1.8 for e in x]
d2 = [e^1.7 + 1.2 for e in x]

import pylab
pylab.clf() # clear the figure first
pylab.figure(1)
# plot some data and add a legend
pylab.plot(x,d1, label="Foo")
pylab.plot(x,d2,label="Bar")
pylab.legend() # print the legend
pylab.title("$n^X$ FooBar")
pylab.ylabel("fubar") # label the axes
pylab.xlabel("0-9")
pylab.savefig('foo.png',dpi=72) # fire!

--
name: Martin Albrecht
_pgp: http://pgp.mit.edu:11371/pks/lookup?op=get&search=0x8EF0DC99
_www: http://www.informatik.uni-bremen.de/~malb
_jab: martinr...@jabber.ccc.de

Stan Schymanski

unread,
Jul 1, 2008, 5:18:25 AM7/1/08
to sage-support
Thanks a lot, that's great! I don't understand how to change the
legend descriptioins "Foo" and "Bar" in the first example, but the
second one is pretty straightforward. I look forward to an
implemenation in SAGE plot.

Regards,
Stan

On Jun 30, 4:13 pm, Martin Albrecht <m...@informatik.uni-bremen.de>
wrote:
> _jab: martinralbre...@jabber.ccc.de
Reply all
Reply to author
Forward
0 new messages