-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_with_plotly.py
More file actions
281 lines (238 loc) · 10.9 KB
/
plot_with_plotly.py
File metadata and controls
281 lines (238 loc) · 10.9 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#==============================================================================
# Do simple plots through plot.ly, such as line plot, histogram, monthly means
# Function names:
# timeseries_plot()
# wind_rose_plot()
# species_histogram()
# monthly_box_plots()
# hourly_box_plots()
#==============================================================================
# Uses modules:
# datetime, numpy, pandas, plot.ly, source_AQ_data, windrose, quick_tools,
# calendar
from datetime import datetime, timedelta
import pandas as pd
import numpy as np
import source_AQ_data
import plotly.plotly as py
import plotly.graph_objs as go
import quick_tools
import calendar
#==============================================================================
def timeseries_plot(species='None',filename = 'ExampleData', average = 'None',
verified = True):
"""
Produces a simple line plot of concentration against time.
Function IN:
species (REQUIRED, STRING):
The name of the species you want to plot.
filename(OPTIONAL, STRING):
The name of the raw data file, if left it just uses
example data
average(OPTIONAL, STRING):
Choose what type of averaging to use. Choices are None (default),
8-hour, daily, weekly, monthly
verfied(OPTIONAL, BOOLEAN):
Choose whether to plot just verfied data or all data.
Default = True
"""
# Get the data for the species required. Also include the filename
# if the filename is provided - use example data if not.
# Also returns variable name (this might be changed slightly from user input)
species_data, variablename = source_AQ_data.select_one_variable(species, filename)
# If just using verfied data then purge unverified data
if verified:
species_data = source_AQ_data.purge_unverified(species_data, variablename)
# Set the data in a format the plot.ly needs to work
data = go.Scatter(x = species_data.index, y = species_data[variablename],
mode = 'markers')
data = [data]
# Set the layout for the plot.ly graph
layout = go.Layout(
title = 'Concentration of ' + variablename + ' at Edinburgh St leonards',
xaxis = dict(title = 'Date and Time'),
yaxis = dict(title = variablename + ' ('+species_data['Unit'][0]+')'))
# Set the filename & combine data and layout
filename = '%s_timeseries' % variablename
fig = go.Figure(data = data, layout = layout)
py.plot(fig,filename = filename)
pass
def wind_rose_plot(filename = 'ExampleData'):
"""
Procuduces a wind rose plot of wind speed and direction.
Function IN:
filename (OPTIONAL, STRING):
The filename of a csv file where this data is kept. If not
provided then uses the example file.
"""
# Import the windrose module
from windrose import windrose
# Get the wind direction and the wind speed from the file
wd, wd_name = source_AQ_data.select_one_variable('Modelled Wind Direction',
filename = filename)
ws, ws_name = source_AQ_data.select_one_variable('Modelled Wind Speed',
filename = filename)
# Combine the two DataFrames
wind = pd.concat([wd,ws], axis = 1)
# Drop NaNs
wind.dropna(inplace = True)
# Format the windspeeds and directions into windrose format
dirc_bins = 8
speed_bins = 4
windrose_data, speed_bin_names = windrose(wind['Modelled Wind Speed'],
wind['Modelled Wind Direction'], direction_bin_size = dirc_bins,
speed_bin_size = speed_bins)
# Make each circle of the windrose a "trace" for plot.ly
colours = quick_tools.get_colours_rgb(num_colours = len(speed_bin_names))
# Need to do it in reverse order so the larger ones don't cover the smaller
trace_dict = {}
finished_data = []
for x,sn in enumerate(reversed(speed_bin_names)):
trace_dict[sn] = go.Area(
r = windrose_data[sn],
t = windrose_data['Direction'],
name = sn,
marker = dict(color=colours[x]))
finished_data.append(trace_dict[sn])
# Set the layout options
layout = go.Layout(
title = 'Wind Speed Distribution at Edinburgh St Leonards',
font = dict(size = 16),
legend = dict(font = dict(size = 16)),
radialaxis = dict(ticksuffix = '%'),
orientation = 270)
filename = 'Wind Speed Distribution at Edinburgh St Leonards'
fig = go.Figure(data = finished_data, layout = layout)
py.plot(fig, filename = filename)
pass
def species_histogram(filename='ExampleData', species = 'None', verified = True,
num_bins = 50):
"""
Produces a histogram of concentrationof a given species.
Function IN:
species (REQUIRED, STRING):
The name of the species you want to plot.
filename(OPTIONAL, STRING):
The name of the raw data file, if left it just uses
example data
verfied(OPTIONAL, BOOLEAN):
Choose whether to plot just verfied data or all data.
Default = True
num_bins (OPTIONAL, INTEGER):
Choose the number of bins for the plot. Default = 50
"""
# Get the data for the species required. Also include the filename
# if the filename is provided - use example data if not.
# Also returns variable name (this might be changed slightly from user input)
species_data, variablename = source_AQ_data.select_one_variable(species, filename)
# If just using verfied data then purge unverified data
if verified:
species_data = source_AQ_data.purge_unverified(species_data, variablename)
data = [go.Histogram(x = species_data[variablename],
nbinsx = num_bins)]
layout = go.Layout(
title = 'Histogram of %s concentration at Edinburgh St Leonards' % variablename,
yaxis = dict(title = 'Frequency'),
xaxis = dict(title = variablename + ' ' + species_data.Unit[0]),
showlegend = False)
fig = go.Figure(data = data, layout = layout)
filename = 'Histogram of %s at Edinburgh St Leonards' % variablename
py.plot(fig, filename = filename)
pass
def monthly_box_plots(filename = 'ExampleData', species='None',
verified = True):
"""
Produces box plots of mean, std dev, percentiles of species per month.
Function IN:
filename (OPTIONAL, STRING):
The filepath and name where the csv file is stored. If not path
is given the the example data is used.
species (OPTIONAL, STRING):
The species you wish to plot. If this is left as 'None' then it
will go to the selecting tool. (Will also do this if the species
written is not available.)
verified (OPTIONAL, BOOLEAN):
Choose whether to use all the data (False) or just the verified
data (True). Default is True.
"""
# Get the data for the species required. Also include the filename
# if the filename is provided - use example data if not.
# Also returns variable name (this might be changed slightly from user input)
species_data, variablename = source_AQ_data.select_one_variable(species, filename)
# If just using verfied data then purge unverified data
if verified:
species_data = source_AQ_data.purge_unverified(species_data, variablename)
# Get a list of months
month_names = [calendar.month_name[x] for x in range(1,13)]
# Create dictionary with each month as a key containing all monthly data
monthly_dict = {}
for n,month in enumerate(month_names):
monthly_dict[month] = species_data.loc[species_data.index.month == (n + 1)]
# Create list to put in the plot.ly traces and combine them
# to send to plot.ly
box_data = []
for x, month in enumerate(month_names):
box_data.append( go.Box(
y = monthly_dict[month][variablename].values,
name = month))
layout = go.Layout(
yaxis = dict( title = variablename + ' ' + species_data.Unit[0]),
showlegend = False,
title = 'Monthly Averages for '+ variablename +' at Edinburgh St Leonards')
# Create filename and send to plot.ly
filename = 'Box Plot of Monthly Average %s' % variablename
fig = go.Figure(data = box_data, layout = layout)
py.plot(fig, filename = filename)
pass
def hourly_box_plots(filename = 'ExampleData', species = 'None', verified = True):
"""
Produces box plots of mean, std dev, percentiles of species for
hour of the day.
Function IN:
filename (OPTIONAL, STRING):
The filepath and name where the csv file is stored. If not path
is given the the example data is used.
species (OPTIONAL, STRING):
The species you wish to plot. If this is left as 'None' then it
will go to the selecting tool. (Will also do this if the species
written is not available.)
verified (OPTIONAL, BOOLEAN):
Choose whether to use all the data (False) or just the verified
data (True). Default is True.
"""
# Get the data for the species required. Also include the filename
# if the filename is provided - use example data if not.
# Also returns variable name (this might be changed slightly from user input)
species_data, variablename = source_AQ_data.select_one_variable(species, filename)
# If just using verfied data then purge unverified data
if verified:
species_data = source_AQ_data.purge_unverified(species_data, variablename)
# Get a list of hours
hour_names = [x for x in range(24)]
# Create dictionary with each month as a key containing all monthly data
hourly_dict = {}
for n,hour in enumerate(hour_names):
hourly_dict[hour] = species_data.loc[species_data.index.hour == (n)]
# Create list to put in the plot.ly traces and combine them
# to send to plot.ly
box_data = []
for x, hour in enumerate(hour_names):
box_data.append( go.Box(
y = hourly_dict[hour][variablename].values,
name = str(hour).zfill(2)))
layout = go.Layout(
yaxis = dict( title = variablename + ' ' + species_data.Unit[0]),
xaxis = dict( title = 'Hour of the Day'),
showlegend = False,
title = 'Hourly Averages for '+ variablename +' at Edinburgh St Leonards')
# Create filename and send to plot.ly
filename = 'Box Plot of Hourly Average %s' % variablename
fig = go.Figure(data = box_data, layout = layout)
py.plot(fig, filename = filename)
pass
if __name__ == '__main__':
#data = source_AQ_data.select_one_variable('Ozone')
wind_rose_plot()
## ============================================================================
## END OF PROGAM
## ============================================================================