-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyplot-images.py
More file actions
49 lines (39 loc) · 1.47 KB
/
pyplot-images.py
File metadata and controls
49 lines (39 loc) · 1.47 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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"))
del alco["Total"]
columns, years = alco.unstack().columns.levels
# The state abbreviations come straight from the file
states = pd.read_csv(
"states.csv",
names=("State", "Standard", "Postal", "Capital"))
states.set_index("State", inplace=True)
# Alcohol consumption will be sorted by year 2009
frames = [pd.merge(alco[column].unstack(), states,
left_index=True, right_index=True).sort_values(2009)
for column in columns]
# How many years are covered?
span = max(years) - min(years) + 1
# Select a good-looking style
matplotlib.style.use("ggplot")
STEP = 5
# Plot each frame in a subplot
for pos, (draw, style, column, frame) in enumerate(zip(
(plt.contourf, plt.contour, plt.imshow), # (1)
(plt.cm.autumn, plt.cm.cool, plt.cm.spring),
columns, frames)):
# Select the subplot with 2 rows and 2 columns
plt.subplot(2, 2, pos + 1) # (2)
# Plot the frame
draw(frame[frame.columns[:span]], cmap=style, aspect="auto") # (3)
# Add embellishments
plt.colorbar() # (4)
plt.title(column)
plt.xlabel("Year")
plt.xticks(range(0, span, STEP), frame.columns[:span:STEP])
plt.yticks(range(0, frame.shape[0], STEP), frame.Postal[::STEP])
plt.xticks(rotation=-17)
plt.tight_layout()
plt.savefig("../images/pyplot-all.pdf")
#plt.show()