Sage: Ticket #12326: Add example(s) to documentation of combining Sage with matplotlib
https://trac.sagemath.org/ticket/12326
<p>
From <a class="ext-link" href="http://ask.sagemath.org/question/703/combine-sage-plot-with-matplotlib?answer=1734#1734"><span class="icon"></span>this ask.sagemath.org answer</a>, an interesting sort of example (maybe could be spruced a little) which could be useful in the documentation for plotting.
</p>
<pre class="wiki">#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()
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')
</pre><p>
The user says
</p>
<blockquote class="citation">
<p>
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.
</p>
</blockquote>
en-usSagehttps://trac.sagemath.org/chrome/site/logo_sagemath_trac.png
https://trac.sagemath.org/ticket/12326
Trac 1.1.6kcrismanFri, 20 Jan 2012 01:41:35 GMTdescription changed
https://trac.sagemath.org/ticket/12326#comment:1
https://trac.sagemath.org/ticket/12326#comment:1
<ul>
<li><strong>description</strong>
modified (<a href="/ticket/12326?action=diff&version=1">diff</a>)
</li>
</ul>
TicketjdemeyerTue, 13 Aug 2013 15:35:53 GMTmilestone changed
https://trac.sagemath.org/ticket/12326#comment:2
https://trac.sagemath.org/ticket/12326#comment:2
<ul>
<li><strong>milestone</strong>
changed from <em>sage-5.11</em> to <em>sage-5.12</em>
</li>
</ul>
TicketzimmermaThu, 22 Aug 2013 13:43:51 GMT
https://trac.sagemath.org/ticket/12326#comment:3
https://trac.sagemath.org/ticket/12326#comment:3
<p>
apparently this shows how to plot using matplotlib a graph made with Sage plot command.
I'm looking for the other way round: plot with Sage a matplotlib graphics (for example made by
the pylab.imshow command).
</p>
<p>
Paul
</p>
Ticketvbraun_spamThu, 30 Jan 2014 21:20:52 GMTmilestone changed
https://trac.sagemath.org/ticket/12326#comment:4
https://trac.sagemath.org/ticket/12326#comment:4
<ul>
<li><strong>milestone</strong>
changed from <em>sage-6.1</em> to <em>sage-6.2</em>
</li>
</ul>
Ticketvbraun_spamTue, 06 May 2014 15:20:58 GMTmilestone changed
https://trac.sagemath.org/ticket/12326#comment:5
https://trac.sagemath.org/ticket/12326#comment:5
<ul>
<li><strong>milestone</strong>
changed from <em>sage-6.2</em> to <em>sage-6.3</em>
</li>
</ul>
Ticketvbraun_spamSun, 10 Aug 2014 16:51:03 GMTmilestone changed
https://trac.sagemath.org/ticket/12326#comment:6
https://trac.sagemath.org/ticket/12326#comment:6
<ul>
<li><strong>milestone</strong>
changed from <em>sage-6.3</em> to <em>sage-6.4</em>
</li>
</ul>
Ticket