Plotting with Sage
system:sage

<p>&nbsp;</p>
<hr />
<h1 style="text-align: center;"><span style="color: #000080;">Plotting with Sage</span></h1>
<p style="text-align: center;">Jose Guzman and Sooyun Kim</p>
<p style="text-align: center;">version 1.0.1</p>
<p style="text-align: center;">Freiburg University</p>
<p style="text-align: center;">April 18, 2009</p>
<p>Please send any suggestions or comments to  <a href="mailto:nin@neurohost.org">nin@neurohost.org</a></p>
<hr />
<h2>Index</h2>
<ol>
<li> <a href="#introduction"> Introduction </a></li>
<li><a href="#plot"> The plot() function </a></li>
<li><a href="#show"> The  function show()</a></li>
<li><a href="#additional"> Other type of plots<br /></a></li>
</ol>
<p><br /><a href="mailto:nin@neurohost.org"></a></p>
<hr />
<p><a name="introduction"></a></p>
<h2><strong>1. Introduction</strong></h2>
<p>This document describes briefly how to create 2D plots&nbsp; with Sage. It assumes a basic knowledge of the Sage notebook and a minimal object oriented programming experience, which is indeed not necessary to follow the explications here. The syntax employed is generally self explanatory, however,&nbsp; source code is fully commented. This worksheet was made as a personal use/reference, but you are free to copy and use this document for your own purposes.</p>
<p>We can use Sage to plot simple graphics the <strong>plot()</strong> and <strong>show()</strong> functions. These commands allow us to control just a few number of parameters, but this is generally enough for screening processes (i.e to see how a function looks like). To show the plotting properties of Sage, we will plot the following 2 equations $f(x)$&nbsp; and $g(x)$.</p>
<p style="text-align: center;">$ f(x) = 40 {e}^{(-\pi x)} \sin{(3 \pi x)} $</p>
<p style="text-align: center;">$g(x) = 40 {e}^{(-\pi x)} \cos{(3 \pi x)} $</p>
<p style="text-align: left;">And the function resulting of the subtraction $h(x)=f(x)-g(x)$</p>
<p style="text-align: left;">&nbsp;</p>

{{{id=2|
# we create symbolic mathematical functions (typ sage.calculus.calculus.SymbolicArithmetic)
f(x) = 40*e^(-pi*x)*sin(3*pi*x) # the function f(x)
g(x) = 40*e^(-pi*x)*cos(3*pi*x) # the function g(x)
h=(f-g) # difference between f(x) and g(x)
///
}}}

<p><a name="plot"></a></p>
<h2>2. The function plot()<br /></h2>
<p>The function plot will initiate an object of the type sage.plot.plot. Plot()&nbsp; The syntax of the plot command is:</p>
<p><strong><span style="color: #ff0000;">plot(f, xmin, xmax, options)</span></strong></p>
<ul>
<li>where <strong>f</strong> is a function or a list of functions (a 'sage.calculus.calculus.SymbolicArithmetic' object) or similar</li>
<li><strong>xmax,xmin&nbsp;</strong> the starting x value and end x value to plot. You can enter a&nbsp; python tuple of the form (xmin,xmax)</li>
<li><strong>options</strong> (rgbcolor=() for the color, you can use a tuple for rgb color or a text ('red','blue','orange',etc..)</li>
</ul>
<p>Only the argument <strong>f</strong> (the function object) is mandatory, the rest are optional. If the tuple <strong>(xmax,xmin) </strong>is not added, the values xmin=-1 and xmax=+1 are assumed.&nbsp; The rgbcolor parameter allows to enter directly the following string codes.</p>
<p>&nbsp;</p>
<pre>    "red"   : (1.0,0.0,0.0),<br />    "orange": (1.0,0.5,0.0),<br />    "yellow": (1.0,1.0,0.0),<br />    "green" : (0.0,1.0,0.0),<br />    "blue"  : (0.0,0.0,1.0),<br />    "purple": (0.5,0.0,1.0),<br />    "white" : (1.0,1.0,1.0),<br />    "black" : (0.0,0.0,0.0),<br />    "grey"  : (0.5,0.5,0.5)<br /><br /></pre>
<p>Then, if you want to plot the sin(x) function between 0 and 2$\pi$ in orange you have only to enter the following command:</p>

{{{id=17|
plot(sin(x),(0,2*pi),rgbcolor='orange')
///
}}}

<p>But we will create 3 objects (sage.plot.plot) to manipulate the plots differently with the show() funcion later</p>

{{{id=13|
fig1 = plot(g,0,5,rgbcolor=(0,0,1))   # fig1 is an object sage.plot.plot between 0 and 0.1 (blue)
fig2 = plot(f,0,5,rgbcolor='red')       # fig2 ... (red)
fig3 = plot(h,0,5,rgbcolor=(0.5,0.5,0))   # fig3 (dark green)
///
}}}

<p>We can have a look the to options of the function plot with the <strong>plot.options</strong> command. This simply shows the dictionary of the different plot() options and the default configuration if these are not indicated.</p>

{{{id=3|
plot.options
///
{'fillalpha': 0.5, 'plot_points': 200, 'thickness': 1, 'adaptive_tolerance': 0.01, 'fillcolor': 'automatic', 'alpha': 1, 'adaptive_recursion': 5, 'rgbcolor': (0, 0, 1), 'fill': None}
}}}

<p>Once we have the different objects is a good idea to combine all in a list to plot them later&nbsp; with the command <strong>show()</strong>.</p>

{{{id=4|
figure = fig1 + fig2 + fig3 # we add all the graphic objects in a list
///
}}}

<A NAME='show'></A>
<h2>3. The function show()</h2>
<p>Finally, we can show the figures with <strong>show()</strong>. This function takes an argument the object reated with plot()&nbsp; which has the following parameters:</p>
<p><strong><span style="color: #ff0000;">show(f, xmin, xmax, ymin, ymax, options)</span></strong></p>
<ul>
<li><strong>f </strong>is the list of object functions (or the single function) created with plot(), this is mandatory</li>
<li><strong>xmin</strong>, <strong>xmax</strong>, <strong>ymin</strong> and <strong>ymax</strong> are the limits of the plots (notice that the objects can be larger in the x/y axis, but for your porposes you may simply want to show only a given range of the function.</li>
<li><strong>options</strong> (like dpi=int, figsize=[], axes=True/False,fontsize=integer, frame=False/True)</li>
</ul>

{{{id=6|
# And finally we should show the plot object with given parameters
# we first set the axis to fontsize 14
figure.fontsize(14)

figure.show(xmin=0,xmax=1.5,ymin=-40,ymax=45,figsize=[8,4])# show() with some argument.
///
}}}

<p><a name="additional"></a></p>
<h2>4. Other type of plots</h2>
<p>We can combine different types of plots all together to represent a common graphic. Some different plot types are described here with its syntax:</p>
<ul>
<li><strong>line</strong><span style="color: #ff0000;">&nbsp;&nbsp; line([(xstart,ystart),(xend,yend)],options)<span style="color: #000000;">, line takes as argument a 2-element <em>list</em> of 2 tuples<br /></span></span></li>
<li><strong>polygon</strong> <span style="color: #ff0000;">polygon([[][][][]],options)</span>, polygon takes as argument a list with 3 or points (python list).</li>
<li><strong>circle</strong> <span style="color: #ff0000;">circle((xcenter,ycenter),radius,options<span style="color: #000000;">, <span style="color: #000000;">takes as argument the center (a tuple ) and the radius.</span></span></span><span style="color: #000000;"> </span></li>
<li><strong>text&nbsp;</strong> <span style="color: #ff0000;">text ('string',(x,y), options)<span style="color: #000000;">, text takes as arguments a <em>string</em> and a <em>tuple</em><br /></span></span></li>
<li><span style="color: #000000;"><strong>arrow</strong> </span><span style="color: #ff0000;">arrow((xstart,ystart),(xend,yend),options)<span style="color: #000000;">, arrow takes as argument a 2-element <em>tuple</em> of 2 tuples<br /></span></span></li>
</ul>
<p>&nbsp;</p>

{{{id=14|
#define and plot 2 custom functions f_(x) and g_(x)
f_(x) = 10 -x
g_(x) = x

plot_f = plot(f_(x),0,10,rgbcolor='purple')
plot_g = plot(g_(x),0,10,rgbcolor='red')

# we plot a line and an arrow
ln1  = line([(0,5),(10,5)],rgbcolor='gray',linestyle='--')
arw = arrow((5,5),(5,10),rgbcolor='gray')

# We print some labels
#pt = point((5, 5), rgbcolor='white', pointsize=40, faceted=True)
title = text('Intersection between two lines', (6,11), rgbcolor='black',fontsize=15)

# Polygon
plg = polygon([[12,10],[17,10],[17,7.5],[12,7.5]],rgbcolor='black',
thickness=1)

# Point
pt = point((5,5),rgbcolor='white',pointsize=30,faceted=True)

# Circle

cir = circle((15,5),2)
 
# combine graphic objects
mygraphic = (plot_f + plot_g + ln1 + arw + plg + cir + title + pt)

# set the size of the axis
mygraphic.fontsize(12)

mygraphic.show(frame=True, axes=False,xmin=0,xmax=20) # plot the objects
///
}}}

<p>We can use the fucntion plot_list to plot a list of numbers. The function list_plot() is indeed very similar to plot():</p>
<p><strong><span style="color: #ff0000;">list_plot(f, xmin, xmax, options)</span></strong></p>
<ul>
<li>where <strong>f</strong> a list of numbers</li>
<li><strong>options</strong> (rgbcolor=() for the color, you can use a tuple for rgb color or a text ('red','blue','orange',etc..)</li>
</ul>
<p>the argument f is a list of data, but it will form a list of tuples of the type (x,y), where x is the lenth of the list. In our example we create 80 random values (between 0 and 1) and i will be plotted from 0 to 80</p>
<p>&nbsp;</p>

{{{id=31|
# random values

import random

x=[] # create an empty li
#generate 80 float random numbers and append it to x
for i in range(80): x.append(random.random())

# list_plot takes x as argument and plot in red (1,0,0) with size=20)
list_plot(x,rgbcolor=(1,0,0),pointsize=20)
///
}}}

<p>To plot a list of x vs. y we have only to zip the 2 lists and put the result as argument for the list_plot() function</p>

{{{id=33|
x=srange(0, 4, 0.2)


y= srange(0,4,.2)

xy = zip(x,y)

list_plot(xy,pointsize=40,rgbcolor='white',faceted='True')
///
}}}

{{{id=34|
list_plot?
///
}}}

{{{id=35|

///
}}}