This Sage worksheet is one of the tutorials developed for the MAA PREP Workshop "Sage: Using Open-Source Mathematics Software with Undergraduates" (funding provided by NSF DUE 0817071).
Thanks to Sage's integration of projects like matplotlib, Jmol, and Tachyon, Sage has comprehensive plotting capabilities. This pair of worksheets continues a more in-depth demonstration of them. This part of the tutorial consists of the following sections:
The syntax for vector fields is very similar to other multivariate constructions. Notice that the arrows are scaled appropriately, and colored by length in the 3D case.
{{{id=67| var('x,y') plot_vector_field((-y+x,y*x),(x,-3,3),(y,-3,3)) ///3d vector field plots are ideally viewed with 3d glasses (right-click on the plot and select "Style" and "Stereographic")
{{{id=147| /// }}} {{{id=146| /// }}}We can plot functions of complex variables, where the magnitude is indicated by the brightness (black=0) and the argument is indicated by the hue (red=positive real).
{{{id=66| f(z) = exp(z)#z^5 + z - 1 + 1/z complex_plot(f, (-5,5),(-5,5)) ///These plot where an expression is true, and are useful for plotting inequalities.
{{{id=94| region_plot(cos(x^2+y^2) <= 0, (x, -3, 3), (y, -3, 3),aspect_ratio=1) ///We can get fancier options as well.
{{{id=97| region_plot(sin(x)*sin(y) >= 1/4, (x,-10,10), (y,-10,10), incol='yellow', bordercol='black', borderstyle='dashed', plot_points=250,aspect_ratio=1) ///You can interpolate a surface between a set of 3d points using list_plot3d.
{{{id=150| /// }}}Here, we use the normal/Gaussian distribution on the interval from 0 to 1 to generate ten random 3D points. This utilizes the list comprehension construction, which will be discussed in more depth in the introductory programming tutorial.
{{{id=103| coordinates=[(normalvariate(0,1),normalvariate(0,1),normalvariate(0,1)) for _ in range(20)] coordinates[:10] /// [(-1.1088645533422412, 0.59676974404244254, 0.81571201790630599), (0.23431344547495428, -0.57631425541809489, -0.20587995169264531), (-1.1362464190710324, -1.5972197744755712, 0.55445914462760648), (-0.13436889318267059, -0.85717244823632477, -0.89698344270407393), (-1.1150579558688323, -0.43015494718451336, 1.095446556447734), (0.69307364565188667, -0.15849007385200445, 0.66668933993990276), (-0.20044504414875314, -0.16495554553172473, -0.20039383487710977), (0.22892230339553393, 0.58724247599436363, -0.86555576882551566), (-0.28911551975915434, -1.9094982451548888, 0.80592857107579796), (-0.99008357389209811, -1.3222131399217851, 0.42361533200558066)] }}}We can then interpolate a surface. Do you remember what the last line of this cell does?
{{{id=102| surface=list_plot3d(coordinates,num_points=100, point_list=True) pts=points(coordinates,size=10,color='red') surface+pts /// }}}Sage has access to a number of more sophisticated numerical techniques for this sort of thing as well.
{{{id=101| surface=list_plot3d(coordinates,interpolation_type='spline',num_points=40,point_list=True) surface+pts /// }}} {{{id=109| /// }}}If you plot a matrix, it is considered the z-coordinates of a grid of integer x,y coordinates, starting at the origin.
{{{id=99| zcoordinates=random_matrix(RR,5,5) coordinates=[(i,j,zcoordinates[i,j]) for i in range(5) for j in range(5)] surface=list_plot3d(zcoordinates, num_points=40) pts=points(coordinates,size=10,color='red') surface+pts /// }}} {{{id=106| surface=list_plot3d(zcoordinates, num_points=40,interpolation_type='spline',degree=4) surface+pts /// }}}Sage includes a variety of built-in graphics objects. These are particularly useful for adding to one's plot certain objects which are difficult to describe with equations, but which are basic geometric objects nonetheless. In this section we will try to demonstrate the syntax of some of the most useful of them; for most of the the contextual (remember, append '?') help will give more details.
To make one point, a coordinate pair suffices.
{{{id=171| point((3,5)) ///It doesn't matter how multiples points are generated; they must go in as input via a list (square brackets). Here, we demonstrate the hard (but naive) and easy (but a little more sophisticated) way to do this.
{{{id=173| f(x)=x^2 points([(0,f(0)), (1,f(1)), (2,f(2)), (3,f(3)), (4,f(4))]) ///Sage tries to tell how many dimensions you are working in automatically.
{{{id=136| f(x,y)=x^2-y^2 points([(x,y,f(x,y)) for x in range(5) for y in range(5)]) /// }}}The syntax for lines is the same as that for points, but you get... well, you get connecting lines too!
{{{id=83| f(x)=x^2 line([(x,f(x)) for x in range(5)]) /// }}}3d lines are automatically smoothed.
{{{id=137| f(x,y)=x^2-y^2 line([(x,y,f(x,y)) for x in range(5) for y in range(5)]) /// }}}Sage has disks and spheres of various types available. Generally the center and radius are all that is needed, but other options are possible.
{{{id=81| circle((0,1),1, aspect_ratio=1) ///Polygons will try to complete themselves and fill in the interior; otherwise the syntax is fairly self-evident.
{{{id=142| polygon([[0,0],[1,1],[1,2]]) ///In 2d, one can typeset mathematics using the 'text' command. This can be used to finetune certain types of labels. Unfortunately, in 3D the text is just text.
{{{id=82| var('x') text('$\int_0^2 x^2\, dx$', (0.5,2))+plot(x^2,(x,0,2),fill=True) /// }}} {{{id=91| var('x,y') text3d('sin(xy)', (0,0,2))+plot3d(sin(x*y),(x,-2,2),(y,-2,2)) /// }}}We can save 2d plots to many different formats. Sage can determine the format based on the filename for the image.
{{{id=141| p=plot(x^2,(x,-1,1)) p ///You can also save 3d plots to files. To do this, adjust the figure how you like, and click on "Get Image" next to the picture. Try saving a snapshot of the following 3d image to your desktop.
{{{id=156| var('x,y') p=plot3d(x^2-y^2,(x,-2,2),(y,-2,2)) p /// }}}You can view the 3d plot with a few other different backends too. If you have a recent version of Safari, Chrome, or Firefox, or if you have an iPhone, you may like this view, which uses the new HTML5 Canvas functionality.
{{{id=161| show(p,viewer='canvas3d') /// }}}Here is a backend that uses a 3d ray-tracing engine to provide photo-realistic images.
{{{id=158| show(p,viewer='tachyon') /// }}}If you try to save the 3d image using a code cell, Sage uses Tachyon.
{{{id=159| p.save('my_plot.png') /// }}}You can do many other things with Tachyon. Here are some examples from its documentation; the idea is that you have a sense of its potential, although reading the documentation would be essential for coming up with your own graphics of this sort.
{{{id=164| t = Tachyon(xres=500,yres=500, camera_center=(2,0,0)) t.light((4,3,2), 0.2, (1,1,1)) t.texture('t2', ambient=0.1, diffuse=0.9, specular=0.5, opacity=1.0, color=(1,0,0)) t.texture('t3', ambient=0.1, diffuse=0.9, specular=0.5, opacity=1.0, color=(0,1,0)) t.texture('t4', ambient=0.1, diffuse=0.9, specular=0.5, opacity=1.0, color=(0,0,1)) t.sphere((0,0.5,0), 0.2, 't2') t.sphere((0.5,0,0), 0.2, 't3') t.sphere((0,0,0.5), 0.2, 't4') t.show() /// }}} {{{id=165| t = Tachyon(xres=800,yres=800, camera_center=(2,5,2), look_at=(2.5,0,0)) t.light((0,0,100), 1, (1,1,1)) t.texture('r', ambient=0.1, diffuse=0.9, specular=0.5, opacity=1.0, color=(1,0,0)) for i in srange(0,50,0.1): t.sphere((i/10,sin(i),cos(i)), 0.05, 'r') t.texture('white', color=(1,1,1), opacity=1, specular=1, diffuse=1) t.plane((0,0,-100), (0,0,-100), 'white') t.show() /// }}} {{{id=166| t = Tachyon(xres=512,yres=512, camera_center=(3,0.3,0), raydepth=8) t.light((4,3,2), 0.2, (1,1,1)) t.texture('t0', ambient=0.1, diffuse=0.9, specular=0.5, opacity=1.0, color=(1.0,0,0)) t.texture('t1', ambient=0.1, diffuse=0.9, specular=0.3, opacity=1.0, color=(0,1.0,0)) t.texture('t2', ambient=0.2,diffuse=0.7, specular=0.5, opacity=0.7, color=(0,0,1.0)) t.texture('white', color=(1,1,1)) t.plane((0,0,-1), (0,0,1), 'white') t.plane((0,-20,0), (0,1,0), 'white') t.plane((-20,0,0), (1,0,0), 'white') k=0 for i in srange(-1,1,0.05): k += 1 t.sphere((i,i^2 - 0.5,i^3), 0.1, 't%s'%(k%3)) t.cylinder((0,0,0), (0,0,1), 0.05,'t1') t.show() /// }}} {{{id=167| /// }}}