Lecture 16. Drawing Graphics: 2d plots

 

Sage has plotting support that covers:

Sage uses the Python library Matplotlib (http://matplotlib.sourceforge.net/) is used under the hood to render 2d graphics; for 3d graphics, Sage can use a Java applet (jmol), an HTML5 canvas renderer, or a raytracer.  

In this worksheet, we'll explain how to use the "mathematica-style" 2d plotting capabilities of Sage.

{{{id=7| /// }}}

Drawing Lines

First, we'll discuss a simple but very powerful plotting command in Sage called "line".  It takes as input a list of points, and draws a sequence of line segments connecting them.    The points are given as 2-tuples (x,y), which are the x and y coordinates of a point in the plane.   The output of calling the line command is a line object.  

{{{id=5| L = line([(-2,-2), (3,8), (5, 5)]) print L /// Graphics object consisting of 1 graphics primitive }}}

To see the actual plot of L, just put L by itself on a line or type show(L) or L.show():

{{{id=4| L /// }}} {{{id=3| show(L) /// }}} {{{id=1| L.show() /// }}}

Useful function: zip() takes two arrays of the same length and returns a list of tuples of pairwise elements:

{{{id=36| zip([-2,3,5],[-2,8,5]) /// [(-2, -2), (3, 8), (5, 5)] }}} {{{id=34| line(zip([-2,3,5],[-2,8,5])) /// }}}

Incidentally, there are many, many options that you can pass to the show command.   The three most useful are:

You can combine these options.  For example:

{{{id=10| L.show(frame=True, gridlines=True, figsize=[8,2]) /// }}}

 In the notebook you can just click and download the default plots displayed above, since they are png images.   However, if you want to include images in a paper you're writing, or use an image in an editor such as Inkscape, it's much better to save the images in other formats.   Thus a critically useful command is L.save('filename.ext'), which enables you to save a graphics object to a file.  The extension of the filename determines the type of the file.  For example, below we save L to pdf, eps, and svg formats.  Note that the svg image just appears embedded in your web browser, and you can pan around.  In any case, you can always (right or control) click on the link or image to save it as a file on your computer.     

{{{id=14| L.save('image.pdf') L.save('image.eps') L.save('image.svg') /// }}}

Lines (and all other graphics objects) have numerous properties that you can adjust, which you find in the documentation.  The most important properties of lines are:

{{{id=17| line([(-2,-2), (3,8), (5, 5)], color='purple', thickness=3, linestyle='--') /// }}} {{{id=16| line([(-2,-2), (3,8), (5, 5)], color='#042a99', thickness=1.5, linestyle=':') /// }}}

Let's have some fun:

{{{id=13| line([(random(), random()) for _ in range(100)], color='purple') /// }}}

Arithmetic: a key unusual idea in Sage graphics is that you combine together different graphics using +, as illustrated below:

{{{id=20| L1 = line([(0,0), (1,1), (2,0)], color='green', thickness=7) L2 = line([(1,0), (2,5), (3,0)], color='purple', thickness=10, alpha=.7) # alpha = transparency L1 + L2 /// }}}

There are numerous other important plotting commands in Sage, including point, circle, polygon, arrow, and text, as illustrated below:

{{{id=21| G = point((1,1), pointsize=200) + circle((1,1), .5) # zorder makes sure that triangle is on top G += polygon([(0,0), (1,.6), (2,0)], color='purple', zorder=5) G += arrow((1,1), (2,1.2), color='green') # You can use TeX formulas: G += text(r"$\sqrt{\sin(\pi x^2)}$",(1.8,1.35),color='black',fontsize=20) G.show(aspect_ratio = 1) /// }}}

There are also a function just called "plot" that makes a plot of a wide range of Sage objects.  It is very useful especially for plotting functions of one variable.  It is probably the most used Sage plotting function.  The result is a graphics object, which you can use just like any graphics object discussed above.

{{{id=25| plot(x*sin(1/x), (x, -1, 5), color='green', thickness=2) /// }}}

matrix_plot is another similar plotting function, which allows you to visualize a matrix.

{{{id=27| A = random_matrix(RDF,100); matrix_plot(A) /// }}} {{{id=12| matrix_plot(A^2) /// }}}

Finally, there is a graphics_array function that lets you assemble several independent plots into a single big plot.

{{{id=29| graphics_array([[matrix_plot(A), matrix_plot(A^2)], [plot(sin), plot(cos,color='red')]]) /// }}}

Bonus -- you can animate graphics.  Given any list of graphics objects, the animate command will make a single animated GIF file out of them.  For example:

{{{id=33| v = [plot(sin(sin(a)*x), (x,0,10)) for a in [0,0.1,..,2*pi]] z = animate(v, xmin=0,xmax=10,ymin=-1,ymax=1) z.show(delay=5) /// }}} {{{id=31| /// }}}