Profile picture

Annotations and Legends

Last updated: July 2nd, 20192019-07-02Project preview

rmotr


Annotations and legends

As it has been seen, so far the graphics made have some clear weak points:

  • They do not indicate the meaning of each of the axes.
  • They do not have a legend that explains the meaning of the different colors / series included.
  • In some cases, the ticks do not represent the values you want to represent.

purple-divider

Hands on!

In [1]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

%matplotlib inline
In [2]:
mtcars = pd.read_table('data/mtcars.txt', sep=',')
diamonds = pd.read_table('data/diamonds.txt', sep=',')

green-divider

 Axis names and graphic title

To include identifications in the axes and in the graph itself, matplotlib puts at our disposal three well-differentiated functions:

  • title: plot title.
  • xlabel: X axis label.
  • ylabel: Y axis label.
In [3]:
cuts = list(diamonds['cut'].unique())

x_values = [cuts.index(element) + np.random.uniform(-0.3, 0.3) for element in diamonds['cut']]
y_values = diamonds['price']

plt.scatter(x_values, y_values, color="black", alpha=0.1)

plt.ylim(-0.5, 20000)
plt.title("Price vs. Cut", weight="bold", size=15, color="0.3")
Out[3]:
Text(0.5,1,'Price vs. Cut')
In [4]:
cuts = list(diamonds['cut'].unique())

x_values = [cuts.index(element) + np.random.uniform(0, 0.6) for element in diamonds['cut']]
y_values = diamonds['price']

plt.scatter(x_values, y_values, color="black", alpha=0.1)

plt.xlim(-0.5, 5)
plt.ylim(-0.5, 20000)
plt.title("Price vs. Cut", weight="bold", size=15, color="0.3")
plt.xlabel("Cut", weight="bold", size=12)
plt.ylabel("Price", weight="bold", size=12)
Out[4]:
Text(0,0.5,'Price')

green-divider

 Axis ticks

Although the matplotlib graphs require numerical values in both axis, sometimes said numerical values refer to categorical values (discrete variables) and it is necessary to modify the labels of the assigned values in the axis.

In order to carry out this modification we have the following functions:

  • xticks: to modify X axis ticks.
  • yticks: to modify Y axis ticks.
In [5]:
cuts = list(diamonds['cut'].unique())

x_values = [cuts.index(element) + np.random.uniform(-0.3, 0.3) for element in diamonds['cut']]
y_values = diamonds['price']

plt.scatter(x_values, y_values, color="black", alpha=0.1)

plt.ylim(-0.5, 20000)
plt.title("Price vs. Cut", weight="bold", size=15, color="0.3")
plt.xlabel("Cut", weight="bold", size=12)
plt.ylabel("Price", weight="bold", size=12)
plt.xticks(range(0, 5), cuts, rotation=45)
Out[5]:
([<matplotlib.axis.XTick at 0x7ff5a12ba208>,
  <matplotlib.axis.XTick at 0x7ff5a12b6b38>,
  <matplotlib.axis.XTick at 0x7ff5a12b6898>,
  <matplotlib.axis.XTick at 0x7ff5a13504e0>,
  <matplotlib.axis.XTick at 0x7ff5a13509b0>],
 <a list of 5 Text xticklabel objects>)
In [6]:
cuts = list(diamonds['cut'].unique())

x_values = [cuts.index(element) + np.random.uniform(-0.3, 0.3) for element in diamonds['cut']]
y_values = diamonds['price']

plt.scatter(x_values, y_values, color="black", alpha=0.1)

plt.ylim(-0.5, 20000)
plt.title("Price vs. Cut", weight="bold", size=15, color="0.3")
plt.xlabel("Cut", weight="bold", size=12)
plt.ylabel("Price", weight="bold", size=12)
plt.xticks(range(0, 5), cuts, rotation=45)
plt.yticks(range(0, 20000, 5000))
Out[6]:
([<matplotlib.axis.YTick at 0x7ff5a1215f28>,
  <matplotlib.axis.YTick at 0x7ff5a12157f0>,
  <matplotlib.axis.YTick at 0x7ff5a12377f0>,
  <matplotlib.axis.YTick at 0x7ff5a147a668>],
 <a list of 4 Text yticklabel objects>)

green-divider

 Legends

In matplotlib it is necessary to include the legends manually since graphics "by default" do not include them.

matplotlib puts at our disposal the legend function for the rendering of legends and the management of their positioning, content, etc.

In [7]:
plt.scatter(mtcars[mtcars.am == 0].mpg,
            mtcars[mtcars.am == 0].hp,
            color='red', 
            marker=(5, 1, 0),
            s=100.)

plt.scatter(mtcars[mtcars.am == 1].mpg,
            mtcars[mtcars.am == 1].hp,
            color='blue',
            marker=(4, 1, 0),
            s=100.)

plt.legend()
Out[7]:
<matplotlib.legend.Legend at 0x7ff5a147a080>
In [8]:
series_1 = plt.scatter(mtcars[mtcars.am == 0].mpg,
                       mtcars[mtcars.am == 0].hp,
                       color='red', 
                       marker=(5, 1, 0),
                       s=100.)

series_2 = plt.scatter(mtcars[mtcars.am == 1].mpg,
                       mtcars[mtcars.am == 1].hp,
                       color='blue',
                       marker=(4, 1, 0),
                       s=100.)

plt.legend((series_1, series_2), ('Automatic', 'Manual'))
Out[8]:
<matplotlib.legend.Legend at 0x7ff5a11c49e8>
In [9]:
series_1 = plt.scatter(mtcars[mtcars.am == 0].mpg,
                       mtcars[mtcars.am == 0].hp,
                       color='red', 
                       marker=(5, 1, 0),
                       s=100.)

series_2 = plt.scatter(mtcars[mtcars.am == 1].mpg,
                       mtcars[mtcars.am == 1].hp,
                       color='blue',
                       marker=(4, 1, 0),
                       s=100.)

plt.legend((series_1, series_2),
           ('Automatic', 'Manual'),
           loc="lower center")
Out[9]:
<matplotlib.legend.Legend at 0x7ff5a11a4630>
In [10]:
series_1 = plt.scatter(mtcars[mtcars.am == 0].mpg,
                       mtcars[mtcars.am == 0].hp,
                       color='red', 
                       marker=(5, 1, 0),
                       s=100.)

series_2 = plt.scatter(mtcars[mtcars.am == 1].mpg,
                       mtcars[mtcars.am == 1].hp,
                       color='blue',
                       marker=(4, 1, 0),
                       s=100.)

plt.legend((series_1, series_2),
           ('Automatic', 'Manual'),
           ncol=2,
           loc="lower left")
Out[10]:
<matplotlib.legend.Legend at 0x7ff5a1174fd0>
In [11]:
series_1 = plt.scatter(mtcars[mtcars.am == 0].mpg,
                       mtcars[mtcars.am == 0].hp,
                       color='red', 
                       marker=(5, 1, 0),
                       s=100.)

series_2 = plt.scatter(mtcars[mtcars.am == 1].mpg,
                       mtcars[mtcars.am == 1].hp,
                       color='blue',
                       marker=(4, 1, 0),
                       s=100.)

plt.legend((series_1, series_2),
           ('Automatic', 'Manual'),
           title="Gear type")
Out[11]:
<matplotlib.legend.Legend at 0x7ff5a10e9cf8>

purple-divider

Notebooks AI
Notebooks AI Profile20060