In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
In [2]:
data = [10, 20, 15, 30, 25]

A simple plot

Matplotlib docs: plot().

The plot() function will draw a line chart. When using iPython or an iPython notebook, invoking plot() (or other charting functions, such as bar() and pie()) will display the resulting chart.

In [3]:
plt.plot(data)
Out[3]:
[<matplotlib.lines.Line2D at 0x108ab4f28>]

Adding a title

Matplotlib docs: suptitle().

The suptitle() function is used to display a title for the entire chart, which is more apparent when creating charts that consist of several subplots.

In [9]:
plt.suptitle("Main Title", fontsize = 30)
plt.plot(data)
Out[9]:
[<matplotlib.lines.Line2D at 0x108e7c898>]

Add axis labels

Matplotlib docs: xlabel() and ylabel().

Labels are instances of matplotlib.text.Text.

Colors can be represented as a tuple of RGB colors or as a string for a predefined name, e.g. 'blue' and 'gold' (see the full list of names)

In [25]:
plt.xlabel('X Factor', color = (0.9, 0, 0.9), fontsize = 20)
plt.ylabel('Y Combos', color = 'Chocolate', fontsize = 16, weight = 'bold', fontname = 'Comic Sans MS')
plt.plot(data)
Out[25]:
[<matplotlib.lines.Line2D at 0x10a0c67b8>]
In [35]:
plt.xticks(rotation = 'vertical')
plt.yticks(rotation = -45)
plt.plot(data)
Out[35]:
[<matplotlib.lines.Line2D at 0x10a55e7b8>]

Labeling axis ticks

In [37]:
plt.yticks([15, 30], ['Meh', 'Whoa!'])
plt.xticks([0, 1, 2, 3, 4], ['Zero', 'Un', 'Deux', 'Tres', 'Cinco'])
plt.plot(data)
Out[37]:
[<matplotlib.lines.Line2D at 0x10a645fd0>]

Setting the axis limit

Matplotlib docs: xlim() and ylim().

By default, the lower and upper bounds are set based on the max and min of the data. The xlim() and ylim() functions can be used to set the beginning and/or end of an axis's range.

In [43]:
plt.xlim(xmax = 10)
plt.ylim([0, 50]) 
plt.plot(data)
Out[43]:
[<matplotlib.lines.Line2D at 0x10b095978>]

Using stylesheets

Matplotlib docs: Customizing plots with style sheets

The tedious process of setting up visual styles has been a common complaint about matplotlib. However, the pyplot.style package allows the use of pre-defined stylesheets.

Use plt.style.available to see a list of stylesheets available to your system. These are the ones included in matplotlib:

In [44]:
print(plt.style.available)
['fivethirtyeight', 'bmh', 'dark_background', 'ggplot', 'grayscale']
In [46]:
plt.style.use('fivethirtyeight')
plt.plot(data)
Out[46]:
[<matplotlib.lines.Line2D at 0x10a115f28>]