Ask Your Question

combine sage plot with matplotlib

asked 2011-08-14 16:39:54 +0200

buergi gravatar image

As some others I'm also searching for a convenient way for merging sage Graphics and figures from matplotlib. Actually I don't understand why this is so difficult since Sage uses matplotlib internally. Nevertheless there seems to be no way for combining plots from both packages.

The best way would be to wrap a matplotlib plot into a Sage Graphics object. I really love Sage's plot(...)+plot(...) syntax, but unfortunately there are some important plot types missing (e.g. errorbar plot). Of course one can do all plotting in matlotlib, but it would be more convenient to handle sage objects.

I saw in the sage documentation that one can get a matplotlib figure from a Graphics object, but didn't found a way to draw on it with matplotlib.

Does anyone know any way for combining plots from those packages?

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted

answered 2012-01-19 08:04:36 +0200

ADuC812 gravatar image

updated 2012-01-19 11:36:07 +0200

For some time, I was looking for a way how to generate several sage Graphics() objects and plot them on a matplotlib canvas in an arbitrary arrangement, using .matplotlib() function. It went out not being straightforward. I decided that the solution I've found may be interesting for others as well. The following code produces a plot with a nice inset:

#make some graphs
# plot main figure
from matplotlib.figure import Figure
figure = Figure() # you may set the size here
main_plot = figure.add_axes((0.2,0.2,0.7,0.7))
g.matplotlib('a.svg', figure=figure, sub=main_plot)
# plot an inset
inset = figure.add_axes((0.6,0.2,0.3,0.3))
g_ins.matplotlib('a.svg', figure=figure, sub=inset)
# display graph (note that only single sage Graphics object has to be saved )'a.svg', figure=figure, sub=inset)

UPD: if figures are drawn strangely, add


to matplotlib() parameters. By default it is 1.0, which may be undesired.

Also, you may want to draw the figure itself, not by Graphics().save() function. Replace the last line with the following:

from matplotlib.backends.backend_agg import FigureCanvasAgg
edit flag offensive delete link more


I've made adding an example like this ``.

kcrisman gravatar imagekcrisman ( 2012-01-19 09:02:47 +0200 )edit

after messing a bit with aspect ratio, added an update about it. It would be good to add it to your example as well

ADuC812 gravatar imageADuC812 ( 2012-01-19 11:37:33 +0200 )edit

answered 2011-08-15 09:47:09 +0200

kcrisman gravatar image

The correct answer to this question is Trac 5128. Unfortunately, this has not yet been finished. But once this is done, what you want will be possible.

It's certainly possible in principle, but, as you have seen, it's quite annoying in practice. The main reason is that Sage plots are NOT really mpl plots until we actually save or show them as graphics files; until then they are Sage-y Python objects. Look at the source code file for some plot type, under the _render_on_subplot method for where mpl really is used.

Any contribution to this patch would be very welcome, by the way!

edit flag offensive delete link more



It's quite unfortunate that this trac is still not finished... Any updates?

Cheng gravatar imageCheng ( 2021-06-08 01:08:17 +0200 )edit

Unfortunately, not yet.

kcrisman gravatar imagekcrisman ( 2021-09-30 17:31:50 +0200 )edit

answered 2011-08-14 16:52:08 +0200

buergi gravatar image

Wow hey wait a minute, I found one way to do it. But unfortunately I hoped to get it working the other way round, i.e. converting a matplotlib figure into a

import matplotlib.pyplot as pl
plot(lambda x:f(x),(0,200)).matplotlib(figure=pl.gcf())
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2011-08-14 16:39:54 +0200

Seen: 3,777 times

Last updated: Jan 19 '12