Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 74
plot(2*x+exp(-x), (x,-4,4))
plot(2*x+exp(-x), (x,-4,4), axes_labels=['$x$', '$2x+e^{-x}$'], color='green', linestyle='--', thickness=3) print('\n\n NOTE: Sage understands Latex syntax for typesetting math expressions in axes labels.')
NOTE: Sage understands Latex syntax for typesetting math expressions in axes labels.
# Piecewise plots. f1 = x^2 f2 = 5-x f3 = ln(x) f = piecewise( [ ((-infinity,0),f1), ((0,4),f2), ((4,infinity),f3) ] ) f.plot((x,-4,10), axes_labels=['$x$', '$f(x)$']) # NOTE: If we don't want vertical lines at the points of discontinuity, use the following instead: #f.plot(x,-4,10, exclude=[0,4])
# Another way to do the same/similar piecewise plot: f1 = plot(x^2, x, -4, 0) # I cannot use -infinity for lower bound here. So I used -4. f2 = plot(5-x, x, 0, 4) f3 = plot(ln(x), x, 4, 10) # I cannot use infinity for upper bound. So I used 10. (f1+f2+f3).show(xmin=-4, xmax=10, axes_labels=['$x$', '$f(x)$'])
f1 = x^2 f2 = 5-x f3 = ln(x) f = plot( piecewise( [ ((-infinity,0),f1), ((0,4),f2), ((4,infinity),f3) ] ), (x,-4,10), axes_labels=['$x$', '$f(x)$'], exclude=[0,4] ) pt1 = point([(0,0), (4,1)], color='black', pointsize=50) pt2 = point([(0,5), (4,ln(4))], color='white', pointsize=50, faceted='true') (f+pt1+pt2).show()
y = var('y') # This defines y as another variable. By default you get only x. implicit_plot( cos(x*y)==x^2 - y, (x,-3,3), (y,-2,8) ) print('\n NOTE the "==" sign used to denote the conditional equality.')
NOTE the "==" sign used to denote the conditional equality.
# parametric plots t = var('t') parametric_plot( ( sin(t^2-cos(t)), cos(3*t) ), (t,-pi/2,pi) )
# Plots of (x,y) data: Can use the "scatter_plot" command, the "point" command, or the "line" command. # Must create a list of 2d ordered pairs of points. # It doesn't work with 2 separate lists -- it must be one list of 2-tuples. xl = [-4.8, -3.1, -2.2, -0.7, 0.2, 2.5, 4.0, 5.6] yl = [3.7, 2.9, 2.4, 2.2, 3.5, 5.8, 9.6, 6.3] datapoints = zip(xl, yl) # The "zip" command creates matched pairs from 2 separate lists. scatter_plot(datapoints, axes_labels=['$x$', '$y$'], markersize=30) point(datapoints, axes_labels=['$x$', '$y$'], pointsize=30) line(datapoints, axes_labels=['$x$', '$y$'], color='blue')
f = open('./globtemp.csv') # This file has comma separated data, with a plain text header line. datapoints = [] # This will assemble my data, in the form of a list of ordered pairs headerline = f.readline() # Read & discard the header line line = f.readline() # This is the first line of actual data while (line !=''): xy = line.split(',') # The comma splits each line into two items of data datapoints.append((float(xy[0]),float(xy[1]))) line = f.readline() point(datapoints, axes_labels=['year', 'temp $^\circ$C'], pointsize=30)
y = var('y') p = plot(2*x+exp(-x), (x,-3,3), color='green', linestyle='--', thickness=2, aspect_ratio=1) q = implicit_plot( cos(x*y)==x^2 - y, (x,-3,3), (y,-2,8), figsize=8, aspect_ratio=1 ) show(p+q)

The coolest graph game!

It is possible to make some really interesting and unusual artistic sketches using graphs. In particular, implicit curves and parametric curves lend themselves to a variety of different shapes and forms. The following sketch shows an example (you can see the plot commands by clicking on the little triangular tab below):
y = var('y') mouth = implicit_plot( (-y+2.4)^2*(2.2-x^2)==(x^2+3*(-y+2.4)-2)^2, (x,-6,6), (y,-2,8), linewidth=3, color='red' ) head = implicit_plot( 10*x^2==y^3*(6-y), (x,-6,6), (y,-2,8), linewidth=2 ) rear = implicit_plot( ((x-3.6)^2 + (y-4)^2)^2==(x-3.6)^2 - (y-4)^2, (x,-6,6), (y,-2,8), linewidth=2, color='green' ) lear = implicit_plot( ((x+3.6)^2 + (y-4)^2)^2==(x+3.6)^2 - (y-4)^2, (x,-6,6), (y,-2,8), linewidth=2, color='green' ) t = var('t') reye = parametric_plot( ( 0.02*t*cos(t)+1.5, 0.02*t*sin(t)+4 ), (t,0,20), color='black' ) leye = parametric_plot( ( 0.02*t*cos(-t+pi)-1.5, 0.02*t*sin(-t+pi)+4 ), (t,0,20), color='black' ) show(mouth+head+lear+rear+reye+leye)
Explore the following examples, and try out your own variations in an effort to make your own coolest graphs

  1. x=0.02tcos(t)x = 0.02 t \cos(t),  y=0.02tsin(t)y = 0.02 t \sin(t). Use tt=0 to 10, and tt=0 to 100.
    Variations: Replace one of the 0.02 by 0.04.
                    Replace sin(t)\sin(t) by sin(2t)\sin(2t) or cos(t)\cos(t) by cos(2t)\cos(2t).
                    Interchange and/or replace sin\sin with cos\cos.
  2. x=2cos(20t)costx = 2 \sqrt{\cos(20t)} \cos t,   y=2cos(20t)sinty = 2 \sqrt{\cos(20t)} \sin t. Let tt=0 to 100.
    Variations: Replace cos(20t)\cos(20t) with [cos(20t)](1/20)[\cos(20t)]^{(1/20)}
  3. x=t+sin(2t)x = t + \sin (2t),   y=t+sin(3t)y = t + \sin (3t)
    Variations: Replace sin\sin with cos\cos. Play with different numbers.
  4. x=4costx = 4 \cos t,   y=2sin(t+sin(150t))y = 2 \sin (t + \sin (150t))
  5. x=1.5costcos(30t)x = 1.5 \cos t - \cos (30t),   y=1.5sintsin(30t)y = 1.5 \sin t - \sin (30t)
  6. x=4sin(t+cos(100t))x = 4 \sin (t + \cos (100t)),   y=4cos(t+sin(100t))y = 4 \cos (t + \sin (100t))
  7. y2(y26)=x2(x28)y^2 (y^2 - 6) = x^2 (x^2 - 8)
  8. y2=x3+3x2y^2 = x^3 + 3 x^2