combine sage plot with matplotlib

i like this post (click again to cancel)
4
i dont like this post (click again to cancel)

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?

asked Aug 14 '11

buergi gravatar image buergi
73 3 9

3 Answers:

i like this answer (click again to cancel)
3
i dont like this answer (click again to cancel)

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
pl.errorbar(xs,ys,yerr=dys,fmt="ro")
plot(lambda x:f(x),(0,200)).matplotlib(figure=pl.gcf())
pl.savefig('myplot')
link

posted Aug 14 '11

buergi gravatar image buergi
73 3 9
i like this answer (click again to cancel)
3
i dont like this answer (click again to cancel)

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!

link

posted Aug 15 '11

kcrisman gravatar image kcrisman
6784 14 67 152
i like this answer (click again to cancel)
1
i dont like this answer (click again to cancel)

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
x=var('x')
g=plot(sin(x))
g_ins=plot(cos(x))
# 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 )
g_ins.save('a.svg', figure=figure, sub=inset)

UPD: if figures are drawn strangely, add

aspect_ratio='automatic'

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
figure.set_canvas(FigureCanvasAgg(figure)) 
figure.savefig('a.svg')
link

posted Jan 19 '12

ADuC812 gravatar image ADuC812
68 1 8

updated Jan 19 '12

I've made adding an example like this http://trac.sagemath.org/sage_trac/ticket/12326.

kcrisman (Jan 19 '12)

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 (Jan 19 '12)

Your answer

Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
[hide preview]

Question tools

1 follower

Tags:

Stats:

Asked: Aug 14 '11

Seen: 477 times

Last updated: Jan 19 '12

powered by ASKBOT version 0.7.22
Copyright Sage, 2010. Some rights reserved under creative commons license.