Skip to content

Instantly share code, notes, and snippets.

@anishazaveri

anishazaveri/tufte5.py

Last active May 30, 2020
Embed
What would you like to do?
# create data
x = list(range(1951, 1960))
data = []
data.append({'label': 'New York', 'x': x, 'y': [
13.8, 13.6, 14.3, 13, 13.6, 13.5, 13.4, 13, 12.6]})
data.append({'label': 'Massachusetts', 'x': x, 'y': [
10.1, 10, 11.5, 10.8, 11.7, 11.50, 10.1, 11.8, 10.7]})
data.append({'label': 'Connecticut', 'x': x, 'y': [
12.7, 10.9, 12.5, 11, 14.3, 12.4, 11.8, 10.2, 9.7]})
data.append({'label': 'Rhode Island', 'x': x, 'y': [
8, 8.2, 8.2, 8, 10.4, 8.2, 8.4, 8.5, 9]})
# initialize figure
fig, ax = plt.subplots(figsize=(6, 6))
# remove splines
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
# set spline bounds
ax.spines['left'].set_bounds(8, 16)
ax.spines['bottom'].set_bounds(1951, 1959)
# set axis limits
ax.set_ylim(7, 17)
ax.set_xlim(1950, 1960)
# set axis ticks
ax.xaxis.set_ticks(x)
ax.yaxis.set_ticks(list(range(8, 18, 2)))
# make ticks inward facing
ax.xaxis.set_tick_params(direction='in')
ax.yaxis.set_tick_params(direction='in')
# plot data
for data_dict in data:
x = data_dict['x']
y = data_dict['y']
label = data_dict['label']
ax.scatter(x, y, s=8, color='black', zorder=3)
ax.scatter(x, y, s=64, color='white', zorder=2)
if label == 'Connecticut':
linestyle = 'solid'
ax.plot([1955, 1956], [y[x.index(1955)], y[x.index(1956)]],
color='black', linewidth=2, zorder=1, linestyle=linestyle)
else:
linestyle = (0, (5, 10))
ax.plot(x, y, color='black', zorder=1, linewidth=0.7, linestyle=linestyle)
ax.text(x[-1]+0.5, y[-1], label, va='center')
ax.text(1957.5, 15.5, 'Traffic Deaths per 100,000\nPersons in Connecticut,\nMassachusetts, Rhode Island,\nand New York, 1951-1959', va='center')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.