MTH 337: Week 5

In [1]:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

Add text to figures using Matplotlib "text"

In [2]:
xpts = [1, 2, 1.5, 1]
ypts = [1, 1, 2, 1]
plt.plot(xpts, ypts)
plt.text(1.4, 1.4, 'Triangle');

fontsize

In [3]:
xpts = [1, 2, 1.5, 1]
ypts = [1, 1, 2, 1]
plt.plot(xpts, ypts)
plt.text(1.4, 1.4, 'Triangle', fontsize=20);

color

In [4]:
xpts = [1, 2, 1.5, 1]
ypts = [1, 1, 2, 1]
plt.plot(xpts, ypts)
plt.text(1.4, 1.4, 'Triangle', fontsize=20, color='r');

backgroundcolor

In [5]:
xpts = [1, 2, 1.5, 1]
ypts = [1, 1, 2, 1]
plt.plot(xpts, ypts)
plt.text(1.4, 1.4, 'Triangle', fontsize=20, color='r', backgroundcolor='lime');

weight

In [6]:
xpts = [1, 2, 1.5, 1]
ypts = [1, 1, 2, 1]
plt.plot(xpts, ypts)
plt.text(1.4, 1.4, 'Triangle', weight='bold');

Using different properties together

In [7]:
xpts = [1, 2, 1.5, 1]
ypts = [1, 1, 2, 1]
plt.plot(xpts, ypts)
plt.text(1.25, 1.2, 'Triangle', weight='bold', color='m', fontsize=32, backgroundcolor='y');

annotate

This adds text to a graph, and an arrow pointing from the text to some location. The keyword arguments are:

  • xy: the location to be labeled, as an (x, y) tuple of coordinates.
  • xytext: the location of the text, also as an (x, y) tuple of coordinates.
  • s: the text to be added.
  • arrowprops: a Python dictionary of {key:value} pairs, separated by commas. The keys are
    • arrowstyle
    • width
    • headwidth
    • headlength

Using the arrowstyle property

In [8]:
xpts = [1, 2, 1.5, 1]
ypts = [1, 1, 2, 1]
plt.plot(xpts, ypts)
plt.annotate(xy=(2, 1), xytext=(1.8, 1.1), s='vertex', arrowprops={'arrowstyle':'->'});

Specifying the arrow properties

In [9]:
xpts = [1, 2, 1.5, 1]
ypts = [1, 1, 2, 1]
plt.plot(xpts, ypts)
plt.annotate(xy=(2, 1), xytext=(1.8, 1.1), s='vertex', arrowprops={'width':1, 'headwidth': 5, 'headlength': 8});