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
-axis by iterating over the list 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)) 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: P = plot(lambda x: sin(x)/x, -4,4, rgbcolor=(0,0,1)) + \ ... plot(lambda x: x*cos(x), -4,4, rgbcolor=(1,0,0)) + \ ... plot(lambda x: 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 = lambda x: (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
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)
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.....
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:
Make some plots of
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()
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:
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))
x) |
Return True if
is a Graphics object.
sage: is_Graphics(1) False sage: is_Graphics(disk((0.0, 0.0), 1, (0, pi/2))) True
data, [plotjoined=False]) |
list_plot
takes a single list of data, in which case it forms a
list of tuples
where
goes from 0 to
and
is
the
th data value, and puts points at those tuple values.
list_plot
also takes a list of tuples
where
is the
th data representing the
-value, and
is the
th
-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))
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:
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)
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
coordinates, the second function giving the
coordinates, and the
third function (if present) giving the
coordinates.
Input:
We draw a 2d parametric plot:
sage: t = var('t') sage: parametric_plot( (sin(t), sin(2*t)), 0, 2*pi, rgbcolor=hue(0.6) )
We draw a 3d parametric plot:
sage: parametric_plot3d( (5*cos(x), 5*sin(x), x), (-12, 12), plot_points=150, color="red")
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(lambda x:sin(5*x)^2, 0, 2*pi, rgbcolor=hue(0.6))
A red figure-8:
sage: polar_plot(lambda x:abs(sqrt(1 - sin(x)^2)), 0, 2*pi, rgbcolor=hue(1.0))
A green limacon of Pascal:
sage: polar_plot(lambda x:2 + 2*cos(x), 0, 2*pi, rgbcolor=hue(0.3))
n, [format=hex]) |
Given an integer
, 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)]
v, n, m) |
v, xrange, yrange, plot_points) |
Input:
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))
[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)
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]
c) |
Convert a tuple or string to a matplotlib rgb color tuple.
Input:
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)
v, plot_points) |
Input:
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])
Class: ArrowFactory
An arrow from (xmin, ymin) to (xmax, ymax).
A straight, black 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=0.05)
Special Functions: __repr__,
_from_xdata_ydata,
_reset
self) |
Returns a string representation of this ArrowFactory object.
TESTS:
sage: arrow type arrow? for help and examples
Class: BarChartFactory
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))
Special Functions: __repr__,
_from_xdata_ydata,
_reset
self) |
Returns a string representation of this BarChartFactory object.
TESTS:
sage: bar_chart type bar_chart? for help and examples
Class: CircleFactory
circle.options
to see all options
circle(center, radius, **kwds)
Input:
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])
Special Functions: __repr__,
_from_xdata_ydata,
_reset
self) |
Returns a string representation of this CircleFactory object.
TESTS:
sage: circle type circle? for help and examples
Class: ContourPlotFactory
contour_plot
takes a function of two variables,
and plots contour lines of the function over the specified
xrange and yrange as demonstrated below.
contour_plot(f, (xmin, xmax), (ymin, ymax), ...)
Input:
Here we plot a simple function of two variables:
sage: f = lambda x,y: cos(x^2 + y^2) sage: contour_plot(f, (-4, 4), (-4, 4))
Here we change the ranges and add some options:
sage: h = lambda x,y: (x^2)*cos(x*y) sage: contour_plot(h, (-10, 5), (-5, 5), fill=False, plot_points=100)
An even more complicated plot.
sage: f = lambda x,y: sin(x^2 + y^2)*cos(x)*sin(y) sage: contour_plot(f, (-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: x, y = var('x,y') sage: contour_plot(y^2 + 1 - x^3 - x, (y,-pi,pi), (x,-pi,pi)) sage: contour_plot(lambda x,y: 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 = lambda x,y: 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)
Special Functions: __repr__,
_from_xdata_ydata,
_reset
self) |
Returns a string representation of this ContourPlotFactory object.
TESTS:
sage: contour_plot type contour_plot? for help and examples
Class: DiskFactory
A disk at a point =
with radius =
spanning (in radians) angle=
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)
Special Functions: __repr__,
_from_xdata_ydata,
_reset
self) |
Returns a string representation of this DiskFactory object.
TESTS:
sage: disk type disk? for help and examples
Class: GraphicPrimitive
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'>
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
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_
self) |
Return the allowed options for a graphics primitive.
Output:
sage: from sage.plot.plot import GraphicPrimitive sage: GraphicPrimitive({})._allowed_options() {}
self, [options=None]) |
Translate 2d plot options into 3d plot options.
self) |
String representation of this graphics primitive.
sage: from sage.plot.plot import GraphicPrimitive sage: GraphicPrimitive({})._repr_() 'Graphics primitive'
Class: GraphicPrimitive_Arrow
We crate 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,2.0)
self, xmin, ymin, xmax, ymax, 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) to (2,3)
Functions: plot3d
self) |
sage: arrow((0,0),(1,1)).plot3d()
Special Functions: __init__,
_allowed_options,
_plot3d_options,
_render_on_subplot,
_repr_
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())) [('hue', 'The color given as a hue.'), ('rgbcolor', 'The color as an rgb tuple.'), ('width', 'How wide the entire arrow is.')]
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))
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) to (2,3)'
Class: GraphicPrimitive_BarChart
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'>
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_
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')]
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])
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
self, x, y, r, options) |
Functions: 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
self, xy_data_array, xrange, yrange, options) |
Special Functions: __init__,
_allowed_options,
_render_on_subplot,
_repr_
Class: GraphicPrimitive_Disk
self, point, r, angle, options) |
Special Functions: __init__,
_allowed_options,
_render_on_subplot,
_repr_
Class: GraphicPrimitive_Line
sage: from sage.plot.plot import GraphicPrimitive_Line sage: GraphicPrimitive_Line([1,2,7], [1,5,-1], {}) Line defined by 3 points
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
self) |
sage: EllipticCurve('37a').plot(thickness=5).plot3d()
Special Functions: __getitem__,
__init__,
__len__,
__setitem__,
_allowed_options,
_plot3d_options,
_render_on_subplot,
_repr_
self, i) |
Extract the i-th element of the line (which is stored as a list of points).
Input:
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)]
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
self, i, point) |
Set the i-th element of this line (really a sequence of lines through given points).
Input:
We create a line graphics object
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
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.')]
self, subplot) |
Render this line on a matplotlib subplot.
Input:
This implicitly calls this function:
sage: line([(1,2), (3,-4), (2, 5), (1,2)])
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
self, xy_data_array, xrange, yrange, options) |
Special Functions: __init__,
_allowed_options,
_render_on_subplot,
_repr_
Class: GraphicPrimitive_NetworkXGraph
Input:
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()})
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
self, xpos_array, ypos_array, xvec_array, yvec_array, options) |
Special Functions: __init__,
_allowed_options,
_render_on_subplot,
_repr_
Class: GraphicPrimitive_Point
self, xdata, ydata, options) |
Functions: plot3d
self) |
sage: E = EllipticCurve('37a') sage: P = E(0,0) sage: def get_points(n): return sum([point(i*P, 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
self, xdata, ydata, options) |
Functions: 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
self, string, point, options) |
Functions: plot3d
Special Functions: __init__,
_allowed_options,
_plot3d_options,
_render_on_subplot,
_repr_
Class: GraphicPrimitiveFactory
self) |
Functions: reset
Special Functions: __init__,
_coerce,
_graphic3d
self) |
Return 3d version of this graphics primitive.
We call this if the user tries to create a graphic but gives points (etc) in 3-space instead of in the plane.
Class: GraphicPrimitiveFactory_arrow
Special Functions: __call__
Class: GraphicPrimitiveFactory_bar_chart
Special Functions: __call__
Class: GraphicPrimitiveFactory_circle
Special Functions: __call__
Class: GraphicPrimitiveFactory_contour_plot
Special Functions: __call__
Class: GraphicPrimitiveFactory_disk
Special Functions: __call__
Class: GraphicPrimitiveFactory_from_point_list
Special Functions: __call__
Class: GraphicPrimitiveFactory_matrix_plot
Special Functions: __call__
Class: GraphicPrimitiveFactory_plot_field
Special Functions: __call__
Class: GraphicPrimitiveFactory_points
Special Functions: __call__
Class: GraphicPrimitiveFactory_text
Special Functions: __call__
Class: Graphics
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])
self) |
Create a new empty Graphics objects with all the defaults.
sage: G = Graphics()
Functions: 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
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
self, [show=None]) |
Set whether or not the
and
axes are shown by default.
Input:
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
self, [c=None]) |
Set the axes color.
If called with no input, return the current axes_color setting.
Input:
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
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:
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
self, [l=None]) |
Set the axes labels.
Input:
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
self, [xmin=None], [xmax=None], [ymin=None], [ymax=None]) |
Set the ranges of the
and
axes.
Input:
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)
self, [w=None]) |
Set the axes width. Use this to draw a plot with really fat or really thin axes.
Input:
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
self, [s=None]) |
Set the font size of axes labels and tick marks.
Input:
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
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
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)])
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]) |
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)
self, ratio) |
Set the aspect ratio.
Input:
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
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]) |
Show this graphics image with the default image viewer.
Optional input:
frame - (default: False) draw a frame around the image
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)
self, [c=None]) |
Set the color of the axes tick labels.
Input:
If called with no input, return the current tick_label_color setting.
sage: p = plot(cos, (-3,3)) sage: p.tick_label_color() (0, 0, 0) sage: p.tick_label_color((1,0,0)) sage: p.tick_label_color() (1.0, 0.0, 0.0) sage: p
self, [new=None]) |
sage: G = Graphics(); print G Graphics object consisting of 0 graphics primitives sage: G.xmax() 1 sage: G.xmax(2) 2.0 sage: G.xmax() 2.0
self, [new=None]) |
sage: G = Graphics(); print G Graphics object consisting of 0 graphics primitives sage: G.xmin() -1 sage: G.xmax(2) 2.0 sage: G.xmax() 2.0
self, [new=None]) |
sage: G = Graphics(); print G Graphics object consisting of 0 graphics primitives sage: G.ymax() 1 sage: G.ymax(2) 2.0 sage: G.ymax() 2.0
self, [new=None]) |
sage: G = Graphics(); print G Graphics object consisting of 0 graphics primitives sage: G.ymin() -1 sage: G.ymin(2) 2.0 sage: G.ymin() 2.0
Special Functions: __add__,
__delitem__,
__getitem__,
__init__,
__len__,
__radd__,
__setitem__,
__str__,
_arrow,
_bar_chart,
_circle,
_contour_plot,
_disk,
_extend_axes,
_extend_x_axis,
_extend_y_axis,
_line,
_matrix_plot,
_plot_field,
_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.
The xmin, xmax, ymin, and ymax properties of the graphics objects are expanded to include all objects in both scenes. If the aspect ratio property of either or both objects are set, then the larger aspect ratio is chosen.
sage: g1 = plot(abs(sqrt(x^3-1)), (x,1,5)) sage: g2 = plot(-abs(sqrt(x^3-1)), (x,1,5), rgbcolor=(1,0,0)) sage: g1 + g2 # displays the plot
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); print G Graphics object consisting of 3 graphics primitives sage: len(G) 3 sage: del(G[2]) sage: print G Graphics object consisting of 2 graphics primitives sage: len(G) 2
self, i) |
Returns the ith graphics primitive object:
sage: G = circle((1,1),2) + circle((2,2),5); print G Graphics object consisting of 2 graphics primitives 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); print G Graphics object consisting of 3 graphics primitives sage: len(G) 3
self, other) |
Compute and return other + this graphics object.
This only works when other is a Python int equal to 0. In all other cases a TypeError is raised. The main reason for this function is to make suming a list of graphics objects easier.
sage: S = circle((0,0), 2) sage: print int(0) + S Graphics object consisting of 1 graphics primitive sage: print S + int(0) Graphics object consisting of 1 graphics primitive
The following would fail were it not for this function:
sage: v = [circle((0,0), 2), circle((2,3), 1)] sage: print sum(v) Graphics object consisting of 2 graphics primitives
self, i, x) |
You can replace a GraphicPrimitive (point, line, circle, etc...) in a Graphics object G with any other GraphicPrimitive
sage: G = circle((1,1),1) + circle((1,2),1) + circle((1,2),5); print G Graphics object consisting of 3 graphics primitives
sage: p = polygon([[1,3],[2,-2],[1,1],[1,3]]); print p Graphics object consisting of 1 graphics primitive
sage: G[1] = p[0] sage: G # show the plot
self) |
Return string representation of this plot.
sage: S = circle((0,0), 2); S.__str__() 'Graphics object consisting of 1 graphics primitive' sage: print S Graphics object consisting of 1 graphics primitive
WARNING: __str__
is not called when printing lists of graphics
objects, which can be confusing, since they will all pop up. One
workaround is to call show_default
:
For example, below when we do print v
two plots are displayed:
sage: v = [circle((0,0), 2), circle((2,3), 1)] sage: print v [, ]
However, if we call show_default
then we see the text representations
of the graphics:
sage: show_default(False) sage: print v [Graphics object consisting of 1 graphics primitive, Graphics object consisting of 1 graphics primitive] sage: v [Graphics object consisting of 1 graphics primitive, Graphics object consisting of 1 graphics primitive]
sage: show_default(True)
self, xmin, ymin, xmax, ymax, options) |
Add an arrow with given bounding box to this graphics object.
(For internal use - you should just use addition.)
Input:
This will display a bold green arrow going up and to the right.
sage: S = circle((0,0), 2) sage: S._arrow(0,0,5,5, {'width':0.2, 'rgbcolor':(0,1,0)}); S
self, ind, datalist, xrange, yrange, options) |
Add a bar chart to this graphics objects.
(For internal use - you should just use addition.)
Input:
sage: S = circle((0,0), 2) sage: S._bar_chart(range(4), [1,3,2,0], (0,4), (0,3), {'width': 0.5, 'rgbcolor': (0, 0, 1)}) sage: S
self, x, y, r, options) |
Add a circle to this graphics object.
(For internal use - you should just use addition.)
Input:
We inscribe a circle in another circle:
sage: S = circle((0,0), 2) sage: S._circle(1,0,1, {'rgbcolor':(0.5,0,1), 'thickness':1, 'fill':True, 'alpha':1}) sage: S.show(aspect_ratio=1, axes=False)
self, xy_data_array, xrange, yrange, options) |
Add a countor plot to this graphics object.
(For internal use - you should just use addition.)
Input: xy_data_array - xrange, yrange -
self, point, r, angle, options) |
Add a disk to this graphics object.
(For internal use - you should just use addition.)
Input:
self, xmin, xmax, ymin, ymax) |
Extend both the x and y axis so that the x-axis contains both xmin and xmax, and the y-axis contains by ymin and ymax.
sage: S = circle((0,0), 2) sage: S._extend_axes(-2, 3, -5, 1) sage: S.xmin(), S.xmax(), S.ymin(), S.ymax() (-2.0, 3, -5, 2.0)
self, x) |
Extend the x axis range so that it contains x.
sage: S = circle((0,0), 2) sage: S.xmin(), S.xmax() (-2.0, 2.0) sage: S._extend_x_axis(1) sage: S.xmin(), S.xmax() (-2.0, 2.0) sage: S._extend_x_axis(-5) sage: S.xmin(), S.xmax() (-5, 2.0) sage: S._extend_x_axis(5) sage: S.xmin(), S.xmax() (-5, 5)
self, y) |
Extend the y axis range so that it contains y.
sage: S = circle((0,0), 2) sage: S.ymin(), S.ymax() (-2.0, 2.0) sage: S._extend_y_axis(1) sage: S.ymin(), S.ymax() (-2.0, 2.0) sage: S._extend_y_axis(-5) sage: S.ymin(), S.ymax() (-5, 2.0) sage: S._extend_y_axis(5) sage: S.ymin(), S.ymax() (-5, 5)
self, xdata, ydata, options) |
Add a line to this graphics object.
(For internal use - you should just use addition.)
Input:
self, xy_data_array, xrange, yrange, options) |
Add a matrix plot to this graphics object.
(For internal use - you should just use addition.)
Input: xy_data_array - xrange, yrange -
self, xpos_array, ypos_array, xvec_array, yvec_array, xrange, yrange, options) |
Add a vector field plot to this graphics object.
Input:
xrange, yrange - options - dictionary of options
self, xdata, ydata, options) |
Add a plot of a point or list of points to this graphics object.
(For internal use - you should just use addition.)
Input: xy_data_array - xrange, yrange -
self, xdata, ydata, options) |
Add a plot of a polygon to this graphics object.
(For internal use - you should just use addition.)
Input:
self, xmin, xmax, ymin, ymax) |
Perform various manipulations on the axes ranges so that the ranges look OK, e.g., they are 10 percent bigger than all graphics object.
Input:
sage: P = line([(-1,-2), (3,5)]) sage: P._prepare_axes(-1,-2, 3, 10) (-2.1000000000000001, -0.89000000000000001, 2.2999999999999998, 10.77) sage: P._prepare_axes(-1,-2, None, 10) (-2.1000000000000001, -0.89000000000000001, -3.2000000000000002, 11.32)
self) |
Show this graphics objects.
If the show_default
function has been called with True
(the default), then you'll see this graphics object displayed.
Otherwise you'll see a text representation of it.
We create a plot and call _repr_
on it, which causes it
to be displayed as a plot:
sage: P = plot(cos, (-1,1)) sage: P._repr_() ''
Just doing this also displays the plot:
sage: P
Note that printing P with the print
statement does not display the plot:
sage: print P Graphics object consisting of 1 graphics primitive
Now we turn off showing plots by default:
sage: show_default(False)
Now we just get a string. To show P you would have to do show(P)
.
sage: P._repr_() 'Graphics object consisting of 1 graphics primitive' sage: P Graphics object consisting of 1 graphics primitive
Finally, we turn show_default
back on:
sage: show_default(True)
self, string, point, options) |
Add a countor plot to this graphics object.
(For internal use - you should just use addition.)
Input:
Class: GraphicsArray
self, array) |
Functions: append,
ncols,
nrows,
save,
show
self, [filename=None], [dpi=100], [figsize=(6, 3.7082039324993699)], [axes=None]) |
save the graphics_array
to
(for now) a png called 'filename'.
self, [filename=None], [dpi=100], [figsize=(6, 3.7082039324993699)], [axes=None]) |
Show this graphics array using the default viewer.
Optional input:
Special Functions: __getitem__,
__init__,
__len__,
__set_figsize__,
__setitem__,
__str__,
_render,
_repr_
self, filename, [dpi=None], [figsize=(6, 3.7082039324993699)], [axes=None]) |
render
loops over all graphics objects
in the array and adds them to the subplot.
Class: ImplicitPlotFactory
implicit_plot
takes a function of two variables, implicit_plot(f, (xmin, xmax), (ymin, ymax), ...)
Input:
We can define a level-
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)
Special Functions: __repr__,
_reset
self) |
Returns a string representation of this ImplicitPlotFactory object.
TESTS:
sage: implicit_plot type implicit_plot? for help and examples
self) |
Sets the default options for this ImplicitPlotFactory object.
TESTS:
sage: implicit_plot._reset() sage: implicit_plot.options['contours'] (0.0,)
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.
Input:
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
,
:
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
-Bessel function
,
:
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
,
:
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
-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: 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)])
Special Functions: __repr__,
_from_xdata_ydata,
_graphic3d,
_reset
self) |
Returns a string representation of this LineFactory object.
TESTS:
sage: line type line? for help and examples
Class: MatrixPlotFactory
Each (
th,
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
(
th,
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')
Special Functions: __repr__,
_from_xdata_ydata,
_reset
self) |
Returns a string representation of this MatrixPlotFactory object.
TESTS:
sage: matrix_plot type matrix_plot? for help and examples
Class: PlotFactory
plot(X, ...)
where
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 - the number of points to initially plot before doing adaptive refinement plot_division - the maximum number of subdivisions to introduce in adaptive refinement. max_bend - parameter that affects adaptive refinement 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
(these are the same as for the line function):
Input:
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
,
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 200 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 80 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 plot_division=0.
sage: p=plot(lambda x: 1, (x,0,3), plot_points=4, randomize=False, plot_division=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
wiggles wildly near 0
, so the
first plot below won't look perfect. Sage adapts to this
and plots extra points near the origin.
sage: plot(sin(1/x), (x, -1, 1))
With the plot_points
option you can increase the number
of sample points, to obtain a more accurate plot.
sage: plot(sin(1/x), (x, -1, 1), plot_points=1000)
Note that the independent variable may be omitted if there is no ambiguity:
sage: plot(sin(1/x), (-1, 1), plot_points=1000)
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
Special Functions: __call__,
__repr__,
_call,
_reset
self) |
Returns a string representation of this PlotFactory object.
TESTS:
sage: plot type plot? for help and examples
Class: PlotFieldFactory
plot_vector_field
takes two functions of two variables,
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: plot_vector_field((lambda x,y: .01*x,x+y), (-10,10), (-10,10))
Special Functions: _from_xdata_ydata,
_repr_,
_reset
Class: PointFactory
A point of size `pointsize' defined by point =
.
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)
Special Functions: __repr__,
_from_xdata_ydata,
_reset
self) |
Returns a string representation of this PointFactory object.
TESTS:
sage: point type point? for help and examples
Class: PolygonFactory
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))
Author: David Joyner (2006-04-14): the long list of examples above.
Special Functions: __repr__,
_from_xdata_ydata,
_reset
self) |
Returns a string representation of this PolygonFactory object.
TESTS:
sage: polygon Sage polygon; type polygon? for help and examples
Class: TextFactory
Returns a 2d or 3d text graphics object at the point
Type text.options
for a dictionary of options for 2d text. The 3d options
are as for other 3d graphics objects (i.e., mainly just rgbcolor at present).
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.
3D OPTIONS: rgbcolor - the color of the text
Some 2d text:
sage: text("Sage is really neat!!",(2,12))
Some 2d 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')
The same text, but in 3d:
sage: text("Sage is really neat!!",(2,12,1))
The same text in larger font and colored red:
sage: text("Sage is really neat!!",(2,12),fontsize=20,rgbcolor=(1,0,0))
And in 3d in two places:
sage: text("Sage is...",(2,12,1), rgbcolor=(1,0,0)) + text("quite powerful!!",(4,10,0), rgbcolor=(0,0,1))
You can also align 2d 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
Special Functions: __repr__,
_from_xdata_ydata,
_reset
self) |
Returns a string representation of this TextFactory object.
TESTS:
sage: text type text? for help and examples
See About this document... for information on suggesting changes.