5.1 2D Plotting

Module: sage.plot.plot

2D Plotting

Sage provides extensive 2D plotting functionality. The underlying rendering is done using the matplotlib Python library.

The following graphics primitives are supported:

The following plotting functions are supported:

The following miscellaneous Graphics functions are included:

Type ? after each primitive in Sage for help and examples.

We construct a plot involving several graphics objects:

sage: G = plot(cos, -5, 5, thickness=5, rgbcolor=(0.5,1,0.5))
sage: P = polygon([[1,2], [5,6], [5,0]], rgbcolor=(1,0,0))
sage: P   # show it

We draw a circle and a curve:

sage: circle((1,1), 1) + plot(x^2, (0,5))

Notice that the above circle is not round, because the aspect ratio of the coordinate system is not 1:1. The aspect_ratio option to show allows us to fix this:

sage: show(circle((1,1), 1) + plot(x^2, (0,5)), aspect_ratio=1)

With an aspect ratio of 2 the circle is squashed half way down (it looks twice as wide as it does tall):

sage: show(circle((1,1), 1) + plot(x^2, (0,5)), aspect_ratio=2)

Use figsize to set the actual aspect ratio of the rendered image (i.e., of the frame). For example, this image is twice as many pixels wide as it is tall:

sage: show(circle((1,1), 1) + plot(x^2, (0,5)), figsize=[8,4])

Next we construct the reflection of the above polygon about the $ y$ -axis by iterating over the list of first-coordinates of the first graphic element of $ P$ (which is the actual Polygon; note that $ P$ is a Graphics object, which consists of a single polygon):

sage: Q = polygon([(-x,y) for x,y in P[0]], rgbcolor=(0,0,1))
sage: Q   # show it

We combine together different graphics objects using ``+'':

sage: H = G + P + Q
sage: print H
Graphics object consisting of 3 graphics primitives
sage: type(H)
<class 'sage.plot.plot.Graphics'>
sage: H[1]
Polygon defined by 3 points
sage: list(H[1])
[(1.0, 2.0), (5.0, 6.0), (5.0, 0.0)]
sage: H       # show it

We can put text in a graph:

sage: L = [[cos(pi*i/100)^3,sin(pi*i/100)] for i in range(200)]
sage: p = line(L, rgbcolor=(1/4,1/8,3/4))
sage: t = text('A Bulb', (1.5, 0.25))
sage: x = text('x axis', (1.5,-0.2))
sage: y = text('y axis', (0.4,0.9))
sage: g = p+t+x+y
sage: g.show(xmin=-1.5, xmax=2, ymin=-1, ymax=1)

We plot the Riemann zeta function along the critical line and see the first few zeros:

sage: i = CDF.0      # define i this way for maximum speed.
sage: p1 = plot(lambda t: arg(zeta(0.5+t*i)), 1,27,rgbcolor=(0.8,0,0))
sage: p2 = plot(lambda t: abs(zeta(0.5+t*i)), 1,27,rgbcolor=hue(0.7))
sage: print p1 + p2
Graphics object consisting of 2 graphics primitives
sage: p1 + p2    # display it

Many concentric circles shrinking toward the origin:

sage: show(sum(circle((i,0), i, hue=sin(i/10)) for i in [10,9.9,..,0]), aspect_ratio=1)

Here is a pretty graph:

sage: g = Graphics()
sage: for i in range(60):
...    p = polygon([(i*cos(i),i*sin(i)), (0,i), (i,0)],\
...                rgbcolor=hue(i/40+0.4), alpha=0.2)
...    g = g + p
...
sage: g.show(dpi=200, axes=False)

Another graph:

sage: x = var('x')
sage: P = plot(sin(x)/x, -4,4, rgbcolor=(0,0,1)) + \
...       plot(x*cos(x), -4,4, rgbcolor=(1,0,0)) + \
...       plot(tan(x),-4,4,rgbcolor=(0,1,0))
...
sage: P.show(ymin=-pi,ymax=pi)

PYX EXAMPLES: These are some examples of plots similar to some of the plots in the PyX (http://pyx.sourceforge.net) documentation:

Symbolline:

sage: y(x) = x*sin(x^2)
sage: v = [(x, y(x)) for x in [-3,-2.95,..,3]]
sage: show(points(v, rgbcolor=(0.2,0.6, 0.1), pointsize=30) + plot(spline(v), -3.1, 3))

Cycliclink:

sage: x = var('x')
sage: g1 = plot(cos(20*x)*exp(-2*x), 0, 1)
sage: g2 = plot(2*exp(-30*x) - exp(-3*x), 0, 1)
sage: show(graphics_array([g1, g2], 2, 1), xmin=0)

Pi Axis: In the PyX manual, the point of this example is to show labeling the X-axis using rational multiples of Pi. Sage currently has no support for controlling how the ticks on the x and y axes are labeled, so this is really a bad example:

sage: g1 = plot(sin(x), 0, 2*pi)
sage: g2 = plot(cos(x), 0, 2*pi, linestyle = "--")
sage: g1 + g2    # show their sum

An illustration of integration:

sage: f = (x-3)*(x-5)*(x-7)+40
sage: P = line([(2,0),(2,f(2))], rgbcolor=(0,0,0))
sage: P += line([(8,0),(8,f(8))], rgbcolor=(0,0,0))
sage: P += polygon([(2,0),(2,f(2))] + [(x, f(x)) for x in [2,2.1,..,8]] + [(8,0),(2,0)],  rgbcolor=(0.8,0.8,0.8))
sage: P += text("$\\int_{a}^b f(x) dx$", (5, 20), fontsize=16, rgbcolor=(0,0,0))
sage: P += plot(f, 1, 8.5, thickness=3)
sage: P    # show the result

NUMERICAL PLOTTING:

Sage also provides 2D plotting with an interface that is a likely very familiar to people doing numerical computation. For example,

sage: from pylab import *
sage: t = arange(0.0, 2.0, 0.01)
sage: s = sin(2*pi*t)
sage: P = plot(t, s, linewidth=1.0)
sage: xl = xlabel('time (s)')
sage: yl = ylabel('voltage (mV)')
sage: t = title('About as simple as it gets, folks')
sage: grid(True)
sage: savefig('sage.png')

Since the above overwrites many Sage plotting functions, we reset the state of Sage, so that the examples below work!

sage: reset()

See http://matplotlib.sourceforge.net for complete documentation about how to use Matplotlib.

TESTS: We test dumping and loading a plot.

sage: p = plot(sin(x), (x, 0,2*pi))
sage: Q = loads(dumps(p))

Author Log:

Module-level Functions

adaptive_refinement( f, p1, p2, [adaptive_tolerance=0.01], [adaptive_recursion=5], [level=0])

The adaptive refinement algorithm for plotting a function f. See the docstring for plot or PlotFactory for a description of the algorithm.

Input:

f
- a function of one variable
p1, p2
- two points to refine between
adaptive_recursion
- (default: 5) how many levels of recursion to go before giving up when doing adaptive refinement. Setting this to 0 disables adaptive refinement.
adaptive_tolerance
- (default: 0.01) how large a difference should be before the adaptive refinement code considers it significant. See the documentation for plot() for more information.

Output:
list
- a list of points to insert between p1 and p2 to get a better linear approximation between them

TESTS:

sage: from sage.plot.plot import adaptive_refinement
sage: adaptive_refinement(sin, (0,0), (pi,0), adaptive_tolerance=0.01, adaptive_recursion=0)
[]
sage: adaptive_refinement(sin, (0,0), (pi,0), adaptive_tolerance=0.01)
[(0.125000000000000*pi, 0.38268343236508978), (0.187500000000000*pi,
0.55557023301960218), (0.250000000000000*pi, 0.707106781186547...),
(0.312500000000000*pi, 0.831469612302545...), (0.375000000000000*pi,
0.92387953251128674), (0.437500000000000*pi, 0.98078528040323043),
(0.500000000000000*pi, 1.0), (0.562500000000000*pi, 0.98078528040323043),
(0.625000000000000*pi, 0.92387953251128674), (0.687500000000000*pi,
0.831469612302545...), (0.750000000000000*pi, 0.70710678118654757),
(0.812500000000000*pi, 0.55557023301960218), (0.875000000000000*pi,
0.3826834323650898...)]

This shows that lowering adaptive_tolerance and raising adaptive_recursion both increase the number of subdivision points:

sage: x = var('x')
sage: f = sin(1/x)
sage: n1 = len(adaptive_refinement(f, (0,0), (pi,0), adaptive_tolerance=0.01)); n1
15
sage: n2 = len(adaptive_refinement(f, (0,0), (pi,0), adaptive_recursion=10, adaptive_tolerance=0.01)); n2
79
sage: n3 = len(adaptive_refinement(f, (0,0), (pi,0), adaptive_tolerance=0.001)); n3
26

adjust_figsize_for_aspect_ratio( figsize, aspect_ratio, xmin, xmax, ymin, ymax)

Adjust the figsize in case the user also specifies an aspect ratio.

INPUTS: figsize - a sequence of two positive real numbers aspect_ratio - a positive real number xmin, xmax, ymin, ymax - real numbers

This function is used mainly internally by plotting code so we explicitly import it:

sage: from sage.plot.plot import adjust_figsize_for_aspect_ratio

This returns (5,5), since the requested aspect ratio is 1 and the x and y ranges are the same, so that's the right size rendered image to produce a 1:1 ratio internally. 5 is used instead of 3 since the image size is always adjusted to the larger of the figsize dimensions.

sage: adjust_figsize_for_aspect_ratio([3,5], 1, 0, 2, 0, 2)
(5, 5)

Here we give a scalar figsize, which is automatically converted to the figsize (figsize, figsize/golden_ratio).

sage: adjust_figsize_for_aspect_ratio(3, 1, 0, 2, 0, 2)
(3, 3)

Here we omit the aspect ratio so the figsize is just returned.

sage: adjust_figsize_for_aspect_ratio([5,6], None, 0, 2, 0, 2)
[5, 6]

Here we have an aspect ratio of 2, and since the x and y ranges are the same the returned figsize is twice as wide as tall:

sage: adjust_figsize_for_aspect_ratio([3,5], 2, 0, 2, 0, 2)
(5, 5/2)

Here the x range is rather large, so to get an aspect ratio where circles look twice as wide as they are tall, we have to shrink the y size of the image.

sage: adjust_figsize_for_aspect_ratio([3,5], 2, 0, 10, 0, 2)
(5, 1/2)

arrow( tailpoint, headpoint)

An arrow from (xmin, ymin) to (xmax, ymax).

INPUT width - (default 2) the width of the arrow shaft, in points rgbcolor - (default (0,0,1)) the color of the arrow (as an rgb tuple) hue - the color of the arrow (as a number) arrowsize - the size of the arrowhead arrowshorten - the length in points to shorten the arrow

A straight, blue arrow

sage: arrow((1, 1), (3, 3))

Make a red arrow:

sage: arrow((-1, -1), (2, 3), rgbcolor=(1,0,0))

You can change the width of an arrow:

sage: arrow((1, 1), (3, 3), width=5, arrowsize=15)

A pretty circle of arrows:

sage: sum([arrow((0,0), (cos(x),sin(x)), hue=x/(2*pi)) for x in [0..2*pi,step=0.1]]).show(aspect_ratio=1)

If we want to draw the arrow between objects, for example, the boundaries of two lines, we can use the arrowshorten option to make the arrow shorter by a certain number of points.

sage: line([(0,0),(1,0)],thickness=10)+line([(0,1),(1,1)], thickness=10)+arrow((0.5,0),(0.5,1), arrowshorten=10,rgbcolor=(1,0,0))

TESTS: We check to make sure the x/y min/max data is correct:

sage: a = arrow((1,1), (5,5))
sage: a.xmin()
1.0
sage: a.xmax()
5.0

bar_chart( datalist)

A bar chart of (currently) one list of numerical data. Support for more datalists in progress.

A bar_chart with blue bars:

sage: bar_chart([1,2,3,4])

A bar_chart with thinner bars:

sage: bar_chart([x^2 for x in range(1,20)], width=0.2)

A bar_chart with negative values and red bars:

sage: bar_chart([-3,5,-6,11], rgbcolor=(1,0,0))

circle( point, radius)

Return a circle at a point = $ (x,y)$ with radius = $ r$ . Type circle.options to see all options

circle(center, radius, **kwds)

Input:

center
- a 2-tuple (x,y)
radius
- a positive number
alpha
- default: 1
fill
- default: False
thickness
- default: 1
rgbcolor
- default: (0,0,0)

sage: c = circle((1,1), 1, rgbcolor=(1,0,0))
sage: c

To correct the apect ratio of certain graphics, it is necessary to show with a `figsize' of square dimensions.

sage: c.show(figsize=[5,5],xmin=-1,xmax=3,ymin=-1,ymax=3)

Here we make an more complicated plot with many circles of different colors

sage: g = Graphics()
sage: step=6; ocur=1/5; paths=16;
sage: PI = math.pi    # numerical for speed -- fine for graphics
sage: for r in range(1,paths+1):
...       for x,y in [((r+ocur)*math.cos(n), (r+ocur)*math.sin(n)) for n in
srange(0, 2*PI+PI/step, PI/step)]:
...           g += circle((x,y), ocur, rgbcolor=hue(r/paths))
...       rnext = (r+1)^2
...       ocur = (rnext-r)-ocur
...
sage: g.show(xmin=-(paths+1)^2, xmax=(paths+1)^2, ymin=-(paths+1)^2, ymax=(paths+1)^2, figsize=[6,6])

TESTS: We test to make sure the x/y min/max data is set correctly.

sage: p = circle((3, 3), 1)
sage: p.xmin()
2.0
sage: p.ymin()
2.0

contour_plot( f, xrange, yrange)

contour_plot takes a function of two variables, $ f(x,y)$ and plots contour lines of the function over the specified xrange and yrange as demonstrated below.

contour_plot(f, (xmin, xmax), (ymin, ymax), ...)

Input:

f
- a function of two variables
(xmin, xmax)
- 2-tuple, the range of x values OR 3-tuple (x,xmin,xmax)
(ymin, ymax)
- 2-tuple, the range of y values OR 3-tuple (y,ymin,ymax) The following inputs must all be passed in as named parameters:
plot_points
- integer (default: 25); number of points to plot in each direction of the grid
fill
- bool (default: True), whether to color in the area between contour lines
cmap
- string (default: 'gray'), the color map to use: autumn, bone, cool, copper, gray, hot, hsv, jet, pink, prism, spring, summer, winter
contours
- integer or list of numbers (default: None): If a list of numbers is given, then this specifies the contour levels to use. If an integer is given, then this many contour lines are used, but the exact levels are determined automatically. If None is passed (or the option is not given), then the number of contour lines is determined automatically, and is usually about 5.

Here we plot a simple function of two variables:

sage: x,y = var('x,y')
sage: contour_plot(cos(x^2+y^2), (-4, 4), (-4, 4))

Here we change the ranges and add some options:

sage: contour_plot((x^2)*cos(x*y), (-10, 5), (-5, 5), fill=False, plot_points=100)

An even more complicated plot.

sage: contour_plot(sin(x^2 + y^2)*cos(x)*sin(y), (-4, 4), (-4, 4),plot_points=100)

Some elliptic curves, but with symbolic endpoints. In the first example, the plot is rotated 90 degrees because we switch the variables x,y.

sage: contour_plot(y^2 + 1 - x^3 - x, (y,-pi,pi), (x,-pi,pi))
sage: contour_plot(y^2 + 1 - x^3 - x, (-pi,pi), (-pi,pi))

We can play with the contour levels.

sage: f = x^2 + y^2
sage: contour_plot(f, (-2, 2), (-2, 2))
sage: contour_plot(f, (-2, 2), (-2, 2), contours=2)
sage: contour_plot(f, (-2, 2), (-2, 2), contours=(0.1, 1.0, 1.2, 1.4), cmap='hsv')
sage: contour_plot(f, (-2, 2), (-2, 2), contours=(1.0,), fill=False)

TESTS: We test to make sure that the x/y min/max data is set correctly.

sage: p = contour_plot(f, (3, 6), (3, 6))
sage: p.xmin()
3.0
sage: p.ymin()
3.0

disk( point, radius, angle)

A disk at a point = $ (x,y)$ with radius = $ r$ spanning (in radians) angle= $ (rad1, rad2)$ Type disk.options to see all options

Make some dangerous disks:

sage: bl = disk((0.0,0.0), 1, (pi, 3*pi/2), rgbcolor=(1,1,0))
sage: tr = disk((0.0,0.0), 1, (0, pi/2), rgbcolor=(1,1,0))
sage: tl = disk((0.0,0.0), 1, (pi/2, pi), rgbcolor=(0,0,0))
sage: br = disk((0.0,0.0), 1, (3*pi/2, 2*pi), rgbcolor=(0,0,0))
sage: P  = tl+tr+bl+br
sage: P.show(figsize=(4,4),xmin=-2,xmax=2,ymin=-2,ymax=2)

TESTS: Check to make sure that the x/y min/max data is correctly set.

sage: d = disk((5,5), 1, (pi/2, pi), rgbcolor=(0,0,0))
sage: d.xmin()
4.0
sage: d.ymin()
4.0
sage: d.xmax()
6.0

float_to_html( r, g, b)

This is a function to present tuples of RGB floats as HTML-happy hex for matplotlib. This may not seem necessary, but there are some odd cases where matplotlib is just plain schizophrenic-for an example, do

sage: vertex_colors = {(1.0, 0.8571428571428571, 0.0): [4, 5, 6], (0.28571428571428559, 0.0, 1.0): [14, 15, 16], (1.0, 0.0, 0.0): [0, 1, 2, 3], (0.0, 0.57142857142857162, 1.0): [12, 13], (1.0, 0.0, 0.85714285714285676): [17, 18, 19], (0.0, 1.0, 0.57142857142857162): [10, 11], (0.28571428571428581, 1.0, 0.0): [7, 8, 9]}
sage: graphs.DodecahedralGraph().show(vertex_colors=vertex_colors)

Notice how the colors don't respect the partition at all.....

graphics_array( array, [n=None], [m=None])

graphics_array take a list of lists (or tuples) of graphics objects and plots them all on one canvas (single plot).

Input:

array
- a list of lists or tuples
n, m
- (optional) integers - if n and m are given then the input array is flattened and turned into an n x m array, with blank graphics objects padded at the end, if necessary.

Make some plots of $ \sin$ functions:

sage: f(x) = sin(x)
sage: g(x) = sin(2*x)
sage: h(x) = sin(4*x)
sage: p1 = plot(f,-2*pi,2*pi,rgbcolor=hue(0.5))
sage: p2 = plot(g,-2*pi,2*pi,rgbcolor=hue(0.9))
sage: p3 = parametric_plot((f,g),0,2*pi,rgbcolor=hue(0.6))
sage: p4 = parametric_plot((f,h),0,2*pi,rgbcolor=hue(1.0))

Now make a graphics array out of the plots; then you can type either: ga.show() or ga.save().

sage: graphics_array(((p1,p2),(p3,p4)))

Here we give only one row:

sage: p1 = plot(sin,-4,4)
sage: p2 = plot(cos,-4,4)
sage: g = graphics_array([p1, p2]); print g
Graphics Array of size 1 x 2
sage: g.show()

hue( h, [s=1], [v=1])

hue(h,s=1,v=1) where 'h' stands for hue, 's' stands for saturation, 'v' stands for value. hue returns a tuple of rgb intensities (r, g, b) All values are in the range 0 to 1.

Input:

h, s, v
- real numbers between 0 and 1. Note that if any are not in this range they are automatically normalized to be in this range by reducing them modulo 1.
Output: A valid RGB tuple.

sage: hue(0.6)
(0.0, 0.40000000000000036, 1.0)

hue is an easy way of getting a broader range of colors for graphics

sage: plot(sin, -2, 2, rgbcolor=hue(0.6))

implicit_plot( f, xrange, yrange)

implicit_plot takes a function of two variables, $ f(x,y)$ and plots the curve $ f(x,y)=0$ over the specified xrange and yrange as demonstrated below.

implicit_plot(f, (xmin, xmax), (ymin, ymax), ...)

Input:

f
- a function of two variables
(xmin, xmax)
- 2-tuple, the range of x values
(ymin, ymax)
- 2-tuple, the range of y values The following inputs must all be passed in as named parameters:
plot_points
- integer (default: 25); number of points to plot in each direction of the grid

We can define a level-$ n$ approximation of the boundary of the Mandelbrot set.

sage: def mandel(n):
...       c = polygen(CDF, 'c')
...       z = 0
...       for i in range(n):
...           z = z*z + c
...       def f(x, y):
...           val = z(CDF(x, y))
...           return val.norm() - 4
...       return f

The first-level approximation is just a circle.

sage: implicit_plot(mandel(1), (-3, 3), (-3, 3)).show(aspect_ratio=1)

A third-level approximation starts to get interesting.

sage: implicit_plot(mandel(3), (-2, 1), (-1.5, 1.5)).show(aspect_ratio=1)

The seventh-level approximation is a degree 64 polynomial, and implicit_plot does a pretty good job on this part of the curve. (plot_points=200 looks even better, but it's about 16 times slower.)

sage: implicit_plot(mandel(7), (-0.3, 0.05), (-1.15, -0.9),plot_points=50).show(aspect_ratio=1)

is_Graphics( x)

Return True if $ x$ is a Graphics object.

sage: is_Graphics(1)
False
sage: is_Graphics(disk((0.0, 0.0), 1, (0, pi/2)))
True

line( points)

Returns either a 2-dimensional or 3-dimensional line depending on value of points.

For information regarding additional arguments, see either line2d? or line3d?.

sage: line([(0,0), (1,1)])
sage: line([(0,0,1), (1,1,1)])

line2d( points)

Create the line through the given list of points.

Type line.options for a dictionary of the default options for lines. You can change this to change the defaults for all future lines. Use line.reset() to reset to the default options.

Input:

alpha
- How transparent the line is
thickness
- How thick the line is
rgbcolor
- The color as an rgb tuple
hue
- The color given as a hue Any MATPLOTLIB line option may also be passed in. E.g.,
linestyle
- The style of the line, which is one of '-' (dashed), '-.' (dash dot), '-' (solid), 'steps', ':' (dotted)
marker
- "'0' (tickleft), '1' (tickright), '2' (tickup), '3' (tickdown), '' (nothing), ' ' (nothing), '+' (plus), ',' (pixel), '.' (point), '1' (tri_down), '3' (tri_left), '2' (tri_up), '4' (tri_right), '<' (triangle_left), '>' (triangle_right), 'None' (nothing), 'D' (diamond), 'H' (hexagon2), '_' (hline), '' (triangle_up), 'd' (thin_diamond), 'h' (hexagon1), 'o' (circle), 'p' (pentagon), 's' (square), 'v' (triangle_down), 'x' (x), '|' (vline)"
markersize
- the size of the marker in points
markeredgecolor
- the markerfacecolor can be any color arg
markeredgewidth
- the size of the markter edge in points

A blue conchoid of Nicomedes:

sage: L = [[1+5*cos(pi/2+pi*i/100), tan(pi/2+pi*i/100)*(1+5*cos(pi/2+pi*i/100))] for i in range(1,100)]
sage: line(L, rgbcolor=(1/4,1/8,3/4))

A line with 2 complex points:

sage: i = CC.0
sage: line([1+i, 2+3*i])

A blue hypotrochoid (3 leaves):

sage: n = 4; h = 3; b = 2
sage: L = [[n*cos(pi*i/100)+h*cos((n/b)*pi*i/100),n*sin(pi*i/100)-h*sin((n/b)*pi*i/100)] for i in range(200)]
sage: line(L, rgbcolor=(1/4,1/4,3/4))

A blue hypotrochoid (4 leaves):

sage: n = 6; h = 5; b = 2
sage: L = [[n*cos(pi*i/100)+h*cos((n/b)*pi*i/100),n*sin(pi*i/100)-h*sin((n/b)*pi*i/100)] for i in range(200)]
sage: line(L, rgbcolor=(1/4,1/4,3/4))

A red limacon of Pascal:

sage: L = [[sin(pi*i/100)+sin(pi*i/50),-(1+cos(pi*i/100)+cos(pi*i/50))] for i in range(-100,101)]
sage: line(L, rgbcolor=(1,1/4,1/2))

A light green trisectrix of Maclaurin:

sage: L = [[2*(1-4*cos(-pi/2+pi*i/100)^2),10*tan(-pi/2+pi*i/100)*(1-4*cos(-pi/2+pi*i/100)^2)] for i in range(1,100)]
sage: line(L, rgbcolor=(1/4,1,1/8))

A green lemniscate of Bernoulli:

sage: v = [(1/cos(-pi/2+pi*i/100), tan(-pi/2+pi*i/100)) for i in range(201)]
sage: L = [(a/(a^2+b^2), b/(a^2+b^2)) for a,b in v]
sage: line(L, rgbcolor=(1/4,3/4,1/8))

A red plot of the Jacobi elliptic function sn$ (x,2)$ , $ -3<x<3$ :

sage: L = [(i/100.0, jacobi('sn', i/100.0 ,2.0)) for i in range(-300,300,30)]    
sage: line(L, rgbcolor=(3/4,1/4,1/8))

A red plot of $ J$ -Bessel function $ J_2(x)$ , $ 0<x<10$ :

sage: L = [(i/10.0, bessel_J(2,i/10.0)) for i in range(100)]
sage: line(L, rgbcolor=(3/4,1/4,5/8))

A purple plot of the Riemann zeta function $ \zeta(1/2 + it)$ , $ 0<t<30$ :

sage: i = CDF.gen()
sage: v = [zeta(0.5 + n/10 * i) for n in range(300)]
sage: L = [(z.real(), z.imag()) for z in v]
sage: line(L, rgbcolor=(3/4,1/2,5/8))

A purple plot of the Hasse-Weil $ L$ -function $ L(E, 1 + it)$ , $ -1<t<10$ :

sage: E = EllipticCurve('37a')
sage: vals = E.lseries().values_along_line(1-I, 1+10*I, 100) # critical line
sage: L = [(z[1].real(), z[1].imag()) for z in vals]
sage: line(L, rgbcolor=(3/4,1/2,5/8))

A red, blue, and green "cool cat":

sage: G = plot(-cos(x), -2, 2, thickness=5, rgbcolor=(0.5,1,0.5))
sage: P = polygon([[1,2], [5,6], [5,0]], rgbcolor=(1,0,0))
sage: Q = polygon([(-x,y) for x,y in P[0]], rgbcolor=(0,0,1))
sage: G + P + Q   # show the plot

A line with no points or one point:

sage: line([])
sage: line([(1,1)])

TESTS: We test to make sure that the x/y min/max data are set correctly.

sage: l = line([(100, 100), (120, 120)]) 
sage: l.xmin()
100.0
sage: l.xmax()
120.0

list_plot( data, [plotjoined=False])

list_plot takes a single list of data, in which case it forms a list of tuples $ (i,di)$ where $ i$ goes from 0 to $ {\rm len}(data)-1$ and $ di$ is the $ i$ th data value, and puts points at those tuple values.

list_plot also takes a list of tuples $ (dxi, dyi)$ where $ dxi$ is the $ i$ th data representing the $ x$ -value, and $ dyi$ is the $ i$ th $ y$ -value. If plotjoined=True, then a line spanning all the data is drawn instead.

sage: list_plot([i^2 for i in range(5)])

Here are a bunch of random red points:

sage: r = [(random(),random()) for _ in range(20)]
sage: list_plot(r,rgbcolor=(1,0,0))

This gives all the random points joined in a purple line:

sage: list_plot(r, plotjoined=True, rgbcolor=(1,0,1))

TESTS: We check to see that the x/y min/max data are set correctly.

sage: p = list_plot([(100,100), (120, 120)])
sage: p.xmin()
100.0
sage: p.ymin()
100.0

matrix_plot( mat)

A plot of a given matrix or 2D array.

Each ($ i$ th, $ j$ th) matrix element is given a different color value depending on its relative size compared to the other elements in the matrix.

The tick marks drawn on the frame axes denote the ($ i$ th, $ j$ th) element of the matrix.

A matrix over ZZ colored with different grey levels:

sage: matrix_plot(matrix([[1,3,5,1],[2,4,5,6],[1,3,5,7]]))

Here we make a random matrix over RR and use cmap='hsv' to color the matrix elements different RGB colors:

sage: matrix_plot(random_matrix(RDF, 50), cmap='hsv')

Another random plot, but over GF(389):

sage: matrix_plot(random_matrix(GF(389), 10), cmap='Oranges')

minmax_data( xdata, ydata, [dict=False])

Returns the minimums and maximums of xdata and ydata.

If dict is False, then minmax_data returns the tuple (xmin, xmax, ymin, ymax); otherwise, it returns a dictionary whose keys are 'xmin', 'xmax', 'ymin', and 'ymax' and whose values are the corresponding values.

sage: from sage.plot.plot import minmax_data
sage: minmax_data([], [])
(-1, 1, -1, 1)
sage: minmax_data([-1, 2], [4, -3])
(-1, 2, -3, 4)
sage: d = minmax_data([-1, 2], [4, -3], dict=True)
sage: list(sorted(d.items()))
[('xmax', 2), ('xmin', -1), ('ymax', 4), ('ymin', -3)]

networkx_plot( graph, [pos=None], [vertex_labels=True], [vertex_size=300], [vertex_colors=None], [edge_colors=None], [graph_border=False], [scaling_term=0.05], [draw_edges=True])

Creates a graphics object ready to display a NetworkX graph.

Input:

graph
- a NetworkX graph
pos
- an optional positioning dictionary: for example, the spring layout from NetworkX for the 5-cycle is 0: [-0.91679746, 0.88169588,], 1: [ 0.47294849, 1.125 ,], 2: [ 1.125 ,-0.12867615,], 3: [ 0.12743933,-1.125 ,], 4: [-1.125 ,-0.50118505,]
vertex_labels
- determines whether labels for nodes are plotted
vertex_size
- node size
vertex_colors
- a dictionary specifying node colors: each key is a color recognized by matplotlib, and each entry is a list of vertices.
edge_colors
- a dictionary specifying edge colors: each key is a color recognized by matplotlib, and each entry is a list of edges.
scaling_term
- default is 0.05. if nodes are getting chopped off, increase; if graph is too small, decrease. should be positive, but values much bigger than 1/8 won't be useful unless the nodes are huge

sage: import networkx
sage: D = networkx.dodecahedral_graph()
sage: networkx_plot(D)

sage: import networkx
sage: from math import sin, cos, pi
sage: P = networkx.petersen_graph()
sage: d = {'#FF0000':[0,5], '#FF9900':[1,6], '#FFFF00':[2,7], '#00FF00':[3,8], '#0000FF':[4,9]}
sage: pos_dict = {}
sage: for i in range(5):
...    x = float(cos(pi/2 + ((2*pi)/5)*i))
...    y = float(sin(pi/2 + ((2*pi)/5)*i))
...    pos_dict[i] = [x,y]
...
sage: for i in range(10)[5:]:
...    x = float(0.5*cos(pi/2 + ((2*pi)/5)*i))
...    y = float(0.5*sin(pi/2 + ((2*pi)/5)*i))
...    pos_dict[i] = [x,y]
...
sage: networkx_plot(graph=P, vertex_colors=d, pos=pos_dict)

sage: C = graphs.CubeGraph(5)
sage: from sage.plot.plot import rainbow
sage: R = rainbow(5)
sage: edge_colors = {}
sage: for i in range(5):
...    edge_colors[R[i]] = []
sage: for u,v,l in C.edges():
...    for i in range(5):
...        if u[i] != v[i]:
...            edge_colors[R[i]].append((u,v,l))
sage: networkx_plot(C.networkx_graph(), pos=C.get_pos(), edge_colors=edge_colors, vertex_labels=False, vertex_size=0)

parametric_plot( funcs, tmin, tmax)

parametric_plot takes two or three functions as a list or a tuple and makes a plot with the first function giving the $ x$ coordinates, the second function giving the $ y$ coordinates, and the third function (if present) giving the $ z$ coordinates.

Input:

funcs
- 2 or 3-tuple of functions
tmin
- start value of t
tmax
- end value of t
other options
- passed to plot.

We draw some 2d parametric plots:

sage: t = var('t')
sage: parametric_plot( (sin(t), sin(2*t)), 0, 2*pi, rgbcolor=hue(0.6) )
sage: parametric_plot((1, t), 0, 4)
sage: parametric_plot((t, t^2), -4, 4)

We draw a 3d parametric plot:

sage: parametric_plot3d( (5*cos(x), 5*sin(x), x), (-12, 12), plot_points=150, color="red")

TESTS:

sage: parametric_plot((x, t^2), -4, 4)
Traceback (most recent call last):
...
ValueError: there cannot be more than one free variable in funcs

sage: parametric_plot((1, x+t), -4, 4)
Traceback (most recent call last):
...
ValueError: there cannot be more than one free variable in funcs

plot( funcs)

Use plot by writing

plot(X, ...)

where $ X$ is a Sage object (or list of Sage objects) that either is callable and returns numbers that can be coerced to floats, or has a plot method that returns a GraphicPrimitive object.

Type plot.options for a dictionary of the default options for plots. You can change this to change the defaults for all future plots. Use plot.reset() to reset to the default options.

PLOT OPTIONS: The plot options are plot_points - (default: 200) the number of points to initially plot before the adaptive refinement. adaptive_recursion - (default: 5) how many levels of recursion to go before giving up when doing adaptive refinement. Setting this to 0 disables adaptive refinement. adaptive_tolerance - (default: 0.01) how large a difference should be before the adaptive refinement code considers it significant. Use a smaller value for smoother plots and a larger value for coarser plots. In general, adjust adaptive_recursion before adaptive_tolerance, and consult the code of adaptive_refinement for the details.

xmin - starting x value xmax - ending x value color - an rgb-tuple (r,g,b) with each of r,g,b between 0 and 1, or a color name as a string (e.g., 'purple'), or an HTML color such as '#aaff0b'.

APPEARANCE OPTIONS: The following options affect the appearance of the line through the points on the graph of $ X$ (these are the same as for the line function):

Input:

alpha
- How transparent the line is
thickness
- How thick the line is
rgbcolor
- The color as an rgb tuple
hue
- The color given as a hue Any MATPLOTLIB line option may also be passed in. E.g.,
linestyle
- The style of the line, which is one of '-' (dashed), '-.' (dash dot), '-' (solid), 'steps', ':' (dotted)
marker
- "'0' (tickleft), '1' (tickright), '2' (tickup), '3' (tickdown), '' (nothing), ' ' (nothing), '+' (plus), ',' (pixel), '.' (point), '1' (tri_down), '3' (tri_left), '2' (tri_up), '4' (tri_right), '<' (triangle_left), '>' (triangle_right), 'None' (nothing), 'D' (diamond), 'H' (hexagon2), '_' (hline), '' (triangle_up), 'd' (thin_diamond), 'h' (hexagon1), 'o' (circle), 'p' (pentagon), 's' (square), 'v' (triangle_down), 'x' (x), '|' (vline)"
markersize
- the size of the marker in points
markeredgecolor
- the markerfacecolor can be any color arg
markeredgewidth
- the size of the marker edge in points

Note that this function does NOT simply sample equally spaced points between xmin and xmax. Instead it computes equally spaced points and add small perturbations to them. This reduces the possibility of, e.g., sampling sin only at multiples of $ 2\pi$ , which would yield a very misleading graph.

We plot the sin function:

sage: P = plot(sin, (0,10)); print P
Graphics object consisting of 1 graphics primitive
sage: len(P)     # number of graphics primitives
1
sage: len(P[0])  # how many points were computed (random)
225
sage: P          # render

sage: P = plot(sin, (0,10), plot_points=10); print P
Graphics object consisting of 1 graphics primitive
sage: len(P[0])  # random output
32
sage: P          # render

We plot with randomize=False, which makes the initial sample points evenly spaced (hence always the same). Adaptive plotting might insert other points, however, unless adaptive_recursion=0.

sage: p=plot(1, (x,0,3), plot_points=4, randomize=False, adaptive_recursion=0)
sage: list(p[0])
[(0.0, 1.0), (1.0, 1.0), (2.0, 1.0), (3.0, 1.0)]

Some colored functions:

sage: plot(sin, 0, 10, rgbcolor='#ff00ff')
sage: plot(sin, 0, 10, rgbcolor='purple')

We plot several functions together by passing a list of functions as input:

sage: plot([sin(n*x) for n in [1..4]], (0, pi))

The function $ \sin(1/x)$ wiggles wildly near 0 . Sage adapts to this and plots extra points near the origin.

sage: plot(sin(1/x), (x, -1, 1))

Note that the independent variable may be omitted if there is no ambiguity:

sage: plot(sin(1/x), (-1, 1))

The algorithm used to insert extra points is actually pretty simple. On the picture drawn by the lines below:

sage: p = plot(x^2, (-0.5, 1.4)) + line([(0,0), (1,1)], rgbcolor='green')
sage: p += line([(0.5, 0.5), (0.5, 0.5^2)], rgbcolor='purple')
sage: p += point(((0, 0), (0.5, 0.5), (0.5, 0.5^2), (1, 1)), rgbcolor='red', pointsize=20)
sage: p += text('A', (-0.05, 0.1), rgbcolor='red')
sage: p += text('B', (1.01, 1.1), rgbcolor='red')
sage: p += text('C', (0.48, 0.57), rgbcolor='red')
sage: p += text('D', (0.53, 0.18), rgbcolor='red')
sage: p.show(axes=False, xmin=-0.5, xmax=1.4, ymin=0, ymax=2)

You have the function (in blue) and its approximation (in green) passing through the points A and B. The algorithm finds the midpoint C of AB and computes the distance between C and D. The point D is added to the curve if it exceeds the (nonzero) adaptive_tolerance threshold. If D is added to the curve, then the algorithm is applied recursively to the points A and D, and D and B. It is repeated adaptive_recursion times (10, by default).

The actual sample points are slightly randomized, so the above plots may look slightly different each time you draw them.

We draw the graph of an elliptic curve as the union of graphs of 2 functions.

sage: def h1(x): return abs(sqrt(x^3  - 1))
sage: def h2(x): return -abs(sqrt(x^3  - 1))
sage: P = plot([h1, h2], 1,4)
sage: P          # show the result

We can also directly plot the elliptic curve:

sage: E = EllipticCurve([0,-1])
sage: plot(E, (1, 4), rgbcolor=hue(0.6))

We can change the line style to one of '-' (dashed), '-.' (dash dot), '-' (solid), 'steps', ':' (dotted):

sage: plot(sin(x), 0, 10, linestyle='-.')

Sage currently ignores points that cannot be evaluated

sage: plot(-x*log(x), (x,0,1))  # this works fine since the failed endpoint is just skipped.

This prints out a warning and plots where it can (we turn off the warning by setting the verbose mode temporarily to -1.)

sage: set_verbose(-1)
sage: plot(x^(1/3), (x,-1,1))
sage: set_verbose(0)

To plot the negative real cube root, use something like the following.

sage: plot(lambda x : RR(x).nth_root(3), (x,-1, 1))

TESTS: We do not randomize the endpoints:

sage: p = plot(x, (x,-1,1))
sage: p[0].xdata[0] == -1
True
sage: p[0].xdata[-1] == 1
True

We check to make sure that the x/y min/max data get set correctly when there are multiple functions.

sage: p = plot([sin(x), cos(x)], 100, 120)
sage: p.xmin()
100.0
sage: p.xmax()
120.0

We check to handle cases where the function gets evaluated at a point which causes an 'inf' or '-inf' result to be produced.

sage: p = plot(1/x, 0, 1)
sage: p = plot(-1/x, 0, 1)

plot_vector_field( ['f', 'g'], xrange, yrange)

plot_vector_field takes two functions of two variables, $ (f(x,y), g(x,y))$ and plots vector arrows of the function over the specified xrange and yrange as demonstrated below.

plot_vector_field((f, g), (xvar, xmin, xmax), (yvar, ymin, ymax))

Plot the vector fields involving sin and cos

sage: x,y = var('x y')
sage: plot_vector_field((sin(x), cos(y)), (x,-3,3), (y,-3,3))
sage: plot_vector_field(( y, (cos(x)-2)*sin(x)), (x,-pi,pi), (y,-pi,pi))

Plot a gradient field

sage: u,v = var('u v')
sage: f = exp(-(u^2+v^2))
sage: plot_vector_field(f.gradient(), (u,-2,2), (v,-2,2))

TESTS:

sage: p = plot_vector_field((.01*x,x+y), (10,20), (10,20))
sage: p.xmin()
10.0
sage: p.ymin()
10.0

point( points)

Returns either a 2-dimensional or 3-dimensional point depending on value of points.

For information regarding additional arguments, see either point2d? or point3d?.

sage: point([(0,0), (1,1)])
sage: point([(0,0,1), (1,1,1)])

point2d( points)

A point of size `pointsize' defined by point = $ (x,y)$ . Type point.options to see all options. point takes either a single tuple of coordinates or a list of tuples.

A purple point from a single tuple or coordinates:

sage: point((0.5, 0.5), rgbcolor=hue(0.75))

Here are some random larger red points, given as a list of tuples

sage: point(((0.5, 0.5), (1, 2), (0.5, 0.9), (-1, -1)), rgbcolor=hue(1), pointsize=30)

TESTS: We check to make sure that the x/y min/max data is set correctly.

sage: p = point((3, 3), rgbcolor=hue(0.75))
sage: p.xmin()
3.0
sage: p.ymin()
3.0

points( points)

Returns either a 2-dimensional or 3-dimensional point depending on value of points.

For information regarding additional arguments, see either point2d? or point3d?.

sage: point([(0,0), (1,1)])
sage: point([(0,0,1), (1,1,1)])

polar_plot( funcs, xmin, xmax)

polar_plot takes a single function or a list or tuple of functions and plots them parametrically in the given range.

Here is a blue 8-leaved petal:

sage: polar_plot(sin(5*x)^2, 0, 2*pi, rgbcolor=hue(0.6))

A red figure-8:

sage: polar_plot(abs(sqrt(1 - sin(x)^2)), 0, 2*pi, rgbcolor=hue(1.0))

A green limacon of Pascal:

sage: polar_plot(2 + 2*cos(x), 0, 2*pi, rgbcolor=hue(0.3))

polygon( points)

Type polygon.options for a dictionary of the default options for polygons. You can change this to change the defaults for all future polygons. Use polygon.reset() to reset to the default options.

We create a purple-ish polygon:

sage: polygon([[1,2], [5,6], [5,0]], rgbcolor=(1,0,1))

Some modern art - a random polygon:

sage: v = [(randrange(-5,5), randrange(-5,5)) for _ in range(10)]
sage: polygon(v)

A purple hexagon:

sage: L = [[cos(pi*i/3),sin(pi*i/3)] for i in range(6)]
sage: polygon(L, rgbcolor=(1,0,1))

A green deltoid:

sage: L = [[-1+cos(pi*i/100)*(1+cos(pi*i/100)),2*sin(pi*i/100)*(1-cos(pi*i/100))] for i in range(200)]
sage: polygon(L, rgbcolor=(1/8,3/4,1/2))

A blue hypotrochoid:

sage: L = [[6*cos(pi*i/100)+5*cos((6/2)*pi*i/100),6*sin(pi*i/100)-5*sin((6/2)*pi*i/100)] for i in range(200)]
sage: polygon(L, rgbcolor=(1/8,1/4,1/2))

Another one:

sage: n = 4; h = 5; b = 2
sage: L = [[n*cos(pi*i/100)+h*cos((n/b)*pi*i/100),n*sin(pi*i/100)-h*sin((n/b)*pi*i/100)] for i in range(200)]
sage: polygon(L, rgbcolor=(1/8,1/4,3/4))

A purple epicycloid:

sage: m = 9; b = 1
sage: L = [[m*cos(pi*i/100)+b*cos((m/b)*pi*i/100),m*sin(pi*i/100)-b*sin((m/b)*pi*i/100)] for i in range(200)]
sage: polygon(L, rgbcolor=(7/8,1/4,3/4))

A brown astroid:

sage: L = [[cos(pi*i/100)^3,sin(pi*i/100)^3] for i in range(200)]
sage: polygon(L, rgbcolor=(3/4,1/4,1/4))

And, my favorite, a greenish blob:

sage: L = [[cos(pi*i/100)*(1+cos(pi*i/50)), sin(pi*i/100)*(1+sin(pi*i/50))] for i in range(200)]
sage: polygon(L, rgbcolor=(1/8, 3/4, 1/2))

This one is for my wife:

sage: L = [[sin(pi*i/100)+sin(pi*i/50),-(1+cos(pi*i/100)+cos(pi*i/50))] for i in range(-100,100)]
sage: polygon(L, rgbcolor=(1,1/4,1/2))

TESTS: We check to make sure that the x/y min/max data is set correctly.

sage: p = polygon([[1,2], [5,6], [5,0]], rgbcolor=(1,0,1))
sage: p.ymin()
0.0
sage: p.xmin()
1.0

Author: David Joyner (2006-04-14): the long list of examples above.

rainbow( n, [format=hex])

Given an integer $ n$ , returns a list of colors, represented in HTML hex, that changes smoothly in hue from one end of the spectrum to the other. Written in order to easily represent vertex partitions on graphs.

Author: Robert L. Miller

sage: from sage.plot.plot import rainbow
sage: rainbow(7)
['#ff0000', '#ffda00', '#48ff00', '#00ff91', '#0091ff', '#4800ff',
'#ff00da']
sage: rainbow(7, 'rgbtuple')
[(1.0, 0.0, 0.0), (1.0, 0.8571428571428571, 0.0), (0.28571428571428581,
1.0, 0.0), (0.0, 1.0, 0.57142857142857162), (0.0, 0.57142857142857162,
1.0), (0.28571428571428559, 0.0, 1.0), (1.0, 0.0, 0.85714285714285676)]

reshape( v, n, m)

setup_for_eval_on_grid( v, xrange, yrange, plot_points)

Input:

v
- a list of functions
xrange
- 2 or 3 tuple (if 3, first is a variable)
yrange
- 2 or 3 tuple
plot_points
- a positive integer

Output:
g
- tuple of fast callable functions
xstep
- step size in xdirection
ystep
- step size in ydirection
xrange
- tuple of 2 floats
yrange
- tuple of 2 floats

sage: x,y = var('x,y')
sage: sage.plot.plot.setup_for_eval_on_grid([x^2 + y^2], (x,0,5), (y,0,pi), 10)
([<sage.ext.fast_eval.FastDoubleFunc object at ...>],
 0.5,
 0.31415926535897931,
 (0.0, 5.0),
 (0.0, 3.1415926535897931))

show_default( [default=None])

Set the default for showing plots using any plot commands. If called with no arguments, returns the current default.

If this is True (the default) then any plot object when displayed will be displayed as an actual plot instead of text, i.e., the show command is not needed.

The default starts out as True:

sage: show_default()
True

We set it to False.

sage: show_default(False)

We see that it is False.

sage: show_default()
False

Now plot commands will not display their plots by default.

Turn back on default display.

sage: show_default(True)

text( string, ['x', 'y'])

Returns a 2d text graphics object at the point $ (x,y)$ .

Type text.options for a dictionary of options for 2d text.

2D OPTIONS: fontsize - How big the text is rgbcolor - The color as an rgb tuple hue - The color given as a hue vertical_alignment - how to align vertically: top, center, bottom horizontal_alignment - how to align horizontally: left, center, right axis_coords - (default: False) if True, use axis coordinates, so that (0,0) is the lower left and (1,1) upper right, irregardless of the x and y range of plotted values.

Some text:

sage: text("Sage is really neat!!",(2,12))

The same text in larger font and colored red:

sage: text("Sage is really neat!!",(2,12),fontsize=20,rgbcolor=(1,0,0))

Some text but guaranteed to be in the lower left no matter what:

sage: text("Sage is really neat!!",(0,0), axis_coords=True, horizontal_alignment='left')

You can also align text differently:

sage: t1 = text("Hello",(1,1), vertical_alignment="top")
sage: t2 = text("World", (1,0.5), horizontal_alignment="left")
sage: t1 + t2   # render the sume

to_float_list( v)

Given a list or tuple or iterable v, coerce each element of v to a float and make a list out of the result.

sage: from sage.plot.plot import to_float_list
sage: to_float_list([1,1/2,3])
[1.0, 0.5, 3.0]

to_mpl_color( c)

Convert a tuple or string to a matplotlib rgb color tuple.

Input:

c
- string or 3-tuple

Output: 3-tuple of floats between 0 and 1.

sage: from sage.plot.plot import to_mpl_color
sage: to_mpl_color('#fa0')
(1.0, 0.66666666666666663, 0.0)
sage: to_mpl_color('#ffffe1')
(1.0, 1.0, 0.88235294117647056)
sage: to_mpl_color('blue')
(0.0, 0.0, 1.0)
sage: to_mpl_color([1,1/2,1/3])
(1.0, 0.5, 0.33333333333333331)
sage: to_mpl_color([1,2,255])   # WARNING -- numbers are reduced mod 1!!
(1.0, 0.0, 0.0)

var_and_list_of_values( v, plot_points)

Input:

v
- (v0, v1) or (var, v0, v1); if the former return the range of values between v0 and v1 taking plot_points steps; if var is given, also return var.
plot_points
- integer >= 2 (the endpoints)

Output:
var
- a variable or None
list
- a list of floats

sage: from sage.plot.plot import var_and_list_of_values
sage: var_and_list_of_values((var('theta'), 2, 5),  5)
(theta, [2.0, 2.75, 3.5, 4.25, 5.0])
sage: var_and_list_of_values((2, 5),  5)
(None, [2.0, 2.75, 3.5, 4.25, 5.0])
sage: var_and_list_of_values((var('theta'), 2, 5),  2)
(theta, [2.0, 5.0])
sage: var_and_list_of_values((2, 5),  2)
(None, [2.0, 5.0])

xydata_from_point_list( points)

Returns two lists (xdata, ydata), each coerced to a list of floats, which correspond to the x-coordinates and the y-coordinates of the points.

The points parameter can be a list of 2-tuples or some object that yields a list of one or two numbers.

This function can potentially be very slow for large point sets.

Class: GraphicPrimitive

class GraphicPrimitive
Base class for graphics primitives, e.g., things that knows how to draw themselves in 2d.

We create an object that derives from GraphicPrimitive:

sage: P = line([(-1,-2), (3,5)])
sage: P[0]
Line defined by 2 points
sage: type(P[0])
<class 'sage.plot.plot.GraphicPrimitive_Line'>
GraphicPrimitive( self, options)

Create a base class GraphicsPrimitive. All this does is set the options.

We indirectly test this function.

sage: from sage.plot.plot import GraphicPrimitive
sage: GraphicPrimitive({})
Graphics primitive

Functions: options,$ \,$ plot3d

options( self)

Return the dictionary of options for this graphics primitive.

By default this function verifies that the options are all valid; if any aren't a verbose message is printed with level 0.

sage: from sage.plot.plot import GraphicPrimitive
sage: GraphicPrimitive({}).options()
{}

Special Functions: __init__,$ \,$ _allowed_options,$ \,$ _plot3d_options,$ \,$ _repr_

_allowed_options( self)

Return the allowed options for a graphics primitive.

Output:

- a reference to a dictionary.

sage: from sage.plot.plot import GraphicPrimitive
sage: GraphicPrimitive({})._allowed_options()
{}

_plot3d_options( self, [options=None])

Translate 2d plot options into 3d plot options.

_repr_( self)

String representation of this graphics primitive.

sage: from sage.plot.plot import GraphicPrimitive
sage: GraphicPrimitive({})._repr_()
'Graphics primitive'

Class: GraphicPrimitive_Arrow

class GraphicPrimitive_Arrow
Primitive class that initializes the arrow graphics type

We create an arrow graphics object, then take the 0th entry in it to get the actual Arrow graphics primitive:

sage: P = arrow((0,1), (2,3))[0]
sage: type(P)
<class 'sage.plot.plot.GraphicPrimitive_Arrow'>
sage: P
Arrow from (0.0,1.0) to (2.0,3.0)
GraphicPrimitive_Arrow( self, xtail, ytail, xhead, yhead, options)

Create an arrow graphics primitive.

sage: from sage.plot.plot import GraphicPrimitive_Arrow
sage: GraphicPrimitive_Arrow(0,0,2,3,{})
Arrow from (0.0,0.0) to (2.0,3.0)

Functions: plot3d

plot3d( self)

sage: arrow((0,0),(1,1)).plot3d()

Special Functions: __init__,$ \,$ _allowed_options,$ \,$ _plot3d_options,$ \,$ _render_on_subplot,$ \,$ _repr_

_allowed_options( self)

Return the dictionary of allowed options for the arrow graphics primitive.

sage: from sage.plot.plot import GraphicPrimitive_Arrow
sage: list(sorted(GraphicPrimitive_Arrow(0,0,2,3,{})._allowed_options().iteritems()))
[('arrowshorten', 'The length in points to shorten the arrow.'),
('arrowsize', 'The size of the arrowhead'),
('hue', 'The color given as a hue.'),
('rgbcolor', 'The color as an rgb tuple.'),
('width', 'The width of the shaft of the arrow, in points.')]

_render_on_subplot( self, subplot)

Render this arrow in a subplot. This is the key function that defines how this arrow graphics primitive is rendered in matplotlib's library.

This function implicitly ends up rendering this arrow on a matplotlib subplot:

sage: arrow((0,1), (2,-1))

_repr_( self)

Text representation of an arrow graphics primitive.

sage: from sage.plot.plot import GraphicPrimitive_Arrow
sage: GraphicPrimitive_Arrow(0,0,2,3,{})._repr_()
'Arrow from (0.0,0.0) to (2.0,3.0)'

Class: GraphicPrimitive_BarChart

class GraphicPrimitive_BarChart
Graphics primitive that represents a bar chart.

sage: from sage.plot.plot import GraphicPrimitive_BarChart
sage: g = GraphicPrimitive_BarChart(range(4), [1,3,2,0], {}); g
BarChart defined by a 4 datalist 
sage: type(g)
<class 'sage.plot.plot.GraphicPrimitive_BarChart'>
GraphicPrimitive_BarChart( self, ind, datalist, options)

Initialize a BarChart primitive.

sage: from sage.plot.plot import GraphicPrimitive_BarChart
sage: GraphicPrimitive_BarChart(range(3), [10,3,5], {'width':0.7})
BarChart defined by a 3 datalist

Special Functions: __init__,$ \,$ _allowed_options,$ \,$ _render_on_subplot,$ \,$ _repr_

_allowed_options( self)

Return the allowed options with descriptions for this graphics primitive. This is used in displaying an error message when the user gives an option that doesn't make sense.

sage: from sage.plot.plot import GraphicPrimitive_BarChart
sage: g = GraphicPrimitive_BarChart(range(4), [1,3,2,0], {})
sage: list(sorted(g._allowed_options().iteritems()))
[('hue', 'The color given as a hue.'),
 ('rgbcolor', 'The color as an rgb tuple.'),
 ('width', 'The width of the bars')]

_render_on_subplot( self, subplot)

Render this bar chart graphics primitive on a matplotlib subplot object.

This rendering happens implicitly when the following command is executed:

sage: bar_chart([1,2,10])

_repr_( self)

Return text representation of this bar chart graphics primitive.

sage: from sage.plot.plot import GraphicPrimitive_BarChart
sage: g = GraphicPrimitive_BarChart(range(4), [1,3,2,0], {})
sage: g._repr_()
'BarChart defined by a 4 datalist'

Class: GraphicPrimitive_Circle

class GraphicPrimitive_Circle
Circle graphics primitive.
GraphicPrimitive_Circle( self, x, y, r, options)

Functions: plot3d

plot3d( self)

sage: circle((0,0), 1).plot3d()
sage: sum([circle((random(),random()), random()).plot3d(z=random()) for _ in range(20)])

Special Functions: __init__,$ \,$ _allowed_options,$ \,$ _render_on_subplot,$ \,$ _repr_

Class: GraphicPrimitive_ContourPlot

class GraphicPrimitive_ContourPlot
Primitive class that initializes the contour_plot graphics type
GraphicPrimitive_ContourPlot( self, xy_data_array, xrange, yrange, options)

Special Functions: __init__,$ \,$ _allowed_options,$ \,$ _render_on_subplot,$ \,$ _repr_

Class: GraphicPrimitive_Disk

class GraphicPrimitive_Disk
Primitive class that initializes the disk graphics type
GraphicPrimitive_Disk( self, point, r, angle, options)

Special Functions: __init__,$ \,$ _allowed_options,$ \,$ _render_on_subplot,$ \,$ _repr_

Class: GraphicPrimitive_Line

class GraphicPrimitive_Line
Primitive class that initializes the line graphics type.

sage: from sage.plot.plot import GraphicPrimitive_Line
sage: GraphicPrimitive_Line([1,2,7], [1,5,-1], {})
Line defined by 3 points
GraphicPrimitive_Line( self, xdata, ydata, options)

Initialize a line graphics primitive.

sage: from sage.plot.plot import GraphicPrimitive_Line
sage: GraphicPrimitive_Line([-1,2], [17,4], {'thickness':2})
Line defined by 2 points

Functions: plot3d

plot3d( self)

sage: EllipticCurve('37a').plot(thickness=5).plot3d()

Special Functions: __getitem__,$ \,$ __init__,$ \,$ __len__,$ \,$ __setitem__,$ \,$ _allowed_options,$ \,$ _plot3d_options,$ \,$ _render_on_subplot,$ \,$ _repr_

__getitem__( self, i)

Extract the i-th element of the line (which is stored as a list of points).

Input:

i
- an integer between 0 and the number of points minus 1

Output: a 2-tuple of floats

sage: L = line([(1,2), (3,-4), (2, 5), (1,2)])
sage: line_primitive = L[0]; line_primitive
Line defined by 4 points
sage: line_primitive[0]
(1.0, 2.0)
sage: line_primitive[2]
(2.0, 5.0)
sage: list(line_primitive)
[(1.0, 2.0), (3.0, -4.0), (2.0, 5.0), (1.0, 2.0)]

__len__( self)

Return the number of points on this line (where a line is really a sequence of line segments through a given list of points).

We create a line, then grab the line primitive as L[0] and compute its length:

sage: L = line([(1,2), (3,-4), (2, 5), (1,2)])
sage: len(L[0])
4

__setitem__( self, i, point)

Set the i-th element of this line (really a sequence of lines through given points).

Input:

i
- an integer between 0 and the number of points on the line minus 1
point
- a 2-tuple of floats

We create a line graphics object $ L$ and get ahold of the corresponding line graphics primitive.

sage: L = line([(1,2), (3,-4), (2, 5), (1,2)])
sage: line_primitive = L[0]; line_primitive
Line defined by 4 points

We then set the 0th point to (0,0) instead of (1,2).

sage: line_primitive[0] = (0,0)
sage: line_primitive[0]
(0.0, 0.0)

Plotting we visibly see the change - now the line starts at (0,0).

sage: L

_allowed_options( self)

Displayed the list of allowed line options.

sage: from sage.plot.plot import GraphicPrimitive_Line
sage: list(sorted(GraphicPrimitive_Line([-1,2], [17,4], {})._allowed_options().iteritems()))
[('alpha', 'How transparent the line is.'),
 ('hue', 'The color given as a hue.'),
 ('linestyle',
  "The style of the line, which is one of '--' (dashed), '-.' (dash dot),
'-' (solid), 'steps', ':' (dotted)."),
 ('marker',
  "'0' (tickleft), '1' (tickright), '2' (tickup), '3' (tickdown), ''
(nothing), ' ' (nothing), '+' (plus), ',' (pixel), '.' (point), '1'
(tri_down), '3' (tri_left), '2' (tri_up), '4' (tri_right), '<'
(triangle_left), '>' (triangle_right), 'None' (nothing), 'D' (diamond), 'H'
(hexagon2), '_' (hline), '^' (triangle_up), 'd' (thin_diamond), 'h'
(hexagon1), 'o' (circle), 'p' (pentagon), 's' (square), 'v'
(triangle_down), 'x' (x), '|' (vline)"),
 ('markeredgecolor', 'the markerfacecolor can be any color arg'),
 ('markeredgewidth', 'the size of the markter edge in points'),
 ('markersize', 'the size of the marker in points'),
 ('rgbcolor', 'The color as an rgb tuple.'),
 ('thickness', 'How thick the line is.')]

_render_on_subplot( self, subplot)

Render this line on a matplotlib subplot.

Input:

subplot
- a matplotlib subplot

This implicitly calls this function:

sage: line([(1,2), (3,-4), (2, 5), (1,2)])

_repr_( self)

String representation of a line primitive.

sage: from sage.plot.plot import GraphicPrimitive_Line
sage: GraphicPrimitive_Line([-1,2,3,3], [17,4,0,2], {})._repr_()
'Line defined by 4 points'

Class: GraphicPrimitive_MatrixPlot

class GraphicPrimitive_MatrixPlot
Primitive class that initializes the matrix_plot graphics type
GraphicPrimitive_MatrixPlot( self, xy_data_array, xrange, yrange, options)

Special Functions: __init__,$ \,$ _allowed_options,$ \,$ _render_on_subplot,$ \,$ _repr_

Class: GraphicPrimitive_NetworkXGraph

class GraphicPrimitive_NetworkXGraph
Primitive class that initializes the NetworkX graph type.

Input:

graph
- a NetworkX graph
pos
- an optional positioning dictionary: for example, the spring layout from NetworkX for the 5-cycle is 0: [-0.91679746, 0.88169588,], 1: [ 0.47294849, 1.125 ,], 2: [ 1.125 ,-0.12867615,], 3: [ 0.12743933,-1.125 ,], 4: [-1.125 ,-0.50118505,]
vertex_labels
- determines whether labels for nodes are plotted
vertex_size
- node size
vertex_colors
- a dictionary specifying node colors: each key is a color recognized by matplotlib, and each entry is a list of vertices.
edge_colors
- a dictionary specifying edge colors: each key is a color recognized by matplotlib, and each entry is a list of edges.
scaling_term
- default is 0.05. if nodes are getting chopped off, increase; if graph is too small, decrease. should be positive, but values much bigger than 1/8 won't be useful unless the nodes are huge
draw_edges
- whether to draw edges.

sage: from sage.plot.plot import GraphicPrimitive_NetworkXGraph
sage: import networkx
sage: D = networkx.dodecahedral_graph()
sage: NGP = GraphicPrimitive_NetworkXGraph(D)
sage: g = Graphics()
sage: g._Graphics__objects.append(NGP)
sage: g.axes(False)
sage: g.show()

sage: import networkx
sage: from sage.plot.plot import GraphicPrimitive_NetworkXGraph
sage: from math import sin, cos, pi
sage: P = networkx.petersen_graph()
sage: d = {'#FF0000':[0,5], '#FF9900':[1,6], '#FFFF00':[2,7], '#00FF00':[3,8], '#0000FF':[4,9]}
sage: pos_dict = {}
sage: for i in range(5):
...    x = float(cos(pi/2 + ((2*pi)/5)*i))
...    y = float(sin(pi/2 + ((2*pi)/5)*i))
...    pos_dict[i] = [x,y]
...
sage: for i in range(10)[5:]:
...    x = float(0.5*cos(pi/2 + ((2*pi)/5)*i))
...    y = float(0.5*sin(pi/2 + ((2*pi)/5)*i))
...    pos_dict[i] = [x,y]
...
sage: NGP = GraphicPrimitive_NetworkXGraph(graph=P, vertex_colors=d, pos=pos_dict)
sage: g = Graphics()
sage: g._Graphics__objects.append(NGP)
sage: g.axes(False)
sage: g.show()

sage: from sage.plot.plot import rainbow
sage: from sage.plot.plot import GraphicPrimitive_NetworkXGraph
sage: import networkx
sage: C = graphs.CubeGraph(5)
sage: pos = C.get_pos()
sage: G = C.networkx_graph()
sage: R = rainbow(5)
sage: edge_colors = {}
sage: for i in range(5):
...    edge_colors[R[i]] = []
sage: for u,v,l in C.edges():
...    for i in range(5):
...        if u[i] != v[i]:
...            edge_colors[R[i]].append((u,v,l))
sage: NGP = GraphicPrimitive_NetworkXGraph(G, pos=pos, vertex_labels=False, vertex_size=0, edge_colors=edge_colors)
sage: G = Graphics()
sage: G._Graphics__objects.append(NGP)
sage: G.axes_range(xmin=-1.1, xmax=2.2, ymin=0, ymax=3.25)
sage: G.axes(False)
sage: G.show()

We color the edges and vertices of a Dodecahedral graph:

sage: g = graphs.DodecahedralGraph()
sage: g.show(edge_colors={(1.0, 0.8571428571428571, 0.0): g.edges()})

GraphicPrimitive_NetworkXGraph( self, graph, [pos=None], [vertex_labels=True], [vertex_size=300], [vertex_colors=None], [edge_colors=None], [scaling_term=0.05], [draw_edges=True])

Special Functions: __init__,$ \,$ _render_on_subplot

Class: GraphicPrimitive_PlotField

class GraphicPrimitive_PlotField
Primitive class that initializes the plot_field graphics type
GraphicPrimitive_PlotField( self, xpos_array, ypos_array, xvec_array, yvec_array, options)

Special Functions: __init__,$ \,$ _allowed_options,$ \,$ _render_on_subplot,$ \,$ _repr_

Class: GraphicPrimitive_Point

class GraphicPrimitive_Point
Primitive class that initializes the point graphics type

GraphicPrimitive_Point( self, xdata, ydata, options)

Functions: plot3d

plot3d( self)

sage: E = EllipticCurve('37a')
sage: P = E(0,0)
sage: def get_points(n):
...     return sum([point((i*P)[0:2], pointsize=3) for i in range(-n,n) if
i != 0 and (i*P)[0] < 3])
sage: sum([get_points(15*n).plot3d(z=n) for n in range(1,10)])

Special Functions: __getitem__,$ \,$ __init__,$ \,$ _allowed_options,$ \,$ _plot3d_options,$ \,$ _render_on_subplot,$ \,$ _repr_

Class: GraphicPrimitive_Polygon

class GraphicPrimitive_Polygon
Primitive class that initializes the polygon graphics type

GraphicPrimitive_Polygon( self, xdata, ydata, options)

Functions: plot3d

plot3d( self)

sage: polygon([(cos(t), sin(t)) for t in srange(0, 2*pi, 2*pi/5)]).plot3d()

Special Functions: __getitem__,$ \,$ __init__,$ \,$ __len__,$ \,$ __setitem__,$ \,$ _allowed_options,$ \,$ _plot3d_options,$ \,$ _render_on_subplot,$ \,$ _repr_

Class: GraphicPrimitive_Text

class GraphicPrimitive_Text
Text graphics primitive.
GraphicPrimitive_Text( self, string, point, options)

Functions: plot3d

Special Functions: __init__,$ \,$ _allowed_options,$ \,$ _plot3d_options,$ \,$ _render_on_subplot,$ \,$ _repr_

Class: Graphics

class Graphics
The Graphics object is an empty list of graphics objects It is useful to use this object when intializing a for loop where different graphics object will be added to the empty object.

sage: G = Graphics(); print G
Graphics object consisting of 0 graphics primitives
sage: c = circle((1,1), 1)
sage: G+=c; print G
Graphics object consisting of 1 graphics primitive

Here we make a graphic of embedded isosceles triangles, coloring each one with a different color as we go:

sage: h=10; c=0.4; p=0.1;
sage: G = Graphics()
sage: for x in srange(1,h+1):
...        l = [[0,x*sqrt(3)],[-x/2,-x*sqrt(3)/2],[x/2,-x*sqrt(3)/2],[0,x*s
qrt(3)]]
...        G+=line(l,rgbcolor=hue(c + p*(x/h)))
sage: G.show(figsize=[5,5])

Graphics( self, [xmin=None], [xmax=None], [ymin=None], [ymax=None])

Create a new empty Graphics objects with all the defaults.

sage: G = Graphics()

Functions: add_primitive,$ \,$ aspect_ratio,$ \,$ axes,$ \,$ axes_color,$ \,$ axes_label_color,$ \,$ axes_labels,$ \,$ axes_range,$ \,$ axes_width,$ \,$ fontsize,$ \,$ plot,$ \,$ plot3d,$ \,$ save,$ \,$ set_aspect_ratio,$ \,$ show,$ \,$ tick_label_color,$ \,$ xmax,$ \,$ xmin,$ \,$ ymax,$ \,$ ymin

add_primitive( self, primitive)

Adds a primitive to this graphics object.

aspect_ratio( self)

Get the current aspect ratio.

Output: either None if the aspect ratio hasn't been set or a positive float

sage: P = circle((1,1), 1)
sage: P.aspect_ratio() is None
True
sage: P.set_aspect_ratio(2)
sage: P.aspect_ratio()
2.0

axes( self, [show=None])

Set whether or not the $ x$ and $ y$ axes are shown by default.

Input:

show
- bool

If called with no input, return the current axes setting.

sage: L = line([(1,2), (3,-4), (2, 5), (1,2)])

By default the axes are displayed.

sage: L.axes()
True

But we turn them off, and verify that they are off

sage: L.axes(False)
sage: L.axes()
False

Displaying L now shows a triangle but no axes.

sage: L

axes_color( self, [c=None])

Set the axes color.

If called with no input, return the current axes_color setting.

Input:

c
- an rgb color 3-tuple, where each tuple entry is a float between 0 and 1

We create a line, which has like everything a default axes color of black.

sage: L = line([(1,2), (3,-4), (2, 5), (1,2)])
sage: L.axes_color()
(0, 0, 0)

We change the axes color to red and verify the change.

sage: L.axes_color((1,0,0))
sage: L.axes_color()
(1.0, 0.0, 0.0)

When we display the plot, we'll see a blue triangle and bright red axes.

sage: L

axes_label_color( self, [c=None])

Set the color of the axes labels.

The axes labels are placed at the edge of the x and y axes, and are not on by default (use the axes_labels command to set them; see the example below). This function just changes their color.

Input:

c
- an rgb 3-tuple of numbers between 0 and 1

If called with no input, return the current axes_label_color setting.

We create a plot, which by default has axes label color black.

sage: p = plot(sin, (-1,1))
sage: p.axes_label_color()
(0, 0, 0)

We change the labels to be red, and confirm this:

sage: p.axes_label_color((1,0,0))
sage: p.axes_label_color()
(1.0, 0.0, 0.0)

We set labels, since otherwise we won't see anything.

sage: p.axes_labels(['$x$ axis', '$y$ axis'])

In the plot below, notice that the labels are red:

sage: p

axes_labels( self, [l=None])

Set the axes labels.

Input:

l
- (default: None) a list of two strings or None

Output: a 2-tuple of strings

If l is None, returns the current axes_labels, which is itself by default None. The default labels are both empty.

We create a plot and put x and y axes labels on it.

sage: p = plot(sin(x), (x, 0, 10))
sage: p.axes_labels(['x','y'])
sage: p.axes_labels()
('x', 'y')

Now when you plot p, you see x and y axes labels:

sage: p

axes_range( self, [xmin=None], [xmax=None], [ymin=None], [ymax=None])

Set the ranges of the $ x$ and $ y$ axes.

Input:

xmin, xmax, ymin, ymax
- floats

sage: L = line([(1,2), (3,-4), (2, 5), (1,2)])
sage: L.axes_range(-1, 20, 0, 2)
sage: L.xmin(), L.xmax(), L.ymin(), L.ymax()
(-1.0, 20.0, 0.0, 2.0)

axes_width( self, [w=None])

Set the axes width. Use this to draw a plot with really fat or really thin axes.

Input:

w
- a float

If called with no input, return the current axes_width setting.

We create a plot, see the default axes width (with funny Python float rounding), then reset the width to 10 (very fat).

sage: p = plot(cos, (-3,3))
sage: p.axes_width()
0.80000000000000004
sage: p.axes_width(10)
sage: p.axes_width()
10.0

Finally we plot the result, which is a graph with very fat axes.

sage: p

fontsize( self, [s=None])

Set the font size of axes labels and tick marks.

Input:

s
- integer, a font size in points.

If called with no input, return the current fontsize.

sage: L = line([(1,2), (3,-4), (2, 5), (1,2)])
sage: L.fontsize()
10
sage: L.fontsize(20)
sage: L.fontsize()
20

All the numbers on the axes will be very large in this plot:

sage: L

plot( self)

Draw a 2d plot this graphics object, which just returns this object since this is already a 2d graphics object.

sage: S = circle((0,0), 2)
sage: S.plot() is S
True

plot3d( self, [z=0])

Returns an embedding of this 2D plot into the xy-plane of 3D space, as a 3D plot object. An optional parameter z can be given to specify the z-coordinate.

sage: sum([plot(z*sin(x), 0, 10).plot3d(z) for z in range(6)]) #long

save( self, [filename=None], [xmin=None], [xmax=None], [ymin=None], [ymax=None], [figsize=(6, 3.7082039324993699)], [figure=None], [sub=None], [savenow=True], [dpi=100], [axes=None], [axes_labels=None], [fontsize=None], [frame=False], [verify=True], [aspect_ratio=None], [gridlines=None], [gridlinesstyle=None], [vgridlinesstyle=None], [hgridlinesstyle=None])

Save the graphics to an image file of type: PNG, PS, EPS, SVG, SOBJ, depending on the file extension you give the filename. Extension types can be: .png, .ps, .eps, .svg, and .sobj (for a Sage object you can load later).

sage: c = circle((1,1),1,rgbcolor=(1,0,0))
sage: c.show(xmin=-1,xmax=3,ymin=-1,ymax=3)

To correct the apect ratio of certain graphics, it is necessary to show with a 'figsize' of square dimensions.

sage: c.show(figsize=[5,5],xmin=-1,xmax=3,ymin=-1,ymax=3)

sage: point((-1,1),pointsize=30, rgbcolor=(1,0,0))

set_aspect_ratio( self, ratio)

Set the aspect ratio.

Input:

ratio
- a positive real number

We create a plot of a circle, and it doesn't look quite round:

sage: P = circle((1,1), 1); P

So we set the aspect ratio and now it is round:

sage: P.set_aspect_ratio(1)
sage: P

Note that the aspect ratio is inherited upon addition (which takes the max of aspect ratios of objects whose aspect ratio has been set):

sage: P + circle((0,0), 0.5)           # still square

In the following example, both plots produce a circle that looks twice as wide as tall:

sage: Q = circle((0,0), 0.5); Q.set_aspect_ratio(2)
sage: P + Q
sage: Q + P

show( self, [xmin=None], [xmax=None], [ymin=None], [ymax=None], [figsize=(6, 3.7082039324993699)], [filename=None], [dpi=100], [axes=None], [axes_labels=None], [frame=False], [fontsize=None], [aspect_ratio=None], [gridlines=None], [gridlinesstyle=None], [vgridlinesstyle=None], [hgridlinesstyle=None])

Show this graphics image with the default image viewer.

Optional input:

filename
- (default: None) string
dpi
- dots per inch
figsize
- [width, height]
aspect_ratio
- the perceived width divided by the perceived height. If the aspect ratio is set to 1, circles will look round. If it is set to 2 they will look twice as wide as they are tall. This is the aspect_ratio of the image, not of the frame that contains it. If you want to set the aspect ratio of the frame, use figsize.
axes
- (default: True)
axes_labels
- (default: None) list (or tuple) of two strings; the first is used as the label for the horizontal axis, and the second for the vertical axis.
fontsize
- (default: current setting - 10) positive integer; used for axes labels; if you make this very large, you may have to increase figsize to see all labels.
frame
- (default: False) draw a frame around the image
gridlines
- (default: None) can be any of the following: 1. None, False: do not add grid lines. 2. True, "automatic", "major": add grid lines at major ticks of the axes. 3. "minor": add grid at major and minor ticks. 4. [xlist,ylist]: a tuple or list containing two elements, where xlist (or ylist) can be any of the following. 4a. None, False: don't add horizontal (or vertical) lines. 4b. True, "automatic", "major": add horizontal (or vertical) grid lines at the major ticks of the axes. 4c. "minor": add horizontal (or vertical) grid lines at major and minor ticks of axes. 4d. an iterable yielding numbers n or pairs (n,opts), where n is the coordinate of the line and opt is a dictionary of MATPLOTLIB options for rendering the line. gridlinesstyle, hgridlinesstyle, vgridlinesstyle
- (default: None) a dictionary of MATPLOTLIB options for the rendering of the grid lines, the horizontal grid lines or the vertical grid lines, respectively.

sage: c = circle((1,1), 1, rgbcolor=(1,0,0))
sage: c.show(xmin=-1, xmax=3, ymin=-1, ymax=3)

To correct the apect ratio of certain graphics, it is necessary to show with a `figsize' of square dimensions.

sage: c.show(figsize=[5,5], xmin=-1, xmax=3, ymin=-1, ymax=3)

You can turn off the drawing of the axes:

sage: show(plot(sin,-4,4), axes=False)

You can also label the axes:

sage: show(plot(sin,-4,4), axes_labels=('x','y'))

You can turn on the drawing of a frame around the plots:

sage: show(plot(sin,-4,4), frame=True)

Add grid lines at the major ticks of t