Profile picture

Annotations and Legends

Last updated: March 27th, 20192019-03-27Project 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 [ ]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

%matplotlib inline
In [ ]:
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 [ ]:
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")
In [ ]:
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)

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 [ ]:
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)
In [ ]:
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))

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 [ ]:
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()
In [ ]:
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'))
In [ ]:
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")
In [ ]:
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")
In [ ]:
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")

purple-divider

Notebooks AI
Notebooks AI Profile20060