language: sage
---
category: Plotting / 2D
---
title: Basic Plot
code: |
print "Basic plot: you need a variable, and a function of the variable here x*sin( x^2 )"
# this defaults many many things you can control
# here in 2 steps using plot() and show() (which can be combined into one line):
pt = plot( x*sin(x^2), x ) # create plot
show( pt )
descr: Basic plot of a function, letting most options default.
---
title: Upper and lower limit
code: |
print "Basic plot extended with upper lower, limit on x"
pt = plot( x*sin(x^2), x, 0, 6*pi ) # args in order: function, variable, lower limit, upper limit,
show( pt )
descr: >
Basic plot, but using plot arguments to control upper and lower
limits on x.
---
title: Many more options/arguments
code: |
print "Plot with many more options/arguments, used by name that are somewhat self explanatory -- tweak values to see what they do, or google them"
# show also offers named arguments for control.
pt = plot( x*sin(x^2), x, 0, 6*pi, rgbcolor="red", linestyle = "-", fill=False, thickness=1, legend_label ="a legend label" )
show( pt, aspect_ratio = 1, axes=true, frame=True, gridlines=false, figsize=4, xmin=-0, xmax=8*pi, ymin=-6, ymax=6 )
descr: >
Plot showing arguments to both plot and show that allow a lot of control over
various aspects of the graph.
---
title: Multiple lines/function
code: |
print "Plot Multiple lines/function on plots"
# also note that functions may be computed and put into variables outside of plot() or show()
function1 = x*sin(x^2)
function2 = x^.5*sin(x)
pt1 = plot( function1, x, 0, 6*pi, rgbcolor="red", linestyle = "-", fill=False, thickness=1, legend_label ="functionl" )
pt2 = plot( function2, x, 0, 6*pi, rgbcolor="blue", linestyle = "--", fill=False, thickness=1, legend_label ="function2" )
show( pt1 + pt2, aspect_ratio = 1, axes=true, frame=True, gridlines=false, figsize=6, xmin=-0, xmax=8*pi, ymin=-8, ymax=8 )
descr: >
Multiple plots can be plotted on the same graph, optionally using different
line styles.
---
title: Custom tickmarks on the axes
code: |
print "Plot with ticks, LaTeX formatting at specific locations"
fx = 1 * (x^2)+1
pt = plot( fx ,x, 0,5, ticks=[[0,1,e,pi,sqrt(20)], 5 ], tick_formatter="latex")
show( pt, figsize=[3,3] )
descr: >
Tickmarks on the axes can be controlled both in position, spacing,
special values, and using LaTeX for values such as pi.
---
title: Using Python functions
code: |
print "Plot using Python functions defined using 'def:'"
# see http://www.sagemath.org/doc/tutorial/tour_functions.html
def f(z): # this and next line define a Python function
return z^2
print "Define plot and show it in one line of code"
show( plot(f, 0, 2), figsize=[6,2] )
print "Alternate syntax "
var( "z" )
show( plot(f(z), z, 0, 2), figsize=[6,2], )
descr: >
Plot using Python functions.
Python functions are defined using the keyword def.
---
category: plotting / 2D Advanced
---
title: Implicit Plot
code: |
print "Implicit plot ( here function of x, y not solved for y )"
var ('x y')
imp = exp(x)==y^2
implicit_plot(imp, (x,-2,2), (y,-3,3), linestyle="--", figsize=4, axes="true", aspect_ratio=1)
descr: >
Implicit plots are used where the relationship is implicit in an equation
rather than explicitly solved.
---
title: Parametric Plot
code: |
print "Parametric Plot ( here 3 functions)"
# for graphing parametric equations ( 2 functions with a common variable implying a relation between the 2 functions )
# 3 plots all controlled by the parameter t
var( "t" ) # our parameter
ll = -1. * pi # lower limit of graph
ul = 1. * pi # upper limit of graph
# parametric_plot( x axis function, y axis function ) , ( parametric variable, min value, max value ), other plot args )
p1 = parametric_plot((cos(t),sin(t)), (t, ll, ul), rgbcolor=hue(0.2))
p2 = parametric_plot((cos(t),sin(t)^4), (t, ll, ul), rgbcolor=hue(0.4))
p3 = parametric_plot((cos(t),sin(t)^8), (t, ll, ul), rgbcolor=hue(0.6))
show( p1+p2+p3, axes=false)
descr: >
Parametric plots are used where the relationship between variables is implied by
their relationship to a third variable, the parameter.
---
title: Polar Plot
code: |
print "Polar Plot"
# Plot using polar coordinates: note that many of the options for plot() work here.
var( "theta" )
f = 1-2*sin(2*theta); show( f )
pp = polar_plot( f, theta, 0, 2*pi, rgbcolor="red", linestyle = "--" )
show( pp )
descr: >
Polar plots are used where the relationship between x and y, in Cartesian
coordinates, is expressed in terms of r an theta, in polar coordinates.
---
title: Slope Field
code: |
print "Plot Slope Field"
var( "x" )
var( "y" )
slope = y ^ 2 # define the slope to be the value of the function y^2
pt = plot_slope_field( slope, ( x, -1, 1 ), ( y, .001, 10 ), plot_points = 10 ) # plot_points: higher numbers plot more "slopes"
show( pt, aspect_ratio='automatic' ) # aspect_ratio: try .1, 1, 10 'automatic' or leave out and let it default
descr: >
Slope fields show the slope of 2 variables defined over a "field", that is
a relationship between x, an y that extends over an area of the plane ( 2 D).
The slope is indicated by the slope of a short line segment.
---
title: ODE Slope Field
descr: |
This example shows, how the solution of the differential equation
$$\frac{dy}{dx} + \frac{y}{x} + x = 2$$
with the initial conditions $y(2) = 4$
looks like.
code: |
x = var('x')
y = function('yy')(x)
eq = diff(y, x) == - y/x - x + 2
h = desolve(eq, y, [2, 4])
sol(x) = h.expand()
print "solution:", sol
# substituting the solution for y(x) in the right hand side
substituted = eq.rhs().substitute_function(yy, sol)
P1 = plot_slope_field( substituted, (x, 0, 6), (y, -5, 15), headlength = 4, headaxislength=3 )
P2 = plot( sol, (x, 0, 6), ymax=15, ymin=-5 )
P = P1 + P2
P.show()
---
title: Vector Field
code: |
print "Plot vector field"
var( "x" )
var( "y" )
A = x - sin(y)
B = -y * cos(x)
field = (A,B)
H = plot_vector_field( field, (x,0,2*pi), (y,0,2*pi), plot_points=15, color='green' ) # plot_points: higher numbers plot more vectors
show( H )
descr: >
Plot of a vector field shows an arrow of variable length indicating the
direction an magnitude of the vector at a large number of points over
the area of the graph.
---
title: List of points
code: |
print "Plot of a list of points"
# either type of list works
dictionary_list = {22: 3365, 27: 3295, 37: 3135, 42: 3020, 47: 2880, 52: 2735, 57: 2550}
tuple_list = [ ( 22, 3365) , ( 27, 3295 ), ( 37, 3135 ), ( 42, 3020 ), ( 47, 2880 ), ( 52, 2735 ), (57, 2550 ) ]
listPlot = list_plot( dictionary_list )
show( listPlot, xmin=0, xmax =50, ymin=2000, ymax=4000, figsize=[4,4] )
descr: >
Points, contained int Python lists, are all plotted.
The lists may be dictionary lists, or lists of 2D tuples.
---
title: Scatter Plot with Line
code: |
print "Scatter plot and line" # http://www.packtpub.com/article/plotting-data-sage
# python function
def noisy_line(m, b, x):
ret = m * x + b + 0.5 * (random() - 0.5)
return ret
slope = 1.0
intercept = -0.5
x_coords = [random() for t in range(50)]
y_coords = [noisy_line(slope, intercept, x) for x in x_coords]
sp = scatter_plot(zip(x_coords, y_coords))
sp += line([(0.0, intercept), (1.0, slope+intercept)], color='red')
sp.show( figsize=[6,2])
descr: >
Plot points, x coords in one list, y coords in another.
---
title: Contour Plot
code: |
print "Contour plot"
# this might be improved, some explanation
f = lambda x,y: cos(x*y)
show ( contour_plot(f, (-4, 4), (-4, 4)) )
descr: >
This plot indicates the contour of a function using
50 shades of gray.
---
category: plotting / Basic Elements
---
title: Points as points (plain and fancy)
code: |
print "Plotting points as points ( plain and fancy )"
plainPoints = point( [1, 1] ) + point( [2, 1] )
plot1 = plot( plainPoints )
show( plot1, figsize=4 )
fancyPoints = point((0.5, 0.5), rgbcolor=(1, 0, 0), size=30)
fancyPoints = fancyPoints + point((0.7, 0.7), rgbcolor=(0, 1, 0), size=50)
show( fancyPoints , figsize=4 )
descr: >
The graphic object point may be plotted using plot()
Plotting this way may combine various elements, both those
in this category but also other 2D plots.
---
title: Line with point at the end
code: |
print "Plot a line with point ( dot ) at end"
# computed in polar form, not required
r = 5
t = pi/4.
x = r*cos( t )
y = r*sin( t )
aPoint = point( ( x, y ), rgbcolor="red", size=30 )
aLine = line([(0.,0.), ( x, y ) ] , rgbcolor="blue" , linestyle = "dashed" )
plot = aPoint + aLine
show( plot, xmin=-1.2, xmax=5.2, ymin=-1.2, ymax=5.2 , figsize=[3,3] )
descr: >
A line is plotted with a point at the end.
---
title: Lines and points with Python function
code: |
Print "Plot lines and points with Python function"
def unitRotator( theta ):
x = cos( theta )
y = sin( theta )
aPoint = point2d( ( x, y ), rgbcolor="red", size=30 ) #point2d?
aLine = line([(0.,0.), ( x, y ) ] )
myRotator = aPoint +aLine
return myRotator
graphic = point( (0, 0 ) )
for theta in srange( .0, 2*pi, 2*pi/10):
graphic = graphic + unitRotator( theta )
show( graphic , xmin=-1.2, xmax=1.2, ymin=-1.2, ymax=1.2 , figsize=[3,3] )
descr: >
A Python function defines many lines and points all of which are plotted.
---
title: Arc
code: |
print "Plot an Arc"
graphic = ( arc((.1,.2), 1, sector=(0, 1*pi /4) ) )
show( graphic , xmin=-1.2, xmax=1.2, ymin=-1.2, ymax=1.2, figsize=[3,3] )
descr: >
Plot an a arc, a segment of a circle. Center, radius and section of
arc are all parameters.
---
title: Circle
code: |
print "Plot a Circle"
anGraphic = circle((0,0), .5, rgbcolor=(1,0,0), fill=False ) # fill controls filling of circle with color
show( anGraphic, aspect_ratio=1, figsize = 4 ,xmax = 1, ymax = 1)
descr: Plot a circle. Center, radius, color are parameters.
---
title: Arrow
code: |
print "Plot an Arrow"
anGraphic = arrow((0,1), (2,3) ) # start cord, end cord as tuples
show( anGraphic, figsize = 2 )
descr: Plot an Arrow, describe by starting and ending coordinates.
---
title: Text
code: |
print "Plot Text"
# text is followed by tuple for position
aGraphic = text("Sample Text", (5,4), rgbcolor=(1,0,0))
show( aGraphic, aspect_ratio=1, figsize = 3 )
descr: >
Plot Text, arguments for text, position, and color.