-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpython201_workshop_script.py
More file actions
222 lines (161 loc) · 4.74 KB
/
python201_workshop_script.py
File metadata and controls
222 lines (161 loc) · 4.74 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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
"""
Python Data Visualization
Data download:
https://figshare.com/ndownloader/articles/1314459/versions/10
Adapted from Data Carpentry:
https://datacarpentry.org/python-ecology-lesson/07-visualization-ggplot-python.html
Accompanying Slides:
https://docs.google.com/presentation/d/1Iy72RxX-J7O5kg-odqqzOw1DijCPijP9yoHmWCkvaeQ/edit?usp=drive_link
"""
# -----------------------------------
# Plotnine (ggplot-style plotting)
# -----------------------------------
import plotnine as p9
import pandas as pd
# Load and clean data
surveys_complete = pd.read_csv('data/surveys.csv')
surveys_complete = surveys_complete.dropna()
# -----------------------------------
# Basic Scatter Plot
# -----------------------------------
surveys_plot = p9.ggplot(
data=surveys_complete,
mapping=p9.aes(x='weight', y='hindfoot_length')
)
surveys_plot + p9.geom_point()
# -----------------------------------
# Adding elements step-by-step
# -----------------------------------
surveys_plot = p9.ggplot(
data=surveys_complete,
mapping=p9.aes(x='weight', y='hindfoot_length', color='species_id')
)
(
surveys_plot
+ p9.geom_point()
+ p9.xlab("Weight (g)")
+ p9.scale_x_log10()
+ p9.theme_bw()
+ p9.theme(text=p9.element_text(size=16))
)
# -----------------------------------
# Boxplot
# -----------------------------------
surveys_plot = p9.ggplot(
data=surveys_complete,
mapping=p9.aes(x='species_id', y='weight')
)
surveys_plot + p9.geom_boxplot()
# -----------------------------------
# Time Series Line Chart
# -----------------------------------
yearly_counts = surveys_complete.groupby(
['year', 'species_id']
)['species_id'].count()
yearly_counts = yearly_counts.reset_index(name='counts')
surveys_plot = p9.ggplot(
data=yearly_counts,
mapping=p9.aes(x='year', y='counts', color='species_id')
)
surveys_plot + p9.geom_line()
# -----------------------------------
# Split Plots (Facet Wrap)
# -----------------------------------
surveys_plot = p9.ggplot(
data=surveys_complete,
mapping=p9.aes(x='weight', y='hindfoot_length', color='species_id')
)
surveys_plot + p9.geom_point(alpha=0.1) + p9.facet_wrap("plot_id")
# -----------------------------------
# Facet Grid (rows ~ columns)
# -----------------------------------
survey_2000_2001 = surveys_complete[
surveys_complete["year"].isin([2000, 2001])
]
surveys_plot = p9.ggplot(
data=survey_2000_2001,
mapping=p9.aes(x='weight', y='hindfoot_length', color='species_id')
)
surveys_plot + p9.geom_point(alpha=0.1) + p9.facet_grid("year ~ sex")
# -----------------------------------
# Visualization Customizations
# -----------------------------------
surveys_plot = p9.ggplot(
data=surveys_complete,
mapping=p9.aes(x='factor(year)')
)
surveys_plot + p9.geom_bar()
surveys_plot + p9.geom_bar() + p9.theme_bw() + p9.theme(
axis_text_x=p9.element_text(angle=90)
)
# Custom theme
my_custom_theme = p9.theme(
axis_text_x=p9.element_text(color="grey", size=10, angle=90, hjust=.5),
axis_text_y=p9.element_text(color="grey", size=10)
)
surveys_plot = p9.ggplot(
data=surveys_complete,
mapping=p9.aes(x='factor(year)')
)
surveys_plot + p9.geom_bar() + my_custom_theme
# -----------------------------------
# Saving Plots
# -----------------------------------
my_plot = (
p9.ggplot(
data=surveys_complete,
mapping=p9.aes(
x='weight',
y='hindfoot_length',
color='species_id'
)
)
+ p9.geom_point()
)
my_plot.save("my_bar_graph.png", width=10, height=10, dpi=300)
# -----------------------------------
# matplotlib Examples
# -----------------------------------
import matplotlib.pyplot as plt
import numpy as np
surveys_complete = pd.read_csv('data/surveys.csv')
surveys_complete = surveys_complete.dropna()
x = surveys_complete.weight
y = surveys_complete.hindfoot_length
plt.scatter(x, y, s=10, c='black')
plt.show()
# -----------------------------------
# Category-wise coloring
# -----------------------------------
labels, index = np.unique(
surveys_complete.species_id,
return_inverse=True
)
scatter = plt.scatter(x, y, s=10, c=index)
plt.legend(
scatter.legend_elements(num=None)[0],
labels,
ncol=6,
loc='upper left',
bbox_to_anchor=(-0.05, 1.15)
)
plt.xlabel("Weight (g)")
plt.xscale("log")
plt.show()
# -----------------------------------
# Boxplots with matplotlib
# -----------------------------------
data = []
labels = []
for element in np.unique(surveys_complete.species_id):
data.append(
surveys_complete.loc[
surveys_complete['species_id'] == element,
'weight'
].to_numpy()
)
labels.append(element)
plt.boxplot(data, labels=labels)
plt.xlabel("Species ID")
plt.ylabel("Weight distribution")
plt.show()