Opened 8 years ago

Last modified 5 years ago

#12326 new enhancement

Add example(s) to documentation of combining Sage with matplotlib

Reported by: kcrisman Owned by: jason, was
Priority: minor Milestone: sage-6.4
Component: graphics Keywords: matplotlib graphics sage combine
Cc: Merged in:
Authors: Reviewers:
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Description (last modified by kcrisman)

From this ask.sagemath.org answer, an interesting sort of example (maybe could be spruced a little) which could be useful in the documentation for plotting.

#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')

The user says

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.

Change History (6)

comment:1 Changed 8 years ago by kcrisman

  • Description modified (diff)

comment:2 Changed 6 years ago by jdemeyer

  • Milestone changed from sage-5.11 to sage-5.12

comment:3 Changed 6 years ago by zimmerma

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).

Paul

comment:4 Changed 6 years ago by vbraun_spam

  • Milestone changed from sage-6.1 to sage-6.2

comment:5 Changed 5 years ago by vbraun_spam

  • Milestone changed from sage-6.2 to sage-6.3

comment:6 Changed 5 years ago by vbraun_spam

  • Milestone changed from sage-6.3 to sage-6.4
Note: See TracTickets for help on using tickets.