Module: sage.plot.plot
SAGE provides 2-d plotting functionality with an interface inspired by the interface for plotting in Mathematica. The underlying rendering is mostly implemented using the matplotlib Python library.
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))
Next we construct the reflection of the above polygon about the
-axis by iterating over the qlist of first-coordinates of the first
graphic element of
(which is the actual Polygon; note that
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))
We combine together different graphics objects using "+":
sage: H = G + P + Q sage: H Graphics object consisting of 3 graphics primitives: 0 -- Line defined by 201 points 1 -- Polygon defined by 3 points 2 -- Polygon defined by 3 points
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)]
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.save('sage.png', xmin=-1.5, xmax=2, ymin=-1, ymax=1)
Author Log:
Module-level Functions
array, [filename=sage.png]) |
graphics_array
take a list of lists (or tuples)
of graphics objects and plots them all on one canvas (single plot).
Make some plots of
functions:
sage: f = lambda x: sin(x) sage: g = lambda x: sin(2*x) sage: h = lambda 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;
Ten you can type either: ga.show()
or ga.save()
.
sage: ga = 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: graphics_array([p1, p2]) Graphics Array of size 1 x 2
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 list of rgb intensities (r, g, b) All values are in range 0 to 1.
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: p = plot(sin, -2, 2, rgbcolor=hue(0.6))
x) |
Return True if x is a Graphics object.
sage: is_Graphics(1) False sage: is_Graphics(disk((0.0,0.0),1,0,90)) True
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 len(data)-1 and di is the ith data value, and puts points at those tuple values list_plot also takes a list of tuples (dxi, dyi) where dxi is the ith data representing the x-value, and dyi is the ith y-value if plotjoined=True, then a line spanning all the data is drawn instead
sage: l = list_plot([i^2 for i in range(5)]); l Graphics object consisting of 1 graphics primitives: 0 -- Point set defined by 5 point(s)
sage: r = [(random(),random()) for _ in range(20)]
Here are a bunch of random red points:
sage: list_plot(r,rgbcolor=(1,0,0)).save('sage.png')
This gives all the random points joined in a purple line:
sage: list_plot(r, plotjoined=True, rgbcolor=(1,0,1)).save('sage.png')
) |
Return the fractional and integer parts of x. Both results carry the sign of x. The integer part is returned as a real.
['f', 'g'], xmin, xmax) |
parametric plot takes two functions as a list or a tuple and make a plot with the first function as the x-value and the second function as the y-value
f = lambda x: sin(x) g = lambda x: sin(2*x) p3 = parametric_plot((f,g),0,2*pi,rgbcolor=hue(0.6))
v) |
c) |
Class: CircleFactory
sage: c = circle((1,1),1,rgbcolor=(1,0,0)) sage: c.save("sage.png", 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.save("sage.png", 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: for r in range(1,paths+1): ... for x,y in [((r+ocur)*cos(n), (r+ocur)*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.save("sage.png", xmin=-(paths+1)^2, xmax=(paths+1)^2, ymin=-(paths+1)^2, ymax=(paths+1)^2, figsize=[6,6])
Special Functions: __repr__,
_from_xdata_ydata,
_reset
Class: DiskFactory
Make some dangerous disks:
sage: bl = disk((0.0,0.0),1,180,270,rgbcolor=(1,1,0)) sage: tr = disk((0.0,0.0),1,0,90,rgbcolor=(1,1,0)) sage: tl = disk((0.0,0.0),1,90,180,rgbcolor=(0,0,0)) sage: br = disk((0.0,0.0),1,270,360,rgbcolor=(0,0,0)) sage: P = tl+tr+bl+br sage: P.save("sage.png",figsize=(4,4),xmin=-2,xmax=2,ymin=-2,ymax=2)
Special Functions: __repr__,
_from_xdata_ydata,
_reset
Class: FigureCanvasPS
Functions: basepath,
draw,
print_figure
) |
Return a nice string representation of the object. If the argument is a string, the return value is the same object.
self, outfile, [dpi=None], [facecolor=portrait], [edgecolor=w], [orientation=w], [papertype=72]) |
Render the figure to hardcopy. Set the figure patch face and edge colors. This is useful because some of the GUIs have a gray figure face color background and you'll probably want to override this on hardcopy
If outfile is a string, it is interpreted as a file name. If the extension matches .ep* write encapsulated postscript, otherwise write a stand-alone PostScript file.
If outfile is a file object, a stand-alone PostScript file is written into this file object.
Special Functions: _print_figure_tex
self, outfile, dpi, facecolor, edgecolor, orientation, papertype) |
If text.usetex is True in rc, a temporary pair of tex/eps files are created to allow tex to manage the text layout via the PSFrags package. These files are processed to yield the final ps or eps file.
Class: FigureCanvasSVG
Functions: print_figure
Class: GraphicPrimitive
Special Functions: __repr__
Class: GraphicPrimitive_Circle
self, x, y, r, options) |
Special Functions: __repr__,
_render_on_subplot
Class: GraphicPrimitive_Disk
self, point, r, theta1, theta2, options) |
Special Functions: __repr__,
_render_on_subplot
Class: GraphicPrimitive_Line
self, xdata, ydata, options) |
Special Functions: __append__,
__getitem__,
__len__,
__repr__,
__setitem__,
_render_on_subplot
Class: GraphicPrimitive_Point
self, xdata, ydata, options) |
Special Functions: __getitem__,
__repr__,
__setitem__,
_render_on_subplot
Class: GraphicPrimitive_Polygon
self, xdata, ydata, options) |
Special Functions: __append__,
__getitem__,
__len__,
__repr__,
__setitem__,
_render_on_subplot
Class: GraphicPrimitive_Text
self, string, point, options) |
Special Functions: __repr__,
_render_on_subplot
Class: GraphicPrimitiveFactory
self) |
Functions: reset
Special Functions: _coerce
Class: GraphicPrimitiveFactory_circle
Special Functions: __call__
Class: GraphicPrimitiveFactory_disk
Special Functions: __call__
Class: GraphicPrimitiveFactory_from_point_list
Special Functions: __call__
Class: GraphicPrimitiveFactory_point
Special Functions: __call__
Class: GraphicPrimitiveFactory_text
Special Functions: __call__
Class: Graphics
sage: G = Graphics(); G Graphics object consisting of 0 graphics primitives: sage: c = circle((1,1), 1) sage: G+=c; G Graphics object consisting of 1 graphics primitives: 0 -- Circle defined by (1.0,1.0) with r=1.0
Here we make a graphic of embeded isoceles 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.save('sage.png',figsize=[5,5])
self) |
Functions: save,
show,
xmax,
xmin,
ymax,
ymin
self, [filename=None], [xmin=True], [xmax=None], [ymin=None], [ymax=[5, 4]], [figsize=None], [fig=None], [sub=None], [savenow=None], [dpi=sage.png]) |
Save the graphics to an image file of type: PNG, PS, or SVG, depending on the file extension you give the filename. Extension types can be: '.png', '.ps', '.svg'
sage: c = circle((1,1),1,rgbcolor=(1,0,0)) sage: c.save("sage.png", 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.save("sage.png", figsize=[5,5],xmin=-1,xmax=3,ymin=-1,ymax=3)
self, [xmin=None], [xmax=[5, 4]], [ymin=None], [ymax=None], [figsize=None], [filename=None]) |
Show a graphics image with default image viewer. (Current implementation is hackish)
sage: c = circle((1,1), 1, rgbcolor=(1,0,0)) sage: c.save("sage.png", 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.save("sage.png", figsize=[5,5], xmin=-1, xmax=3, ymin=-1, ymax=3)
self) |
sage: G = Graphics(); G Graphics object consisting of 0 graphics primitives: sage: G.xmax() 1
self) |
sage: G = Graphics(); G Graphics object consisting of 0 graphics primitives: sage: G.xmin() -1
self) |
sage: G = Graphics(); G Graphics object consisting of 0 graphics primitives: sage: G.ymax() 1
self) |
sage: G = Graphics(); G Graphics object consisting of 0 graphics primitives: sage: G.ymin() -1
Special Functions: __add__,
__delitem__,
__getitem__,
__len__,
__radd__,
__setitem__,
_add_xy_axes,
_circle,
_disk,
_extend_axes,
_extend_x_axis,
_extend_y_axis,
_line,
_point,
_polygon,
_prepare_axes,
_repr_,
_text
self, other) |
If you have any Graphics object G1, you can always add any other amount of Graphics objects G2,G3,... to form a new Graphics object: G4 = G1 + G2 + G3
sage: h1 = lambda x : sqrt(x^3 - 1) sage: h2 = lambda x : -sqrt(x^3 - 1) sage: g1 = plot(h1, 1, 5) sage: g2 = plot(h2, 1, 5) sage: h = g1 + g2; h Graphics object consisting of 2 graphics primitives: 0 -- Line defined by 205 points 1 -- Line defined by 205 points
self, i) |
If G is of type Graphics, then del(G[i]) removes the ith distinct graphic primitive making up that object.
sage: G = circle((1,1),1) + circle((1,2),1) + circle((1,2),5); G Graphics object consisting of 3 graphics primitives: 0 -- Circle defined by (1.0,1.0) with r=1.0 1 -- Circle defined by (1.0,2.0) with r=1.0 2 -- Circle defined by (1.0,2.0) with r=5.0 sage: len(G) 3 sage: del(G[2]) sage: G Graphics object consisting of 2 graphics primitives: 0 -- Circle defined by (1.0,1.0) with r=1.0 1 -- Circle defined by (1.0,2.0) with r=1.0 sage: len(G) 2
self, i) |
Returns the ith graphics primitive object:
sage: G = circle((1,1),2) + circle((2,2),5); G Graphics object consisting of 2 graphics primitives: 0 -- Circle defined by (1.0,1.0) with r=2.0 1 -- Circle defined by (2.0,2.0) with r=5.0 sage: G[1] Circle defined by (2.0,2.0) with r=5.0
self) |
If G is of type Graphics, then len(G) gives the number of distinct graphics primitives making up that object.
sage: G = circle((1,1),1) + circle((1,2),1) + circle((1,2),5); G Graphics object consisting of 3 graphics primitives: 0 -- Circle defined by (1.0,1.0) with r=1.0 1 -- Circle defined by (1.0,2.0) with r=1.0 2 -- Circle defined by (1.0,2.0) with r=5.0 sage: len(G) 3
self, i, x) |
You can replace an GraphicsPrimitive (point, line, circle, etc...) in a Graphics object G with any other GraphicsPrimitive
sage: G = circle((1,1),1) + circle((1,2),1) + circle((1,2),5); G Graphics object consisting of 3 graphics primitives: 0 -- Circle defined by (1.0,1.0) with r=1.0 1 -- Circle defined by (1.0,2.0) with r=1.0 2 -- Circle defined by (1.0,2.0) with r=5.0
sage: p = polygon([[1,3],[2,-2],[1,1],[1,3]]);p Graphics object consisting of 1 graphics primitives: 0 -- Polygon defined by 4 points
sage: G[1] = p[0];G Graphics object consisting of 3 graphics primitives: 0 -- Circle defined by (1.0,1.0) with r=1.0 1 -- Polygon defined by 4 points 2 -- Circle defined by (1.0,2.0) with r=5.0
Class: GraphicsArray
self, array) |
Functions: ncols,
nrows,
save,
show
self, [filename=sage.png]) |
save the graphics_array
to
(for now) a png called 'filename'.
self, [filename=None]) |
show the graphics_array
in
the users browser.
Special Functions: __append__,
__getitem__,
__len__,
__setitem__,
_render,
_repr_
self, filename) |
render
loops over all graphics objects
in the array and adds them to the subplot.
Class: LineFactory
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.
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: p = line(L, rgbcolor=(1/4,1/8,3/4))
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: p = 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: p = 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: p = 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: p = 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: p = line(L, rgbcolor=(1/4,3/4,1/8))
A red plot of the Jacobi elliptic function
sn
,
:
sage: L = [(i/100.0, maxima.eval('jacobi_sn (%s/100.0,2.0)'%i)) for i in range(-300,300)] sage: p = line(L, rgbcolor=(3/4,1/4,1/8))
A red plot of
-Bessel function
,
:
sage: L = [(i/10.0, maxima.eval('bessel_j (2,%s/10.0)'%i)) for i in range(100)] sage: p = line(L, rgbcolor=(3/4,1/4,5/8))
A purple plot of the Riemann zeta function
,
:
(much better that the gnuplot version!):
sage: L = [[(pari(1/2 + i*I/10).zeta().real()).precision(1),(pari(1/2 + i*I/10).zeta().imag()).precision(1)] for i in range (0,300)] sage: p = line(L, rgbcolor=(3/4,1/2,5/8))
A purple plot of the Hasse-Weil
-function
,
:
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: p = line(L, rgbcolor=(3/4,1/2,5/8))
A red, blue, and green "cool cat":
sage: ncos = lambda x: -cos(x) sage: G = plot(ncos, -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: H = G + P + Q
Special Functions: __repr__,
_from_xdata_ydata,
_reset
Class: PlotFactory
We draw the graph of an elliptic curve as the union of graphs of 2 functions.
sage: def h1(x): return sqrt(x^3 - 1) sage: def h2(x): return -sqrt(x^3 - 1) sage: plot([h1, h2], 1,4) Graphics object consisting of 2 graphics primitives: 0 -- Line defined by 204 points 1 -- Line defined by 204 points
Special Functions: __call__,
__repr__,
_reset
Class: PointFactory
A purple point from a single tuple or coordinates:
sage: p1 = point((0.5,0.5), rgbcolor=hue(0.75))
Here are some random larger red points, given as a list of tuples
sage: p2 = point(((0.5,0.5),(1,2),(0.5,0.9),(-1,-1)),rgbcolor=hue(1),pointsize=30)
Special Functions: __repr__,
_from_xdata_ydata,
_reset
Class: PolygonFactory
We create a purple-ish polygon:
sage: polygon([[1,2], [5,6], [5,0]], rgbcolor=(1,0,1)) Graphics object consisting of 1 graphics primitives: 0 -- Polygon defined by 3 points
Some modern art - a random polygon:
sage: v = [(randrange(-5,5), randrange(-5,5)) for _ in range(10)] sage: polygon(v) Graphics object consisting of 1 graphics primitives: 0 -- Polygon defined by 10 points
A purple hexagon:
sage: L = [[cos(pi*i/3),sin(pi*i/3)] for i in range(6)] sage: p = polygon(L, rgbcolor=(1,0,1))
A green limacon of Pascal:
sage: L = [[1+cos(pi*i/100)-cos(pi*i/50),sin(pi*i/100)-sin(pi*i/50)] for i in range(200)] sage: p = polygon(L, rgbcolor=(1/4,1,1/2))
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: p = polygon(L, rgbcolor=(1/8,3/4,1/2))
A blue figure 8:
sage: L = [[2*cos(pi*i/100)*sqrt(1-sin(pi*i/100)^2),2*sin(pi*i/100)*sqrt(1-sin(pi*i/100)^2)] for i in range(200)] sage: p = polygon(L, rgbcolor=(1/8,1/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: p = 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: p = 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: p = 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: p = polygon(L, rgbcolor=(3/4,1/4,1/4))
A blue 8-leaved petal:
sage: L = [[sin(5*pi*i/100)^2*cos(pi*i/100)^3,sin(5*pi*i/100)^2*sin(pi*i/100)] for i in range(200)] sage: p = polygon(L, rgbcolor=(1/3,1/2,3/5))
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: p = 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: p = polygon(L, rgbcolor=(1,1/4,1/2))
AUTHORS: - David Joyner (2006-04-14): the long list of examples above.
Special Functions: __repr__,
_from_xdata_ydata,
_reset
Class: TextFactory
Type this to see some text in top right plane:
sage: t = text("SAGE is really neat!!",(2,12))
Type this to see the same text in larger font and colored red:
sage: t = text("SAGE is really neat!!",(2,12),fontsize=20,rgbcolor=(1,0,0))
You can also center text differently:
sage: t1 = text("Hello",(1,1), vertical_alignment="top") sage: t2 = text("World", (1,0.5), horizontal_alignment="left")
Special Functions: __repr__,
_from_xdata_ydata,
_reset
See About this document... for information on suggesting changes.