Matplotlib Tutorial

Usually what we use is the pyplot module of matplotlib. Let's inline plt and make a simple plot with titles for the axes.


In [2]:
from matplotlib import pyplot as plt
%matplotlib inline

In [9]:
plt.plot([5,6,7,8],[7,4,8,4])

## Adding labels
plt.title("Random Plot")
plt.xlabel("$1/2\cdot x$")
plt.ylabel("Dispersion $[m]$")


Out[9]:
<matplotlib.text.Text at 0x1117b6590>

Styles

We can use a simple style usage for the plots made by plt.

Let's try and use the ggplot style


In [11]:
# import the styles
from matplotlib import style

# select the 'ggplot' style from the default ones
style.use('ggplot')

# and make the same plot -- pretty cool heh? :)
plt.plot([5,6,7,8],[7,4,8,4])
plt.title("Random Plot")
plt.xlabel("$1/2\cdot x$")
plt.ylabel("Dispersion $[m]$")


Out[11]:
<matplotlib.text.Text at 0x11192f4d0>

We can also use some of the formatting options in the plot command. For example color, width, label, etc.


In [20]:
# add 2 plots on the same canvas
plt.plot([2,3,4],[4,9,16], color='green', label="$x^2", lw=2)
plt.plot([2,3,4],[8,27,64], color='red', label="$x^3", lw=3)

# add the legend:
plt.legend()

# add grid
plt.grid(True, color='blue')



In [ ]: