PREP Tutorial, Advanced Plotting, Part 2
system:sage


<h1>Sage Tutorial for Advanced Plotting, Part 2</h1>
<p>This&nbsp;<a href="http://www.sagemath.org" target="_blank">Sage</a>&nbsp;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).</p>
<p>Thanks to Sage's integration of projects like <a href="http://matplotlib.sourceforge.net/" target="_blank">matplotlib</a>, <a href="http://jmol.sourceforge.net/" target="_blank">Jmol</a>, and <a href="http://jedi.ks.uiuc.edu/~johns/raytracer/" target="_blank">Tachyon</a>, Sage has comprehensive plotting capabilities.  This pair of worksheets continues a more in-depth demonstration of them. &nbsp; This part of the tutorial consists of the following sections:</p>
<ul>
<li><a href="#Vector">Vector Fields</a></li>
<li><a href="#Complex">Complex Plots</a></li>
<li><a href="#Region">Region Plots</a></li>
<li><a href="#Interpolate">Interpolated Surfaces</a></li>
<li><a href="#Builtin">Builtin Graphics Objects</a></li>
<li><a href="#Saving">Saving Plots</a></li>
</ul>
<h1 id="Vector">Vector fields</h1>
<p>The syntax for vector fields is very similar to other multivariate constructions. &nbsp;Notice that the arrows are scaled appropriately, and colored by length in the 3D case.</p>

{{{id=67|
var('x,y')
plot_vector_field((-y+x,y*x),(x,-3,3),(y,-3,3))
///
<html><font color='black'><img src='cell://sage0.png'></font></html>
}}}

{{{id=148|
var('x,y,z')
plot_vector_field3d((-y,-z,x), (x,-3,3),(y,-3,3),(z,-3,3))
///
}}}

<p>3d vector field plots are ideally viewed with 3d glasses (right-click on the plot and select "Style" and "Stereographic")</p>

{{{id=147|

///
}}}

{{{id=146|

///
}}}

<h1 id="Complex">Complex Plots</h1>
<p>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).</p>

{{{id=66|
f(z) = exp(z)#z^5 + z - 1 + 1/z
complex_plot(f, (-5,5),(-5,5))
///
<html><font color='black'><img src='cell://sage0.png'></font></html>
}}}

<h1 id="Region">Region plots</h1>
<p>These plot where an expression is true, and are useful for plotting inequalities.</p>

{{{id=94|
region_plot(cos(x^2+y^2) <= 0, (x, -3, 3), (y, -3, 3),aspect_ratio=1)
///
<html><font color='black'><img src='cell://sage0.png'></font></html>
}}}

<p>We can get fancier options as well.</p>

{{{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)
///
<html><font color='black'><img src='cell://sage0.png'></font></html>
}}}

{{{id=96|

///
}}}

<h1 id="Interpolate">Interpolated Surfaces</h1>
<p>You can interpolate a surface between a set of 3d points using list_plot3d.</p>

{{{id=150|

///
}}}

<p>Here, we use the normal/Gaussian distribution on the interval from 0 to 1 to generate ten random 3D points. &nbsp;This utilizes the list comprehension construction, which will be discussed in more depth in the introductory programming tutorial.</p>

{{{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)]
}}}

<p>We can then interpolate a surface. &nbsp;Do you remember what the last line of this cell does?</p>

{{{id=102|
surface=list_plot3d(coordinates,num_points=100, point_list=True)
pts=points(coordinates,size=10,color='red')
surface+pts
///
}}}

<p>Sage has access to a number of more sophisticated numerical techniques for this sort of thing as well.</p>

{{{id=101|
surface=list_plot3d(coordinates,interpolation_type='spline',num_points=40,point_list=True)
surface+pts
///
}}}

{{{id=109|

///
}}}

<p>If you plot a matrix, it is considered the z-coordinates of a grid of integer x,y coordinates, starting at the origin.</p>

{{{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
///
}}}

<h1 id="Builtin">Builtin Graphics Objects</h1>
<p>Sage includes a variety of built-in graphics objects. &nbsp;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. &nbsp;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.</p>
<h2>Points</h2>
<p>To make one point, a coordinate pair suffices.</p>

{{{id=171|
point((3,5))
///
<html><font color='black'><img src='cell://sage0.png'></font></html>
}}}

<p>It doesn't matter how multiples points are generated; they must go in as input via a list (square brackets). &nbsp;Here, we demonstrate the hard (but naive) and easy (but a little more sophisticated) way to do this.</p>

{{{id=173|
f(x)=x^2
points([(0,f(0)), (1,f(1)), (2,f(2)), (3,f(3)), (4,f(4))])
///
<html><font color='black'><img src='cell://sage0.png'></font></html>
}}}

{{{id=63|
points([(x,f(x)) for x in range(5)])
///
<html><font color='black'><img src='cell://sage0.png'></font></html>
}}}

<p>Sage tries to tell how many dimensions you are working in automatically.</p>

{{{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)])
///
}}}

<h2>Lines</h2>
<p>The syntax for lines is the same as that for points, but you get... well, you get connecting lines too!</p>

{{{id=83|
f(x)=x^2
line([(x,f(x)) for x in range(5)])
///
}}}

<p>3d lines are automatically smoothed.</p>

{{{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)])
///
}}}

<h2>Balls</h2>
<p>Sage has disks and spheres of various types available. &nbsp;Generally the center and radius are all that is needed, but other options are possible.</p>

{{{id=81|
circle((0,1),1, aspect_ratio=1)
///
<html><font color='black'><img src='cell://sage0.png'></font></html>
}}}

{{{id=139|
sphere((0,0,1), 1)
///
}}}

{{{id=176|
disk((0,0), 1, (pi, 3*pi/2), color='yellow',aspect_ratio=1)
///
<html><font color='black'><img src='cell://sage0.png'></font></html>
}}}

<h2>Arrows</h2>

{{{id=86|
arrow((0,0), (1,1))
///
<html><font color='black'><img src='cell://sage0.png'></font></html>
}}}

{{{id=140|
arrow3d((0,0,0), (1,1,1))
///
}}}

{{{id=144|

///
}}}

<h2>Polygons</h2>
<p>Polygons will try to complete themselves and fill in the interior; otherwise the syntax is fairly self-evident.</p>

{{{id=142|
polygon([[0,0],[1,1],[1,2]])
///
<html><font color='black'><img src='cell://sage0.png'></font></html>
}}}

{{{id=143|
polygon([[0,0,0],[1,1,2],[1,2,1]])
///
}}}

<h2>Text</h2>
<p>In 2d, one can typeset mathematics using the 'text' command. &nbsp;This can be used to finetune certain types of labels. &nbsp;Unfortunately, in 3D the text is just text.</p>

{{{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))
///
}}}

<h1 id="Saving">Saving Plots</h1>
<p>We can save 2d plots to many different formats.&nbsp; Sage can determine the format based on the filename for the image.</p>

{{{id=141|
p=plot(x^2,(x,-1,1))
p
///
<html><font color='black'><img src='cell://sage0.png'></font></html>
}}}

{{{id=152|
p.save('my_plot.png')
///
}}}

{{{id=153|
p.save('my_plot.pdf')
///
}}}

{{{id=155|
p.save('my_plot.svg')
///
}}}

<p>You can also save 3d plots to files.&nbsp; To do this, adjust the figure how you like, and click on "Get Image" next to the picture.&nbsp; Try saving a snapshot of the following 3d image to your desktop.</p>

{{{id=156|
var('x,y')
p=plot3d(x^2-y^2,(x,-2,2),(y,-2,2))
p
///
}}}

<p>You can view the 3d plot with a few other different backends too.&nbsp; 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.</p>

{{{id=161|
show(p,viewer='canvas3d')
///
}}}

<p>Here is a backend that uses a 3d ray-tracing engine to provide photo-realistic images.</p>

{{{id=158|
show(p,viewer='tachyon')
///
}}}

<p>If you try to save the 3d image using a code cell, Sage uses Tachyon.</p>

{{{id=159|
p.save('my_plot.png')
///
}}}

<p>You can do many other things with Tachyon.&nbsp; 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.</p>

{{{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|

///
}}}