*This is one in a series of posts containing short snippets of Sage code for my students to use: see my page on Sage.*

**Plotting a single function**

### Simple plotting of functions can be done with the `plot`

command:

var('t') f(t) = t^2 plot(f,-1,2)

A slightly more complicated piece of code that yields the same result is

var('t') f(t) = t^2 plot(f(t),(t,-1,2))

We can restrict the vertical range of the plot with the code

var('t') f(t) = t^2 plot(f(t),(t,-1,2), ymin=-.5, ymax = 3)

It is possible to add all sort of features to the plot: labels, legends, etc. Note that the labels can be done in LaTeX!

var('t') f(t) = t^2 plot(f(t),(t,-1,2),ymin=-.5,ymax = 3, axes_labels=['$t$','gallons'], legend_label='$f(t)=t^2$',show_legend=true)

We can also play around with the style of the plot itself:

var('t') f(t) = t^2 plot(f(t),(t,-1,2),ymin=-.5,ymax = 3, thickness=2, color='purple', linestyle='dashed')

For a whole list of options for plotting, see the Sage plotting page.

Finally, Sage can handle asymptotes… if we tell it to. First try out this code:

var('t') f(t) = 1/((t-1)*(t+2)) plot(f(t),(t,-3,2),ymin=-10,ymax = 10)

The following, using `detect_poles`

is much better:

var('t') f(t) = 1/((t-1)*(t+2)) plot(f(t),(t,-3,2),ymin=-10,ymax = 10, detect_poles='show')

**Plotting multiple functions**

In order to plot multiple functions at the same time, we first take advantage of the feature that we can assign names to plots. When we do this, we need to use the `.show()`

command to actually show the plot. We can incorporate many of the display options in to the `.show()`

command:

var('t') f(t) = t^2 fplot = plot(f(t), (t,-10,10)) fplot.show(xmin = -2, xmax=3, ymin=-1, ymax=5, axes_labels=['$t$',' '])

With these tools in mind, we can easily add the plot of a second function. Take a close look at each line of code here; what does each accomplish?

var('t') f(t) = t^2 g(t) = 4-t^2 fplot = plot(f(t), (t,-10,10), color='purple') gplot = plot(g(t), (t,-10,10), color='blue') mainplot = fplot + gplot mainplot.show(xmin = -2, xmax=3, ymin=-1, ymax=5, axes_labels=['$t$',' '])

Here is an example of something a bit fancier (and kind of fun)

var('t') f(t) = t^2 - t^3 g = derivative(f,t) fplot = plot(f(t), (t,-10,10), color='purple', legend_label='$f(t)$') gplot = plot(g(t), (t,-10,10), color='blue', legend_label='$f^\prime(t)$') mainplot = fplot + gplot mainplot.show(xmin = -1, xmax=2, ymin=-1, ymax=1, axes_labels=['$t$',' '], show_legend=true)

**Saving plots**

We can save a plot as a pdf file using the command `.save()`

. Here is a simple example:

var('t') f(t) = t^2 - t^3 fplot = plot(f(t), (t,-1,2), ymax=1, ymin=-1,color='purple') fplot.save(filename='fun-plot.pdf')

If I am going to be using the plot in a LaTeX document, I like to adjust the size. For example:

var('t') f(t) = t^2 - t^3 fplot = plot(f(t), (t,-1,2), ymax=1, ymin=-1,color='purple') fplot.save(filename='fun-plot.pdf', figsize=[4,3])

Of course, you can also add in all the other fancy stuff while saving the plot to a pdf!

var('t') f(t) = t^2 - t^3 fplot = plot(f(t), (t,-1,2),color='purple',legend_label='$f(t)$') fplot.save(filename='fun-plot.pdf', axes_labels=['$t$', ' '], show_legend=true,ymax=1, ymin=-1,figsize=[4,3])