Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

132928 views
License: OTHER
This material was developed by Aaron Tresham at the University of Hawaii at Hilo and is Creative Commons License
licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Advanced Graphing Features

These notes provide instructions for some advanced graphing features in Sage. They are provided here for future reference; you will not need these for the assignment today.

Adding a Legend

We can add a legend using the legend_label option. Make sure you add it to all the plots.

f(x)=x^2 g(x)=(x-2)^2-3 plot(f,xmin=-5,xmax=5,legend_label='f')+plot(g,xmin=-5,xmax=5,color='red',legend_label='g')

Your labels can be whatever string you want (just make sure it's all in quotes).

f(x)=x^2 g(x)=(x-2)^2-3 h(x)=x^3 plot(f,xmin=-5,xmax=5,color='salmon',legend_label='function 1')+plot(g,xmin=-5,xmax=5,color='fuchsia',legend_label='function 2')+plot(h,xmin=-5,xmax=5,ymin=-50,ymax=50,color='teal',legend_label='function 3')

Labelling the Axes

We can also label the axes using the axes_labels option (you only need to add this to one of the plots). Notice the square brackets.

f(x)=x^2 g(x)=(x-2)^2-3 plot(f,xmin=-5,xmax=5,legend_label='f',axes_labels=['x','y'])+plot(g,xmin=-5,xmax=5,color='red',legend_label='g')

Here's another example.

f(x)=x^2 g(x)=(x-2)^2-3 h(x)=x^3 plot(f,xmin=0,xmax=20,color='salmon',legend_label='company 1')+plot(g,xmin=0,xmax=20,color='fuchsia',legend_label='company 2')+plot(h,xmin=0,xmax=20,ymax=2000,color='teal',legend_label='company 3',axes_labels=['time','profit'])

Changing Line Thickness

Make the curves thicker with the "thickness" option.

f(x)=x^2 g(x)=(x-2)^2-3 h(x)=x^3 plot(f,xmin=-5,xmax=5,thickness=5)+plot(g,xmin=-5,xmax=5,color='red',linestyle='dotted',thickness=2)+plot(h,xmin=-5,xmax=5,color='green',linestyle='dashed',thickness=4)

Shading

To shade the area under a curve, add the option fill='axis'

f(x)=x^2 plot(f,fill='axis')

You can also shade the area between ff and gg by adding fill=g to the plot for ff. Notice that there are no quotes around g.

f(x)=x^2 g(x)=(x-2)^2-3 plot(f,xmin=-5,xmax=5,fill=g)+plot(g,xmin=-5,xmax=5,color='red')

You could get the same thing by adding fill=f to the plot of gg instead.

plot(f,xmin=-5,xmax=5)+plot(g,xmin=-5,xmax=5,color='red',fill=f)

Adding a Title

Add a title using the title option. You add this to just one of the plots.

f(x)=x^2 g(x)=(x-2)^2-3 plot(f,xmin=-5,xmax=5,fill=g)+plot(g,xmin=-5,xmax=5,color='red',title='The area between f and g')

Adding Text to Plots

In addition to titles and axis labels, you can add any text to a plot using the "text" command. You specify any string within quotation marks along with an ordered pair specifying the location.

Here is an example. This inserts the string "some text" centered at the point (0.5,0.5)(0.5,0.5).

plot(x^2)+text('some text',(.5,.5))

Here is another example. I have changed the text color to black, and I have rotated the text 45 degrees.

plot(x^2)+text('something',(.5,.5),color='black',rotation=45)

You can also adjust the size using the "fontsize" option, which specifies the size in points (the default is 10).

plot(x^2)+text('something',(.5,.5),color='black',rotation=90, fontsize=12)

If you put fontsize in the plot, it will change the size of the tick mark labels, as well as axis labels and the title.

Notice how the y-axis label and the title overlap in the example below.

plot(x^2,fontsize=14,title='A Graph',axes_labels=['x','y'])

We can use the text command to put the axis label to the side of the y-axis.

plot(x^2,fontsize=14,title='A Graph',axes_labels=['x',''])+text('y',(-.1,1),color='black',fontsize=14)

For some reason, the font size is different for the x and y labels, so it may be better to use "text" for both.

plot(x^2,fontsize=14,title='A Graph')+text('y',(-.1,1),color='black',fontsize=14)+text('x',(1,.1),color='black',fontsize=14)

Here's a final example.

plot(e^(.1*x),xmin=0,xmax=100,title='Population of Somewhere')+text('Population',(-10,10000),color='black',rotation=90)+text('Time (in years)',(50,-2500),color='black')

Adding Text with Math Symbols

If you want to add text to a plot that includes math symbols, then you can use LaTeX\LaTeX (see "Writing in Sage" notes). Put $ signs around the LaTeX\LaTeX code.

For technical reasons (having to do with Python), you should use a "raw string" within the text command. This means you add the letter "r" before the first quotation mark. This is not always necessary, but sometimes leaving off the r will have irritating results.

Here is an example.

plot(sqrt(x),xmin=0,xmax=4)+text(r'$f(x)=\sqrt{x}$',(2.5,1.75)) #Note the r before the quote mark and the dollar signs.