2D Plotting

From www.norsemathology.org

Jump to: navigation, search


2D Plotting in Sage

2D Plotting in Sage consists primarily with the following functions.

  • plot
  • contour_plot
  • implicit_plot
  • parametric_plot
  • polar_plot

The plot Function

The plot function plots one or more functions of one variable. The basic syntax looks like:

plot( function, (xmin,xmax), keyword_arguments )

Simple plot of one function

Plotting a single function with plot is very simple:

plot( sin(x), (-2*pi,2*pi) )

Image: Sage_sin_plot.png

Plotting multiple functions with the same end points

Plotting multiple functions that share the same xmin and xmax is done by passing in a list of functions instead of just one function.

Plotting f(x) = x and g(x) = x2

plot( [x,x^2], (-4,4) )

Image: Sage_2f_plot.png

Plotting multiple functions with different end points

Plotting multiple functions with different end points is done by creating several plots which are stored in variables then plotted as a list of plots.

Plotting f(x) = x where -10 \leq x \leq 10 and g(x) = x2 where -4 \leq x \leq 4

f = plot( x, (-10,10) )
g = plot( x, (-4,4) )
plot( [f,g] )

Image: Sage_multiple_end_points.png‎

Customizing Plots

Changing Line Colors

By default all lines drawn by plot in Sage are drawn in blue. If you want to plot the line in a different color this is done by using the keyword parameter rgbcolor. The rgbcolor parameter takes a tuple consisting of the color's rgb values where each value is between 0 and 1.

Plotting f(x) = sin(x) in red and g(x) = cos(x) in green.

sinPlot = plot( sin(x), (-2*pi,2*pi), rgbcolor=(1,0,0) )
cosPlot = plot( cos(x), (-2*pi,2*pi), rgbcolor=(0,1,0) )
plot( [ sinPlot, cosPlot ] )

Image: Sage_plot_colors.png

Changing Line Style

Sage allows you to change the style of the line drawn in a plot. By default lines are drawn as solid lines, to change this you use the keyword parameter linestyle. linestyle takes a one of several strings used to specify the style of the line. The available options are:

  • '--' Dashed
  • '-.' Dash Dot
  • '-' Solid
  • ':' Dotted

Plotting f(x) = x2 with a dashed line and g(x) = x with a dotted line.

f = plot( x^2, (-4,4), linestyle='--' )
g = plot( x, (-8,8), linestyle=':' )
plot( [f,g] )

Image: Sage_line_styles.png

Other Customization Options

The following keyword parameters are also available to customize the look of the plot:

  • thickness: Default is 1, specifies the thickness of the line
  • alpha: Default is 1 ( fully opaque ), the closer to 0 the more transparent the line is.

Other Options

Plot provides several other options:

  • plot_points: Default is 200, specifies the number of points Sage plots before performing adaptive refinement.
  • adaptive_recursion: Default is 5, specifies how many levels deep the recursion should go when Sage performs adaptive refinement.
  • adaptive_tolerance: Default is 0.01, specifies how much change is necessary for adaptive refinement to consider it significant.

Changing Plot Defaults

Sage allows you to change the default values for several of the plot parameters. The following parameters can have there default values changed:

  • adaptive_tolerance
  • adaptive_recursion
  • plot_points
  • rgbcolor
  • thickness
  • alpha

The default values are stored in the dictionary plot.options. Changing the values is done the same way that changing dictionary values is typically done.

Example: Changing the default line color to red.

plot.options['rgbcolor'] = (1,0,0)

After that command is entered all future plots will be drawn in red by default.

To restore the original defaults call the function: plot.reset() and the default values will be restored.

Note: Changing the defaults applies only to the current worksheet, so changing the defaults in one worksheet will not affect other worksheets.

Contour Plot

The contour_plot function plots a function of two variables of a xrange and yrange.

The basic syntax is: contour_plot( function,(xmin,xmax),(ymin,ymax) )

Plotting f(x,y) = sin(x) + cos(y) where -10 \leq x \leq 10 and -10 \leq y \leq 10

var('x y')
contour_plot( sin(x) + cos(y), (-10,10),(-10,10) )

Image: Sage_contour_plot.png

Setting the Contours

The contours can be set using the contours keyword parameter. The contours parameter takes a list of the contours. One thing to note is that by default contour_plot sets fill to true and contour_plot requires at least 2 contours if fill is set to True. So if you only want 1 contour you need to set fill to False.

Plotting f(x,y) = x2 + y2 with the contours set to [4].

var('x y')
contour_plot( x^2 + y^2, (-3,3), (-3,3), contours=[4], fill=False )

Image: Sage_contour_no_fill.png

Customizing the Colors

The contour_plot function provides a variety of color map options the default is 'gray'. The color map is specified using the cmap keyword parameter and passing in a string representing one of the color maps. For a complete list of the available color maps type contour_plot? into a Sage cell.

Plotting f(x,y) = sin(x) + cos(y) with the color map 'winter'

var('x y')
contour_plot( sin(x) + cos(y), (-10,10),(-10,10), cmap='winter' )

Image: Sage_contour_winter.png

Other Options

Like the plot function contour_plot also provides a plot_points keyword parameter, in contour_plot plot_points defaults to 25. Increasing the value of plot_points produces smoother lines, typically a value of 100 will produce a smooth graph.

Compare our previous plot of f(x,y) = sin(x) + cos(y) with this one with plot_points set to 100.

var('x y')
contour_plot( sin(x) + cos(y), (-10,10),(-10,10), cmap='winter', plot_points=100 )

Image: Sage_contour_winter_smooth.png

Implicit Plot

The implicit_plot function is a convenience function that behind the scenes calls contour_plot with contours set to [0] and fill set to False. The syntax works just like the syntax to contour_plot except the only keyword used is plot_points.

Note: a bug was introduced in Sage version 3.1.4 that caused implicit_plot to set fill to True by default. So if you're using Sage 3.1.4 you can still pass fill as a keyword parameter and set it to False.

Plotting f(x,y) = x3 - x - y2

var('x y')
implicit_plot( x^3 - x - y^2, (-3,3),(-3,3), plot_points=100 )

Image: Sage_bultin_implicit.png

Parametric Plot

The parametric_plot function takes a list or tuple of two functions the first giving the x coordinates and the second giving the y coordinates.

The syntax looks like: parametric_plot( [f1,f2], tmin, tmax )

Here is an example plot the draws a line with a loop using the following equations:

x = {40 {t}^{3} } + {{12 \left( 1 - t \right)} {t}^{2} } + {{300 {\left( 1 - t \right)}^{2} } t} + {4 {\left( 1 - t \right)}^{3} }

 y = {5 {t}^{3} } + {{300 \left( 1 - t \right)} {t}^{2} } + {{300 {\left( 1 - t \right)}^{2} } t} + {\left( 1 - t \right)}^{3}

x = 4 * (1-t)^3 + 300*t*(1-t)^2 + 12*t^2*(1-t) + 40*t^3
y = (1-t)^3 + 300*t*(1-t)^2 + 300*t^2*(1-t) + 5*t^3
parametric_plot( (x,y), 0, 1 )

Image: Parametric_loop.png

Combining Parametric Plots

Just like with regular plots, parametric plots can be combined together.

Here we can use two sets of equation to draw the letter s:

First Set of Equations:

x = {25 {t}^{3} } + {{15 \left( 1 - t \right)} {t}^{2} } + {{12 {\left( 1 - t \right)}^{2} } t} + {50 {\left( 1 - t \right)}^{3} }

y = {15 {t}^{3} } + {{75 \left( 1 - t \right)} {t}^{2} } + {{180 {\left( 1 - t \right)}^{2} } t} + {55 {\left( 1 - t \right)}^{3} }

Second Set of Equations:

x = {4 {t}^{3} } + {{210 \left( 1 - t \right)} {t}^{2} } + {{92 {\left( 1 - t \right)}^{2} } t} + {25 {\left( 1 - t \right)}^{3} }

y = {-25 {t}^{3} } - {{72 \left( 1 - t \right)} {t}^{2} } + {{36 {\left( 1 - t \right)}^{2} } t} + {15 {\left( 1 - t \right)}^{3} }

x1 = 50 * (1-t)^3 + 12*t*(1-t)^2 + 15*t^2*(1-t) + 25*t^3
y1 = 55 * (1-t)^3 + 180*t*(1-t)^2 + 75*t^2*(1-t) + 15*t^3
p1 = parametric_plot( (x1,y1),0,1 )
x2 = 25 * (1-t)^3 + 92*t*(1-t)^2 + 210*t^2*(1-t) + 4*t^3
y2 = 15 * (1-t)^3 + 36*t*(1-t)^2 - 72*t^2*(1-t) - 25*t^3
p2 = parametric_plot( (x2,y2),0,1 )
plot( [p1,p2] )

Image: Sage_parametric_s.png

Other Options

The parametric_plot function provides the same keyword parameters that the plot function provides. So the parameters

  • rgbcolor
  • thickness
  • linestyle
  • adaptive_recursion
  • adaptive_tolerance
  • alpha

are all available to parametric_plot.

Polar Plot


The show function

All plots are Graphics objects in Sage and as such they all have a show method available. The show method allows additional customizations to the appearance of the plot.

Here are some of the main keyword parameters available:

  • dpi - dots per inch for the image
  • figsize - image dimensions in pixels inputed as a list [width,height]
  • axes - boolean flag to turn the axes on or off
  • axes_labels - list or tuple of two strings, the first string is the label for the x axis and the second is for the y axis.
  • fontsize - font size for the axes_labels
  • frame - boolean flag to set if a frame should be drawn around the image
  • gridlines - If passed True grid lines are drawn at the major tick marks, if passed 'minor' grid lines are drawn at the major and minor axes

Here's an example of turning on the frame and the gridlines:

plot( x^2, (-5,5), rgbcolor=(0,1,0) ).show( gridlines=True, frame=True )

Image: Sage_plot_gridlines.png

Note: Axes labels to do not appear to show up if frame is set to True

Other Plots

Matrix Plot

Sage provides a function called matrix_plot which plots a m x n matrix. The plot is divided up into a grid where the horizontal axis represents the column number and the vertical axis represents the row number. The color of each element is based on it's size compared to the other elements in the matrix.

The colors used depend upon the color map specified by the cmap parameter.

Simple example:


Here is the plot generated:

Image: Sage_matrix_plot.png

Vector Field Plot

Sage provides a function called plot_vector_field that plots vector arrows of two functions of two variables over a specified range.

The basic syntax looks like this:

plot_vector_field( (func1,func2), (var1,min,max),(var2,min,max) )

Simple Example:

var('x y')
plot_vector_field((x^2, y^3), (x,-5,5), (y,-5,5))

Here is the plot generated:

Image: Sage_vector_field.png

List Plot

Sage provides a function list_plot which can plot either a list of values or a list of tuples.

If given a list of values list_plot plots using the value as the y coordinate and the position in the list as the x coordinate.

If given a list of tuples list_plot plots using the first value of the tuple as the x value and the second value of the tuple as the y value.


  • plotjoined: True or False (Default: False) If set to True a line is drawn through all the points.

list_plot also accepts the same keyword parameters as plot such as rgbcolor.


list_plot([ sin(x) for x in [0..2*pi,step=0.1] ] )

Here is the plot generated:

Image: Sage_sin_list_plot.png

Personal tools