repl.it
@justinholman/

SeabornCompareDistributions

Python

No description

fork
loading
Files
  • main.py
  • 01-swarm.png
  • 02-box.png
  • 03-strip.png
  • 04-stripj.png
  • 05-strip-d1.png
  • 06-strip-jd2.png
  • 07-strip51.png
  • 08-box51.png
  • e08div.csv
main.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# first we import matplotlib and use('Agg') so we can "savefig" below
import matplotlib as mpl
mpl.use('Agg')

# now lets import the other modules we need
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# with all our modules in place, let's read csv data
e08 = pd.read_csv('e08div.csv') 

# make a swarmplot to show dist of vote by census region
_ = sns.swarmplot(x='Region', y='dem_share', data=e08)
_ = plt.xlabel('Region')
_ = plt.ylabel('% Vote to Obama')
plt.show()
plt.savefig('01-swarm.png')
plt.clf()

# try boxplots 
sns.boxplot(x='Region', y='dem_share', data=e08, palette="PRGn")
_ = plt.xlabel('Region')
_ = plt.ylabel('% Vote to Obama')
plt.show()
plt.savefig('02-box.png')
plt.clf()

# try stripplots
_ = sns.stripplot(x='Region', y='dem_share', data=e08)
_ = plt.xlabel('Region')
_ = plt.ylabel('% Vote to Obama')
plt.show()
plt.savefig('03-strip.png')
plt.clf()

# add jitter
_ = sns.stripplot(x='Region', y='dem_share', data=e08, jitter=True)
_ = plt.xlabel('Region')
_ = plt.ylabel('% Vote to Obama')
plt.show()
plt.savefig('04-stripj.png')
plt.clf()

# try stripplots with Divisions
_ = sns.stripplot(x='Division', y='dem_share', data=e08)
_ = plt.xlabel('Division')
_ = plt.ylabel('% Vote to Obama')
plt.show()
plt.savefig('05-strip-d1.png')
plt.clf()

# add jitter
_ = sns.stripplot(x='Division', y='dem_share', data=e08, jitter=True)
_ = plt.xlabel('Division')
_ = plt.ylabel('% Vote to Obama')
plt.show()
plt.savefig('06-strip-jd2.png')
plt.clf()

# can we fit all 50 States on a strip plot?
_ = sns.stripplot(x='state', y='dem_share', data=e08)
_ = plt.xlabel('Region')
_ = plt.ylabel('% Vote to Obama')
plt.show()
plt.savefig('07-strip51.png')
plt.clf()

# how about all 50 states on a horizontal boxplot?
sns.boxplot(x='dem_share', y='state', data=e08, orient="h")
_ = plt.xlabel('% Vote to Obama')
_ = plt.ylabel('State')
plt.show()
plt.savefig('08-box51.png')
plt.clf()