-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyplot-legend.py
More file actions
30 lines (24 loc) · 856 Bytes
/
pyplot-legend.py
File metadata and controls
30 lines (24 loc) · 856 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import matplotlib, matplotlib.pyplot as plt
import pickle, pandas as pd
# The NIAAA frame has been pickled before
alco = pickle.load(open("alco.pickle", "rb"))
# Select the right data
BEVERAGE = "Beer"
years = alco.index.levels[1]
states = ("New Hampshire", "Colorado", "Utah")
# Select a good-looking style
plt.xkcd()
matplotlib.style.use("ggplot")
# Plot the charts
for state in states:
ydata = alco.ix[state][BEVERAGE]
plt.plot(years, ydata, "-o")
# Add annotations with arrows
plt.annotate(s="Peak", xy=(ydata.argmax(), ydata.max()),
xytext=(ydata.argmax() + 0.5, ydata.max() + 0.1),
arrowprops={"facecolor": "black", "shrink": 0.2})
# Add labels and legends
plt.ylabel(BEVERAGE + " consumption")
plt.title("And now in xkcd...")
plt.legend(states)
plt.savefig("../images/pyplot-legend-xkcd.pdf")