Annotating Graphs

Description

Axes labels and legends

Here we will be placing a legend and axes labels on the graph of the function

(1)
\begin{align} f(x) = sin(\pi x) \end{align}

A legend is generated by setting legend_label when creating the plot object, and axes labels are set with the .axes_labels() method.

Sage Cell

Code

p = plot(sin(pi*x), -pi, pi, legend_label='f(x) = sin (pi x)' )
p.axes_labels(['x', 'y=f(x)'])
p.show()

The six gridline options

Sage has to parameters which control gridlines on graphs; frame, which can be set to true or false and gridlines, which can be set to true, false, or minor. Combining both, this allows for 6 different gridline options. We will demonstrate them all in the following cells. The default value of both parameters is False, so setting them both to False will produce the same graph as if you hadn't set them at all.

Adding axes labels

Code

plot(x^3-x, -2, 2, gridlines=True, frame=False)
plot(x^3-x, -2, 2, gridlines='minor', frame=True)
plot(x^3-x, -2, 2, gridlines=True, frame=True)
plot(x^3-x, -2, 2, gridlines='minor', frame=True)
plot(x^3-x, -2, 2, gridlines=False, frame=False)
plot(x^3-x, -2, 2, gridlines=False, frame=True)

Adding Arrows, Text and Points

Beyond legends and axis labels, sage also offers the ability to place arrows, points and text at specified locations using point(), arrow() and text(). We will be annotating the graph of

(2)
\begin{equation} f(x) = x^2 + 1 \end{equation}

And its tangent line at $f(1)$,

(3)
\begin{equation} g(x) = 2x \end{equation}

Code

tangent_line = plot(2*x, -2, 2, gridlines='minor')
curve = plot(x^2 + 1, -2, 2)
big_dot = point( (1,2), size=60 )
the_arrow = arrow( (0.2, 2.8), (0.9, 2.1) )
the_words = text( '$f(x) = x^2 + 1$ and tangent line at $(1, 2)$.', (0.5, -2.75), fontsize = 14 )
big_image = tangent_line + curve + big_dot + the_arrow + the_words
big_image.show()

A few notes on arrow() and text:

When creating an arrow, the point will be located at the second set of points; for the arrow in the graph we made, if you wanted an arrow pointing at the intersection from the opposite direction, you could input

the_arrow = arrow( (1.8, 1.2), (1.1, 1.9))

When using text(), the text will be center aligned and will originate from the point specified, so make sure to leave enough room on either side to fit what you want on the graph.

Graphing an Integral

Here we will use the fill property to graph a visualization of

(4)
\begin{align} \int_0^2 -1 + \sqrt{x} \ dx \end{align}

Code

plot(-1 + sqrt(x), 0, 2, fill=True)

You can set the color of the filling and the transparency by setting fillcolor and fillalpha:

plot(-1 + sqrt(x), 0, 2, fill=True, fillcolor='yellow', fillalpha=2/3)

Dotted and Dashed lines

Similar to colors, you can also declare linestyle when creating a plot to make different patterns of dots and dashes when plotting multiple functions.

Code

P1 = plot( x^2, 0, 1.2)
P2 = plot( x^3, 0, 1.2, linestyle='-')
P3 = plot( x^4, 0, 1.2, linestyle='-.')
P4 = plot( x^5, 0, 1.2, linestyle=':')
P5 = plot( x^6, 0, 1.2, linestyle='--')
P = P1 + P2 + P3 + P4 + P5
P.show()

Options

Primary Tags:

Secondary Tags:

A list of possible tags can be found at The WeBWorK Open Problem Library. For linear algebra tags see the Curated Courses Project.

Tags

Any related cells go here. Provide a link to the page containing the information about the cell.

Related Cells

Permalink:

Author: Gregory V. Bard. Sage for Undergraduates. American Mathematical Society, Providence, RI, 2015. Available at http://www.gregorybard.com/Sage.html.

Attribute

Date: 27 Feb 2019 22:32

Submitted by: Zane Corbiere

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License