def line2d(points, **options): r""" Create the line through the given list of points. INPUT: - ``points`` - either a single point (as a tuple), a list of points, a single complex number, or a list of complex numbers. Type ``line2d.options`` for a dictionary of the default options for lines. You can change this to change the defaults for all future lines. Use ``line2d.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 - ``legend_color`` -- The color of the text in the legend - ``legend_label`` -- the label for this item in the legend Any MATPLOTLIB line option may also be passed in. E.g., - ``linestyle`` - (default: "-") The style of the line, which is one of - ``"-"`` or ``"solid"`` - ``"--"`` or ``"dashed"`` - ``"-."`` or ``"dash dot"`` - ``":"`` or ``"dotted"`` - ``"None"`` or ``" "`` or ``""`` (nothing) The linestyle can also be prefixed with a drawing style (e.g., ``"steps--"``) - ``"default"`` (connect the points with straight lines) - ``"steps"`` or ``"steps-pre"`` (step function; horizontal line is to the left of point) - ``"steps-mid"`` (step function; points are in the middle of horizontal lines) - ``"steps-post"`` (step function; horizontal line is to the right of point) - ``marker`` - The style of the markers, which is one of - ``"None"`` or ``" "`` or ``""`` (nothing) -- default - ``","`` (pixel), ``"."`` (point) - ``"_"`` (horizontal line), ``"|"`` (vertical line) - ``"o"`` (circle), ``"p"`` (pentagon), ``"s"`` (square), ``"x"`` (x), ``"+"`` (plus), ``"*"`` (star) - ``"D"`` (diamond), ``"d"`` (thin diamond) - ``"H"`` (hexagon), ``"h"`` (alternative hexagon) - ``"<"`` (triangle left), ``">"`` (triangle right), ``"^"`` (triangle up), ``"v"`` (triangle down) - ``"1"`` (tri down), ``"2"`` (tri up), ``"3"`` (tri left), ``"4"`` (tri right) - ``0`` (tick left), ``1`` (tick right), ``2`` (tick up), ``3`` (tick down) - ``4`` (caret left), ``5`` (caret right), ``6`` (caret up), ``7`` (caret down) - ``"$...$"`` (math TeX string) - ``markersize`` -- the size of the marker in points - ``markeredgecolor`` -- the color of the marker edge - ``markerfacecolor`` -- the color of the marker face - ``markeredgewidth`` -- the size of the marker edge in points EXAMPLES: A line with no points or one point:: sage: line([]) #returns an empty plot Graphics object consisting of 0 graphics primitives sage: import numpy; line(numpy.array([])) Graphics object consisting of 0 graphics primitives sage: line([(1,1)]) Graphics object consisting of 1 graphics primitive A line with numpy arrays:: sage: line(numpy.array([[1,2], [3,4]])) Graphics object consisting of 1 graphics primitive A line with a legend:: sage: line([(0,0),(1,1)], legend_label='line') Graphics object consisting of 1 graphics primitive Lines with different colors in the legend text:: sage: p1 = line([(0,0),(1,1)], legend_label='line') sage: p2 = line([(1,1),(2,4)], legend_label='squared', legend_color='red') sage: p1 + p2 Graphics object consisting of 2 graphics primitives Extra options will get passed on to show(), as long as they are valid:: sage: line([(0,1), (3,4)], figsize=[10, 2]) Graphics object consisting of 1 graphics primitive sage: line([(0,1), (3,4)]).show(figsize=[10, 2]) # These are equivalent We can also use a logarithmic scale if the data will support it:: sage: line([(1,2),(2,4),(3,4),(4,8),(4.5,32)],scale='loglog',base=2) Graphics object consisting of 1 graphics primitive Many more examples below! 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)) Graphics object consisting of 1 graphics primitive A line with 2 complex points:: sage: i = CC.0 sage: line([1+i, 2+3*i]) Graphics object consisting of 1 graphics primitive 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)) Graphics object consisting of 1 graphics primitive 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)) Graphics object consisting of 1 graphics primitive 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)) Graphics object consisting of 1 graphics primitive 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)) Graphics object consisting of 1 graphics primitive A green lemniscate of Bernoulli:: sage: cosines = [cos(-pi/2+pi*i/100) for i in range(201)] sage: v = [(1/c, tan(-pi/2+pi*i/100)) for i,c in enumerate(cosines) if c != 0] 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)) Graphics object consisting of 1 graphics primitive A red plot of the Jacobi elliptic function `\text{sn}(x,2)`, `-3 < x < 3`:: sage: L = [(i/100.0, real_part(jacobi('sn', i/100.0, 2.0))) for i in ....: range(-300, 300, 30)] sage: line(L, rgbcolor=(3/4, 1/4, 1/8)) Graphics object consisting of 1 graphics primitive 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)) Graphics object consisting of 1 graphics primitive 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)) Graphics object consisting of 1 graphics primitive 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)) Graphics object consisting of 1 graphics primitive 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 Graphics object consisting of 3 graphics primitives TESTS: Check that :trac:`13690` is fixed. The legend label should have circles as markers.:: sage: line(enumerate(range(2)), marker='o', legend_label='circle') Graphics object consisting of 1 graphics primitive """ from sage.plot.all import Graphics from sage.plot.plot import xydata_from_point_list from sage.rings.all import CC, CDF if points in CC or points in CDF: pass else: try: if not points: return Graphics() except ValueError: # numpy raises a ValueError if not empty pass xdata, ydata = xydata_from_point_list(points) g = Graphics() g._set_extra_kwds(Graphics._extract_kwds_for_show(options)) g.add_primitive(Line(xdata, ydata, options)) if options['legend_label']: g.legend(True) g._legend_colors = [options['legend_color']] return g
def polygon2d(points, **options): r""" Returns a 2-dimensional polygon defined by ``points``. Type ``polygon2d.options`` for a dictionary of the default options for polygons. You can change this to change the defaults for all future polygons. Use ``polygon2d.reset()`` to reset to the default options. EXAMPLES: We create a purple-ish polygon:: sage: polygon2d([[1,2], [5,6], [5,0]], rgbcolor=(1,0,1)) Graphics object consisting of 1 graphics primitive .. PLOT:: sphinx_plot(polygon2d([[1,2], [5,6], [5,0]], rgbcolor=(1,0,1))) By default, polygons are filled in, but we can make them without a fill as well:: sage: polygon2d([[1,2], [5,6], [5,0]], fill=False) Graphics object consisting of 1 graphics primitive .. PLOT:: sphinx_plot(polygon2d([[1,2], [5,6], [5,0]], fill=False)) In either case, the thickness of the border can be controlled:: sage: polygon2d([[1,2], [5,6], [5,0]], fill=False, thickness=4, color='orange') Graphics object consisting of 1 graphics primitive .. PLOT:: P = polygon2d([[1,2], [5,6], [5,0]], fill=False, thickness=4, color='orange') sphinx_plot(P) For filled polygons, one can use different colors for the border and the interior as follows:: sage: L = [[0,0]]+[[i/100, 1.1+cos(i/20)] for i in range(100)]+[[1,0]] sage: polygon2d(L, color="limegreen", edgecolor="black", axes=False) Graphics object consisting of 1 graphics primitive .. PLOT:: L = [[0,0]]+[[i*0.01, 1.1+cos(i*0.05)] for i in range(100)]+[[1,0]] P = polygon2d(L, color="limegreen", edgecolor="black", axes=False) sphinx_plot(P) Some modern art -- a random polygon, with legend:: sage: v = [(randrange(-5,5), randrange(-5,5)) for _ in range(10)] sage: polygon2d(v, legend_label='some form') Graphics object consisting of 1 graphics primitive .. PLOT:: v = [(randrange(-5,5), randrange(-5,5)) for _ in range(10)] P = polygon2d(v, legend_label='some form') sphinx_plot(P) A purple hexagon:: sage: L = [[cos(pi*i/3),sin(pi*i/3)] for i in range(6)] sage: polygon2d(L, rgbcolor=(1,0,1)) Graphics object consisting of 1 graphics primitive .. PLOT:: L = [[cos(pi*i/3.0),sin(pi*i/3.0)] for i in range(6)] P = polygon2d(L, rgbcolor=(1,0,1)) sphinx_plot(P) 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: polygon2d(L, rgbcolor=(1/8,3/4,1/2)) Graphics object consisting of 1 graphics primitive .. PLOT:: L = [[-1+cos(pi*i*0.01)*(1+cos(pi*i*0.01)),2*sin(pi*i*0.01)*(1-cos(pi*i*0.01))] for i in range(200)] P = polygon2d(L, rgbcolor=(0.125,0.75,0.5)) sphinx_plot(P) 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: polygon2d(L, rgbcolor=(1/8,1/4,1/2)) Graphics object consisting of 1 graphics primitive .. PLOT:: L = [[6*cos(pi*i*0.01)+5*cos(3*pi*i*0.01),6*sin(pi*i*0.01)-5*sin(3*pi*i*0.01)] for i in range(200)] P = polygon2d(L, rgbcolor=(0.125,0.25,0.5)) sphinx_plot(P) 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: polygon2d(L, rgbcolor=(1/8,1/4,3/4)) Graphics object consisting of 1 graphics primitive .. PLOT:: n = 4.0; h = 5.0; b = 2.0 L = [[n*cos(pi*i*0.01)+h*cos((n/b)*pi*i*0.01),n*sin(pi*i*0.01)-h*sin((n/b)*pi*i*0.01)] for i in range(200)] P = polygon2d(L, rgbcolor=(0.125,0.25,0.75)) sphinx_plot(P) 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: polygon2d(L, rgbcolor=(7/8,1/4,3/4)) Graphics object consisting of 1 graphics primitive .. PLOT:: m = 9.0; b = 1 L = [[m*cos(pi*i*0.01)+b*cos((m/b)*pi*i*0.01),m*sin(pi*i*0.01)-b*sin((m/b)*pi*i*0.01)] for i in range(200)] P = polygon2d(L, rgbcolor=(0.875,0.25,0.75)) sphinx_plot(P) A brown astroid:: sage: L = [[cos(pi*i/100)^3,sin(pi*i/100)^3] for i in range(200)] sage: polygon2d(L, rgbcolor=(3/4,1/4,1/4)) Graphics object consisting of 1 graphics primitive .. PLOT:: L = [[cos(pi*i*0.01)**3,sin(pi*i*0.01)**3] for i in range(200)] P = polygon2d(L, rgbcolor=(0.75,0.25,0.25)) sphinx_plot(P) 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: polygon2d(L, rgbcolor=(1/8,3/4,1/2)) Graphics object consisting of 1 graphics primitive .. PLOT:: L = [[cos(pi*i*0.01)*(1+cos(pi*i*0.02)), sin(pi*i*0.01)*(1+sin(pi*i*0.02))] for i in range(200)] P = polygon2d(L, rgbcolor=(0.125,0.75,0.5)) sphinx_plot(P) 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: polygon2d(L, rgbcolor=(1,1/4,1/2)) Graphics object consisting of 1 graphics primitive .. PLOT:: L = [[sin(pi*i*0.01)+sin(pi*i*0.02),-(1+cos(pi*i*0.01)+cos(pi*i*0.02))] for i in range(-100,100)] P = polygon2d(L, rgbcolor=(1,0.25,0.5)) sphinx_plot(P) One can do the same one with a colored legend label:: sage: polygon2d(L, color='red', legend_label='For you!', legend_color='red') Graphics object consisting of 1 graphics primitive .. PLOT:: L = [[sin(pi*i*0.01)+sin(pi*i*0.02),-(1+cos(pi*i*0.01)+cos(pi*i*0.02))] for i in range(-100,100)] P = polygon2d(L, color='red', legend_label='For you!', legend_color='red') sphinx_plot(P) Polygons have a default aspect ratio of 1.0:: sage: polygon2d([[1,2], [5,6], [5,0]]).aspect_ratio() 1.0 AUTHORS: - David Joyner (2006-04-14): the long list of examples above. """ from sage.plot.plot import xydata_from_point_list from sage.plot.all import Graphics if options["thickness"] is None: # If the user did not specify thickness if options["fill"] and options["edgecolor"] is None: # If the user chose fill options["thickness"] = 0 else: options["thickness"] = 1 xdata, ydata = xydata_from_point_list(points) g = Graphics() # Reset aspect_ratio to 'automatic' in case scale is 'semilog[xy]'. # Otherwise matplotlib complains. scale = options.get('scale', None) if isinstance(scale, (list, tuple)): scale = scale[0] if scale == 'semilogy' or scale == 'semilogx': options['aspect_ratio'] = 'automatic' g._set_extra_kwds(Graphics._extract_kwds_for_show(options)) g.add_primitive(Polygon(xdata, ydata, options)) if options['legend_label']: g.legend(True) g._legend_colors = [options['legend_color']] return g
def point2d(points, **options): r""" A point of size ``size`` defined by point = `(x, y)`. INPUT: - ``points`` -- either a single point (as a tuple), a list of points, a single complex number, or a list of complex numbers - ``alpha`` -- how transparent the point is - ``faceted`` -- if ``True``, color the edge of the point (only for 2D plots) - ``hue`` -- the color given as a hue - ``legend_color`` -- the color of the legend text - ``legend_label`` -- the label for this item in the legend - ``marker`` -- the marker symbol for 2D plots only (see documentation of :func:`plot` for details) - ``markeredgecolor`` -- the color of the marker edge (only for 2D plots) - ``rgbcolor`` -- the color as an RGB tuple - ``size`` -- how big the point is (i.e., area in points^2=(1/72 inch)^2) - ``zorder`` -- the layer level in which to draw EXAMPLES: A purple point from a single tuple of coordinates:: sage: point((0.5, 0.5), rgbcolor=hue(0.75)) Graphics object consisting of 1 graphics primitive Points with customized markers and edge colors:: sage: r = [(random(), random()) for _ in range(10)] sage: point(r, marker='d', markeredgecolor='red', size=20) Graphics object consisting of 1 graphics primitive Passing an empty list returns an empty plot:: sage: point([]) Graphics object consisting of 0 graphics primitives sage: import numpy; point(numpy.array([])) Graphics object consisting of 0 graphics primitives If you need a 2D point to live in 3-space later, this is possible:: sage: A = point((1, 1)) sage: a = A[0]; a Point set defined by 1 point(s) sage: b = a.plot3d(z=3) This is also true with multiple points:: sage: P = point([(0, 0), (1, 1)]) sage: p = P[0] sage: q = p.plot3d(z=[2,3]) 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), size=30) Graphics object consisting of 1 graphics primitive And an example with a legend:: sage: point((0, 0), rgbcolor='black', pointsize=40, legend_label='origin') Graphics object consisting of 1 graphics primitive The legend can be colored:: sage: P = points([(0, 0), (1, 0)], pointsize=40, legend_label='origin', legend_color='red') sage: P + plot(x^2, (x, 0, 1), legend_label='plot', legend_color='green') Graphics object consisting of 2 graphics primitives Extra options will get passed on to show(), as long as they are valid:: sage: point([(cos(theta), sin(theta)) for theta in srange(0, 2*pi, pi/8)], frame=True) Graphics object consisting of 1 graphics primitive sage: point([(cos(theta), sin(theta)) for theta in srange(0, 2*pi, pi/8)]).show(frame=True) # These are equivalent For plotting data, we can use a logarithmic scale, as long as we are sure not to include any nonpositive points in the logarithmic direction:: sage: point([(1, 2),(2, 4),(3, 4),(4, 8),(4.5, 32)], scale='semilogy', base=2) Graphics object consisting of 1 graphics primitive Since Sage Version 4.4 (:trac:`8599`), the size of a 2d point can be given by the argument ``size`` instead of ``pointsize``. The argument ``pointsize`` is still supported:: sage: point((3, 4), size=100) Graphics object consisting of 1 graphics primitive :: sage: point((3, 4), pointsize=100) Graphics object consisting of 1 graphics primitive We can plot a single complex number:: sage: point(1 + I, pointsize=100) Graphics object consisting of 1 graphics primitive sage: point(sqrt(2) + I, pointsize=100) Graphics object consisting of 1 graphics primitive We can also plot a list of complex numbers:: sage: point([I, 1 + I, 2 + 2*I], pointsize=100) Graphics object consisting of 1 graphics primitive TESTS:: sage: point2d(iter([])) Graphics object consisting of 0 graphics primitives """ from sage.plot.plot import xydata_from_point_list from sage.plot.all import Graphics from sage.structure.element import Expression # points could be a single number if isinstance(points, numbers.Complex): points = [(points.real(), points.imag())] elif isinstance(points, numbers.Real): points = [points] elif isinstance(points, Expression): points = [points] elif not isinstance(points, (list, tuple)): # or an iterator points = list(points) l = len(points) if l == 0: return Graphics() elif l == 2: # special case for a single 2D point if all( isinstance(z, numbers.Real) or ( isinstance(z, Expression) and not complex(z).imag) for z in points): points = [points] elif l == 3: # special case for a single 3D point if all( isinstance(z, numbers.Real) or ( isinstance(z, Expression) and not complex(z).imag) for z in points): raise TypeError('not a 2D point') xdata, ydata = xydata_from_point_list(points) g = Graphics() g._set_extra_kwds(Graphics._extract_kwds_for_show(options)) g.add_primitive(Point(xdata, ydata, options)) if options['legend_label']: g.legend(True) g._legend_colors = [options['legend_color']] return g
def point2d(points, **options): r""" A point of size ``size`` defined by point = `(x,y)`. INPUT: - ``points`` - either a single point (as a tuple), a list of points, a single complex number, or a list of complex numbers. Type ``point2d.options`` to see all options. EXAMPLES: A purple point from a single tuple or coordinates:: sage: point((0.5, 0.5), rgbcolor=hue(0.75)) Passing an empty list returns an empty plot:: sage: point([]) sage: import numpy; point(numpy.array([])) If you need a 2D point to live in 3-space later, this is possible:: sage: A=point((1,1)) sage: a=A[0];a Point set defined by 1 point(s) sage: b=a.plot3d(z=3) This is also true with multiple points:: sage: P=point([(0,0), (1,1)]) sage: p=P[0] sage: q=p.plot3d(z=[2,3]) 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), size=30) And an example with a legend:: sage: point((0,0), rgbcolor='black', pointsize=40, legend_label='origin') The legend can be colored:: sage: P = points([(0,0),(1,0)], pointsize=40, legend_label='origin', legend_color='red') sage: P + plot(x^2,(x,0,1), legend_label='plot', legend_color='green') Extra options will get passed on to show(), as long as they are valid:: sage: point([(cos(theta), sin(theta)) for theta in srange(0, 2*pi, pi/8)], frame=True) sage: point([(cos(theta), sin(theta)) for theta in srange(0, 2*pi, pi/8)]).show(frame=True) # These are equivalent For plotting data, we can use a logarithmic scale, as long as we are sure not to include any nonpositive points in the logarithmic direction:: sage: point([(1,2),(2,4),(3,4),(4,8),(4.5,32)],scale='semilogy',base=2) Since Sage Version 4.4 (ticket #8599), the size of a 2d point can be given by the argument ``size`` instead of ``pointsize``. The argument ``pointsize`` is still supported:: sage: point((3,4), size=100) :: sage: point((3,4), pointsize=100) We can plot a single complex number:: sage: point(CC(1+I), pointsize=100) We can also plot a list of complex numbers:: sage: point([CC(I), CC(I+1), CC(2+2*I)], pointsize=100) """ from sage.plot.plot import xydata_from_point_list from sage.plot.all import Graphics from sage.rings.all import CC, CDF if points in CC or points in CDF: pass else: try: if not points: return Graphics() except ValueError: # numpy raises a ValueError if not empty pass xdata, ydata = xydata_from_point_list(points) g = Graphics() g._set_extra_kwds(Graphics._extract_kwds_for_show(options)) g.add_primitive(Point(xdata, ydata, options)) if options['legend_label']: g.legend(True) g._legend_colors = [options['legend_color']] return g
def point2d(points, **options): r""" A point of size ``size`` defined by point = `(x,y)`. Point takes either a single tuple of coordinates or a list of tuples. Type ``point2d.options`` to see all options. EXAMPLES: A purple point from a single tuple or coordinates:: sage: point((0.5, 0.5), rgbcolor=hue(0.75)) Passing an empty list returns an empty plot:: sage: point([]) If you need a 2D point to live in 3-space later, this is possible:: sage: A=point((1,1)) sage: a=A[0];a Point set defined by 1 point(s) sage: b=a.plot3d(z=3) This is also true with multiple points:: sage: P=point([(0,0), (1,1)]) sage: p=P[0] sage: q=p.plot3d(z=[2,3]) 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), size=30) And an example with a legend:: sage: point((0,0), rgbcolor='black', pointsize=40, legend_label='origin') Extra options will get passed on to show(), as long as they are valid:: sage: point([(cos(theta), sin(theta)) for theta in srange(0, 2*pi, pi/8)], frame=True) sage: point([(cos(theta), sin(theta)) for theta in srange(0, 2*pi, pi/8)]).show(frame=True) # These are equivalent For plotting data, we can use a logarithmic scale, as long as we are sure not to include any nonpositive points in the logarithmic direction:: sage: point([(1,2),(2,4),(3,4),(4,8),(4.5,32)],scale='semilogy',base=2) Since Sage Version 4.4 (ticket #8599), the size of a 2d point can be given by the argument ``size`` instead of ``pointsize``. The argument ``pointsize`` is still supported:: sage: point((3,4), size=100) :: sage: point((3,4), pointsize=100) """ from sage.plot.plot import xydata_from_point_list from sage.plot.all import Graphics if points == []: return Graphics() xdata, ydata = xydata_from_point_list(points) g = Graphics() g._set_extra_kwds(Graphics._extract_kwds_for_show(options)) g.add_primitive(Point(xdata, ydata, options)) if options['legend_label']: g.legend(True) return g
def polygon2d(points, **options): r""" Returns a 2-dimensional polygon defined by ``points``. Type ``polygon2d.options`` for a dictionary of the default options for polygons. You can change this to change the defaults for all future polygons. Use ``polygon2d.reset()`` to reset to the default options. EXAMPLES: We create a purple-ish polygon:: sage: polygon2d([[1,2], [5,6], [5,0]], rgbcolor=(1,0,1)) By default, polygons are filled in, but we can make them without a fill as well:: sage: polygon2d([[1,2], [5,6], [5,0]], fill=False) In either case, the thickness of the border can be controlled:: sage: polygon2d([[1,2], [5,6], [5,0]], fill=False, thickness=4, color='orange') Some modern art -- a random polygon, with legend:: sage: v = [(randrange(-5,5), randrange(-5,5)) for _ in range(10)] sage: polygon2d(v, legend_label='some form') A purple hexagon:: sage: L = [[cos(pi*i/3),sin(pi*i/3)] for i in range(6)] sage: polygon2d(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: polygon2d(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: polygon2d(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: polygon2d(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: polygon2d(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: polygon2d(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: polygon2d(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: polygon2d(L, rgbcolor=(1,1/4,1/2)) One can do the same one with a colored legend label:: sage: polygon2d(L, color='red', legend_label='For you!', legend_color='red') Polygons have a default aspect ratio of 1.0:: sage: polygon2d([[1,2], [5,6], [5,0]]).aspect_ratio() 1.0 AUTHORS: - David Joyner (2006-04-14): the long list of examples above. """ from sage.plot.plot import xydata_from_point_list from sage.plot.all import Graphics if options["thickness"] is None: # If the user did not specify thickness if options["fill"]: # If the user chose fill options["thickness"] = 0 else: options["thickness"] = 1 xdata, ydata = xydata_from_point_list(points) g = Graphics() # Reset aspect_ratio to 'automatic' in case scale is 'semilog[xy]'. # Otherwise matplotlib complains. scale = options.get('scale', None) if isinstance(scale, (list, tuple)): scale = scale[0] if scale == 'semilogy' or scale == 'semilogx': options['aspect_ratio'] = 'automatic' g._set_extra_kwds(Graphics._extract_kwds_for_show(options)) g.add_primitive(Polygon(xdata, ydata, options)) if options['legend_label']: g.legend(True) g._legend_colors = [options['legend_color']] return g
def point2d(points, **options): r""" A point of size ``size`` defined by point = `(x,y)`. INPUT: - ``points`` - either a single point (as a tuple), a list of points, a single complex number, or a list of complex numbers. - ``alpha`` -- How transparent the point is. - ``faceted`` -- If True color the edge of the point. (only for 2D plots) - ``hue`` -- The color given as a hue. - ``legend_color`` -- The color of the legend text - ``legend_label`` -- The label for this item in the legend. - ``marker`` -- the marker symbol for 2D plots only (see documentation of :func:`plot` for details) - ``markeredgecolor`` -- the color of the marker edge (only for 2D plots) - ``rgbcolor`` -- The color as an RGB tuple. - ``size`` -- How big the point is (i.e., area in points^2=(1/72 inch)^2). - ``zorder`` -- The layer level in which to draw EXAMPLES: A purple point from a single tuple or coordinates:: sage: point((0.5, 0.5), rgbcolor=hue(0.75)) Graphics object consisting of 1 graphics primitive Points with customized markers and edge colors:: sage: r = [(random(), random()) for _ in range(10)] sage: point(r, marker='d', markeredgecolor='red', size=20) Graphics object consisting of 1 graphics primitive Passing an empty list returns an empty plot:: sage: point([]) Graphics object consisting of 0 graphics primitives sage: import numpy; point(numpy.array([])) Graphics object consisting of 0 graphics primitives If you need a 2D point to live in 3-space later, this is possible:: sage: A=point((1,1)) sage: a=A[0];a Point set defined by 1 point(s) sage: b=a.plot3d(z=3) This is also true with multiple points:: sage: P=point([(0,0), (1,1)]) sage: p=P[0] sage: q=p.plot3d(z=[2,3]) 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), size=30) Graphics object consisting of 1 graphics primitive And an example with a legend:: sage: point((0,0), rgbcolor='black', pointsize=40, legend_label='origin') Graphics object consisting of 1 graphics primitive The legend can be colored:: sage: P = points([(0,0),(1,0)], pointsize=40, legend_label='origin', legend_color='red') sage: P + plot(x^2,(x,0,1), legend_label='plot', legend_color='green') Graphics object consisting of 2 graphics primitives Extra options will get passed on to show(), as long as they are valid:: sage: point([(cos(theta), sin(theta)) for theta in srange(0, 2*pi, pi/8)], frame=True) Graphics object consisting of 1 graphics primitive sage: point([(cos(theta), sin(theta)) for theta in srange(0, 2*pi, pi/8)]).show(frame=True) # These are equivalent For plotting data, we can use a logarithmic scale, as long as we are sure not to include any nonpositive points in the logarithmic direction:: sage: point([(1,2),(2,4),(3,4),(4,8),(4.5,32)],scale='semilogy',base=2) Graphics object consisting of 1 graphics primitive Since Sage Version 4.4 (:trac:`8599`), the size of a 2d point can be given by the argument ``size`` instead of ``pointsize``. The argument ``pointsize`` is still supported:: sage: point((3,4), size=100) Graphics object consisting of 1 graphics primitive :: sage: point((3,4), pointsize=100) Graphics object consisting of 1 graphics primitive We can plot a single complex number:: sage: point(CC(1+I), pointsize=100) Graphics object consisting of 1 graphics primitive We can also plot a list of complex numbers:: sage: point([CC(I), CC(I+1), CC(2+2*I)], pointsize=100) Graphics object consisting of 1 graphics primitive """ from sage.plot.plot import xydata_from_point_list from sage.plot.all import Graphics from sage.rings.all import CC, CDF if points in CC or points in CDF: pass else: try: if not points: return Graphics() except ValueError: # numpy raises a ValueError if not empty pass xdata, ydata = xydata_from_point_list(points) g = Graphics() g._set_extra_kwds(Graphics._extract_kwds_for_show(options)) g.add_primitive(Point(xdata, ydata, options)) if options['legend_label']: g.legend(True) g._legend_colors = [options['legend_color']] return g
def polygon2d(points, **options): r""" Returns a 2-dimensional polygon defined by ``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. EXAMPLES: We create a purple-ish polygon:: sage: polygon2d([[1,2], [5,6], [5,0]], rgbcolor=(1,0,1)) By default, polygons are filled in, but we can make them without a fill as well:: sage: polygon2d([[1,2], [5,6], [5,0]], fill=False) In either case, the thickness of the border can be controlled:: sage: polygon2d([[1,2], [5,6], [5,0]], fill=False, thickness=4, color='orange') Some modern art -- a random polygon, with legend:: sage: v = [(randrange(-5,5), randrange(-5,5)) for _ in range(10)] sage: polygon2d(v, legend_label='some form') A purple hexagon:: sage: L = [[cos(pi*i/3),sin(pi*i/3)] for i in range(6)] sage: polygon2d(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: polygon2d(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: polygon2d(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: polygon2d(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: polygon2d(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: polygon2d(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: polygon2d(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: polygon2d(L, rgbcolor=(1,1/4,1/2)) Polygons have a default aspect ratio of 1.0:: sage: polygon2d([[1,2], [5,6], [5,0]]).aspect_ratio() 1.0 AUTHORS: - David Joyner (2006-04-14): the long list of examples above. """ from sage.plot.plot import xydata_from_point_list from sage.plot.all import Graphics if options["thickness"] is None: # If the user did not specify thickness if options["fill"]: # If the user chose fill options["thickness"] = 0 else: options["thickness"] = 1 xdata, ydata = xydata_from_point_list(points) g = Graphics() g._set_extra_kwds(Graphics._extract_kwds_for_show(options)) g.add_primitive(Polygon(xdata, ydata, options)) if options['legend_label']: g.legend(True) return g
def point2d(points, **options): r""" A point of size ``size`` defined by point = `(x,y)`. Point takes either a single tuple of coordinates or a list of tuples. Type ``point2d.options`` to see all options. EXAMPLES: A purple point from a single tuple or coordinates:: sage: point((0.5, 0.5), rgbcolor=hue(0.75)) Passing an empty list returns an empty plot:: sage: point([]) If you need a 2D point to live in 3-space later, this is possible:: sage: A=point((1,1)) sage: a=A[0];a Point set defined by 1 point(s) sage: b=a.plot3d(z=3) This is also true with multiple points:: sage: P=point([(0,0), (1,1)]) sage: p=P[0] sage: q=p.plot3d(z=[2,3]) 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), size=30) And an example with a legend:: sage: point((0,0), rgbcolor='black', pointsize=40, legend_label='origin') The legend can be colored:: sage: P = points([(0,0),(1,0)], pointsize=40, legend_label='origin', legend_color='red') sage: P + plot(x^2,(x,0,1), legend_label='plot', legend_color='green') Extra options will get passed on to show(), as long as they are valid:: sage: point([(cos(theta), sin(theta)) for theta in srange(0, 2*pi, pi/8)], frame=True) sage: point([(cos(theta), sin(theta)) for theta in srange(0, 2*pi, pi/8)]).show(frame=True) # These are equivalent For plotting data, we can use a logarithmic scale, as long as we are sure not to include any nonpositive points in the logarithmic direction:: sage: point([(1,2),(2,4),(3,4),(4,8),(4.5,32)],scale='semilogy',base=2) Since Sage Version 4.4 (ticket #8599), the size of a 2d point can be given by the argument ``size`` instead of ``pointsize``. The argument ``pointsize`` is still supported:: sage: point((3,4), size=100) :: sage: point((3,4), pointsize=100) """ from sage.plot.plot import xydata_from_point_list from sage.plot.all import Graphics if points == []: return Graphics() xdata, ydata = xydata_from_point_list(points) g = Graphics() g._set_extra_kwds(Graphics._extract_kwds_for_show(options)) g.add_primitive(Point(xdata, ydata, options)) if options['legend_label']: g.legend(True) g._legend_colors = [options['legend_color']] return g
def line2d(points, **options): r""" Create the line through the given list of points. Type \code{line2d.options} for a dictionary of the default options for lines. You can change this to change the defaults for all future lines. Use \code{line2d.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 - ``legend_label`` -- the label for this item in the legend Any MATPLOTLIB line option may also be passed in. E.g., - ``linestyle`` - The style of the line, which is one of - ``"-"`` (solid) -- default - ``"--"`` (dashed) - ``"-."`` (dash dot) - ``":"`` (dotted) - ``"None"`` or ``" "`` or ``""`` (nothing) The linestyle can also be prefixed with a drawing style (e.g., ``"steps--"``) - ``"default"`` (connect the points with straight lines) - ``"steps"`` or ``"steps-pre"`` (step function; horizontal line is to the left of point) - ``"steps-mid"`` (step function; points are in the middle of horizontal lines) - ``"steps-post"`` (step function; horizontal line is to the right of point) - ``marker`` - The style of the markers, which is one of - ``"None"`` or ``" "`` or ``""`` (nothing) -- default - ``","`` (pixel), ``"."`` (point) - ``"_"`` (horizontal line), ``"|"`` (vertical line) - ``"o"`` (circle), ``"p"`` (pentagon), ``"s"`` (square), ``"x"`` (x), ``"+"`` (plus), ``"*"`` (star) - ``"D"`` (diamond), ``"d"`` (thin diamond) - ``"H"`` (hexagon), ``"h"`` (alternative hexagon) - ``"<"`` (triangle left), ``">"`` (triangle right), ``"^"`` (triangle up), ``"v"`` (triangle down) - ``"1"`` (tri down), ``"2"`` (tri up), ``"3"`` (tri left), ``"4"`` (tri right) - ``0`` (tick left), ``1`` (tick right), ``2`` (tick up), ``3`` (tick down) - ``4`` (caret left), ``5`` (caret right), ``6`` (caret up), ``7`` (caret down) - ``"$...$"`` (math TeX string) - ``markersize`` -- the size of the marker in points - ``markeredgecolor`` -- the color of the marker edge - ``markerfacecolor`` -- the color of the marker face - ``markeredgewidth`` -- the size of the marker edge in points EXAMPLES: 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: cosines = [cos(-pi/2+pi*i/100) for i in range(201)] sage: v = [(1/c, tan(-pi/2+pi*i/100)) for i,c in enumerate(cosines) if c != 0] 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 `\text{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([]) #returns an empty plot sage: line([(1,1)]) A line with a legend:: sage: line([(0,0),(1,1)], legend_label='line') Extra options will get passed on to show(), as long as they are valid:: sage: line([(0,1), (3,4)], figsize=[10, 2]) sage: line([(0,1), (3,4)]).show(figsize=[10, 2]) # These are equivalent """ from sage.plot.plot import Graphics, xydata_from_point_list if points == []: return Graphics() xdata, ydata = xydata_from_point_list(points) g = Graphics() g._set_extra_kwds(Graphics._extract_kwds_for_show(options)) g.add_primitive(Line(xdata, ydata, options)) if options['legend_label']: g.legend(True) return g