m098plotting1

805 days ago by jozefl

Sage has unique functions that graph geometric shapes:

circle((x,y), r,options)      where (x,y) is the coordinate of the center of the circle and r is the length of the radius.

polygon([[x1,y1],[x2,y2],...,[xn,yn]],options)      where the [xi,yi] are the vertices of the polygon.

P = polygon([[0,0],[0,-1],[1,0],[1,-1]],color = 'red') C = circle((1,2),1,color='green') P+C 
       

What two things strike you as odd about this graphed?  Perhaps
      A) Our circle appears to be a horrifically distorted elipse?

      B) Our rectangle appears to be a triangle reflected about x=0.5?


The first problem results from the fact that, like a TI calculater, Sage does not automatically use a 1:1 aspect ration. This is easily fixed with

aspect_ratio=x where x corresponds to 1:x.


The second problem occurs because Sage graphs a polygon according to the order in which we list the vertices. The fix is to list the vertices of your polygon in either clockwise or counter-clockwise order.

P = polygon([[0,0],[0,-1],[1,-1],[1,0]],color = 'red',aspect_ratio=1) C = circle((1,2),1,color='green') P+C 
       

Note that including aspect_ratio when creating the circle puts the entire graph in 1:1 (as we would hope!). Global plot options are better placed in a final show command or in a predefind graph followed by graphname.show().

We'll also make our polygon semitransparent. the alpha  option. Photoshop users will recognize this as a reference to the alpha channel. Programs capable of semi-transparency use an extra channel, the value of which determines how much background objects show through a foreground shape.

P = polygon([[0,0],[0,-1],[1,-1],[1,0]],color = 'red',alpha=0.5) C = circle((1,2),1,color='green',fill=True) C2 = circle((1,0.5),1,color='blue',fill=True) graph=(C2+P+C) graph.show(aspect_ratio=1) 
       

Now we'll explore a variety of different plot options.  For a comprehensive list, see http://www.sagemath.org/doc/reference/sage/plot/plot.html.

P = polygon([[0,0],[0,-1],[1,-1],[1,0]],color = 'red',alpha=0.5) Ptext = text('Square centered on (0,0)',(1.5,-0.7),color='red') C = circle((1,2),1,color='green',fill=True,alpha=0.7) Ctext = text('Circle of radius=1 centered on (1,2)',(2,2),rgbcolor=(0,0.5,0)) C2 = circle((1,0.5),1,color='blue',fill=True,alpha=0.3) C2text = text('Circle of radius=1 centered on (1,0.5)',(2,0.5),color='blue') title=text('Title',(2,3.3),color='black') graph=(C2+C2text+P+Ptext+C+Ctext+title) graph.show(xmin=0,xmax=5,ymax=3.5,aspect_ratio=1,axes_labels=(['ind. var.','dep. var']))