-
-
Save JesseLivezey/afd0b3fc6ba34ea0efc9d529210f1f28 to your computer and use it in GitHub Desktop.
population plot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
pops = np.array([39538223, | |
29145505, | |
21538187, | |
20201249, | |
13011844, | |
12812508, | |
11799448, | |
10711908, | |
10439388, | |
10077331, | |
9288994, | |
8631393, | |
7705281, | |
7151502, | |
7029917, | |
6910840, | |
6785528, | |
6177224, | |
6154913, | |
5893718, | |
5773714, | |
5706494, | |
5118425, | |
5024279, | |
4657757, | |
4505836, | |
4237256, | |
3959353, | |
3605944, | |
3271616, | |
3190369, | |
3104614, | |
3011524, | |
2961279, | |
2937880, | |
2117522, | |
1961504, | |
1839106, | |
1793716, | |
1455271, | |
1377529, | |
1362359, | |
1097379, | |
1084225, | |
989948, | |
886667, | |
779094, | |
733391, | |
643077, | |
576851]) | |
cumulative_pop = np.concatenate([np.array([0]), np.cumsum(np.sort(pops))]) | |
total = cumulative_pop.max() | |
fig, ax = plt.subplots(1, figsize=(5, 5)) | |
for ii in range(50): | |
start_frac = 100*cumulative_pop[ii] / total | |
end_frac = 100*cumulative_pop[ii+1] / total | |
start_sen = ii*2 | |
end_sen = (ii+1)*2 | |
x = np.linspace(start_frac, end_frac, 10) | |
y = np.linspace(start_sen, end_sen, 10) | |
ax.plot(x, y, c='k') | |
ax.scatter(end_frac, end_sen, marker='.', c='k', s=10) | |
if (ii % 10) == 0 and (ii > 0): | |
ax.annotate(f'{int(np.around(end_frac))}%, {end_sen}', | |
(end_frac, end_sen), | |
(end_frac + 20, end_sen - 20), | |
arrowprops={'arrowstyle': '->'}) | |
ax.set_xlim(0, 100) | |
ax.set_ylim(0, 100) | |
ax.plot([0, 100], [0, 100], '--', c='k') | |
ax.set_xlabel('Percent of US population', fontsize=12) | |
ax.set_ylabel('Number of US senators', fontsize=12) | |
fig.tight_layout() | |
plt.savefig('pop_vs_senate.png', dpi=300) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment