Ask Your Question

Revision history [back]

I am very sorry to edit an answer for my own question but I finally ended with a solution. I confess that it is not completely satisfactory. It makes use of the precious post suppress border and the library PIL which is included in Sage.

First of all, I do have a graphic which is here of size 600x600 which is the Mandelbrot set in the range xmin=-2, xmax=1, ymin=-1.5, ymax=1.5.

image description

Let us do some import

sage: from PIL import Image
sage: from matplotlib.backends.backend_agg import FigureCanvasAgg
sage: import matplotlib.pyplot as plt

The graphics that I want to add are the axes the precise plot of the main cardioid and the circle that corresponds to the period 2 attracting cycle.

sage: P1 = line2d([(1-(exp(CC(0,t*2.*RR(pi)))+1)**2)/4 for t in xsrange(-.5,.5,.002)])
sage: P1 += point2d((0,0), color='green')
sage: P2 = circle((-1,0),1/4) 
sage: P2 += point2d((-1,0), color='green')
sage: G = P1 + P2
sage: G.set_axes_range(-2,1,-1.5,1.5)

Now I use the trick in the post mentioned above and save the picture

sage: pm = G.matplotlib(axes=True, axes_pad=0)
sage: pm.subplots_adjust(left=0,right=1,top=1,bottom=0)
sage: pm.set_canvas(FigureCanvasAgg(pm))
sage: pm.set_size_inches(6,6)   # with 80 dpi, this gives a 600x600 picture
sage: pm.savefig('/tmp/a.png', bbox_inches=0)

The result is seen below and one can check that it has the expected dimension 600x600.

image description

Then, using PIL, I can merge the two images

sage: mandel = Image.open('/tmp/mandel.png')
sage: axes   = Image.open('/tmp/a.png')
sage: mask = Image.new('L', (600,600), 125)
sage: mandel.paste(axes, mask)
sage: mandel.save('/tmp/mandel_with_axes.png')

image description