Adding labels to boxplots in one graph (matplotlib)

Viewed 76
import numpy as np
import matplotlib.pyplot as plt

dict_of_ppg = {'1960': points_by_era(1960),
               '1980': points_by_era(1980),
               '2000' : points_by_era(2000),
               '2020': points_by_era(2020),
               '2040' : points_by_era(2040),
               '2060': points_by_era(2060),
               '2080': points_by_era(2080),
               '2100' : points_by_era(2100),
               '2120': points_by_era(2120),
               '2140': points_by_era(2140),
               '2160' : points_by_era(2160)}

ppg_by_era = pd.DataFrame.from_dict(dict_of_ppg)
plt.boxplot(ppg_by_era)
label = ['1960', '1980', '2000', '2020', '2040', '2060', '2080', '2100', '2120', '2140', '2160']
plt.title("Distribution of PPG Through Time")

This gives me eleven boxplots that are working just perfectly. However, I want the labels under each plot on the x-axis to correspond to the labels given in the label list.

How could I do so?

Thanks!

1 Answers

Use labels parameter:

label = ['1960', '1980', '2000', '2020', '2040', 
         '2060', '2080', '2100', '2120', '2140', '2160']
plt.boxplot(ppg_by_era, labels = label)
plt.title("Distribution of PPG Through Time")

Or use DataFrame.boxplot:

ppg_by_era.boxplot()
plt.title("Distribution of PPG Through Time")
Related