-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3DNPS.py
229 lines (186 loc) · 7.23 KB
/
3DNPS.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
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
'''This script creates a 3D scatterplot using Acreage, Camping, and Visitor Statistic data for each National Park in 2017.'''
import pandas as pd
from numpy import nan
from plotly.offline import init_notebook_mode, iplot
data = pd.read_csv('Data/NPSAnnualSummary.csv', skiprows= [0,1,2]) #Downloaded from IRMA, with the desired categories selected
data.drop(['TotalRecreationVisitors', 'TotalNonRecreationVisitors', 'TotalRecreationHours', 'TotalNonRecreationHours',
'TotalConcessionerLodging', 'TotalConcessionerCamping' , 'TotalTentCampers',
'TotalRVCampers', 'TotalBackcountry', 'TotalNonRecreationOvernightStays',
'TotalMiscellaneousOvernightStays' ], axis = 1, inplace = True)
#formatting stuff, remove unecessary rows
data.drop(data.index[4541:4660], inplace = True)
newData1 = data.apply(lambda x: x.str.replace(',',''))
newData2 = newData1.apply(pd.to_numeric, errors='ignore', downcast = 'float') #Convert strings to numbers
newData = newData2.fillna(0)
newData ['RecreationVisits'] = newData['RecreationVisitors']
newData['TotalVisits'] = newData['RecreationVisitors'] + newData['NonRecreationVisitors'] #Sum Total Visits of Each Park
newCampers = newData
newCampers['TotalCampers'] = newCampers['ConcessionerCamping']+ newCampers['TentCampers'] + newCampers['RVCampers'] + newCampers['Backcountry']
#print newCampers
newData.to_csv('Data/UnorganizedNPS.csv', sep=',')
TotalCampers = newCampers.pivot(index='Year', columns = 'ParkName')['TotalCampers']
TotalCampers.fillna(value=nan, inplace=True) #Convert None Values to Nan
TotalCampers = TotalCampers.T
TotalCampers['5YearCamperAvg'] = (TotalCampers[2016] + TotalCampers[2015] + TotalCampers[2014] + TotalCampers[2013] + TotalCampers[2013])//5
#print TotalCampers
TotalHours = newData.pivot(index='Year', columns = 'ParkName')['RecreationHours']
TotalHours.fillna(value=nan, inplace=True) #Convert None Values to Nan
TotalHours = TotalHours.T
TotalHours['5YearHoursAvg'] = (TotalHours[2016] + TotalHours[2015] + TotalHours[2014] + TotalHours[2013] + TotalHours[2013])//5
#print TotalHours
D = newData.pivot(index='Year', columns = 'ParkName')['RecreationVisits'] #pivot table for plotting
D.fillna(value=nan, inplace=True) #Convert None Values to Nan
#D['TotalVisits'] = D.sum(axis=1) #Some total visits of all National Parks
D = D.T
D['5YearVisitAvg'] = (D[2016.0] + D[2015.0] + D[2014.0] + D[2013.0] + D[2013.0])//5
Acres = pd.read_csv('Data/NPSAcres.csv')
print Acres
Acres = Acres.drop(Acres.index[59]) #remove Total Acreage
Acres = Acres.set_index('ParkName')
Acres['2016Visits'] = D[2016]
Acres['5YearVisitAvg'] = D['5YearVisitAvg']
Acres['2016Campers'] = TotalCampers[2016]
Acres['5YearCamperAvg'] = TotalCampers['5YearCamperAvg']
Acres['2016Hours'] = TotalHours[2016]
Acres['5YearHoursAvg'] = TotalHours['5YearHoursAvg']
#print Acres
#print D
#Acres.drop(['Great Smoky Mountains NP'], inplace = True)
#Acres.drop(['Yosemite NP'], inplace = True)
Parks2 = list(Acres.index)
#Converts the numbers into a more readable format
def human_format(num):
magnitude = 0
while abs(num) >= 1000:
magnitude += 1
num /= 1000.0
# add more suffixes if you need them
return '%.2f%s' % (num, ['', 'K', 'M', 'G', 'T', 'P'][magnitude])
#print "Parks 2: ", Parks2
i=0;
text = []
mobiletext = []
#Create custom hover text for the plot. THIS TOOK FOREVER TO FIGURE OUT
for p in Parks2:
print Acres.loc[p]['State'] + "'"
text.append(p + "<br>Visitors: " + human_format(Acres.loc[p]['2016Visits']) + "<br>Acres: " + human_format(Acres.loc[p]['Acres']) + "<br>Campers: " + human_format(Acres.loc[p]['2016Campers']))
mobiletext.append(p)
i+=1
text.append("NOT WPORKIONG")
#Mobile text was only creating the gif for the mobile version of the data visualization
#print mobiletext
#print text
#Plotly formatting code
trace1 = {
"x" : Acres['Acres'],
'y' : Acres['2016Campers'],
"z" : Acres['2016Visits'],
"marker": {
#"cauto": True,
#"cmax": 2.31428917959,
#"cmin": -2.39332076244,
#"color" : np.random.randn(500),
"color" : "rgba(102,51,153,1)",
#"colorscale": "Viridis"
"size": 8,
},
"mode": "markers",
"type": "scatter3d",
"name" : "TESTING",
"hoverinfo" : "text",
#"mode" : "",
"text" : text, # "{:,}".format(str(Acres['Acres'])),
#"uid": "47d11b"
}
#data = Data([trace1])
data = [trace1]
layout = {
"margin": {
"r": 0,
"t": 0,
"b": 0,
"l": 0
},
"scene": {
"zaxis": {
"title": "Total Visitors ",
"type": "log"
},
"xaxis": {
"title": "Total Acreage ",
"type": "log"
},
"yaxis": {
"title": "Total Campers ",
"type": "log"
}
},
"title": "Hello"
}
'''Offline mode was not working embedded on the website'''
#fig = Figure(data=data, layout=layout,)
#offline.plot(fig, filename= "3DNPS.html")s
import plotly
import plotly.plotly as py
import plotly.graph_objs as go
#Add in your own credentials
plotly.tools.set_credentials_file(username='USER', api_key='API-KEY')
fig = go.Figure(data=data, layout=layout)
#offline.plot(fig, filename= "HTMLGraphs/3DNPS.html")
py.iplot(fig, filename='3DNPS')
iplot(fig)
#This is Bokeh code to create a 2D scatter plot with Acreage vs. Visitors. Not used for the final Data Visualization
'''
#D.to_csv('NPSAcres.csv', sep=',')
source = ColumnDataSource(Acres)
#p.select_one(HoverTool).tooltips = [
# ('Hi', '@TotalVisits'),
#]
hover = HoverTool(
tooltips=[
( 'Park','@index'),
( 'Acres','@Acres{0,0}'),
( 'Visitors','@5YearVisitAvg{0,0}')
#( 'Campers','@2016Hours')
],
)
Purp = plasma(20)
Purp = Purp[::-1]
Purp.pop(0)
Purp.pop(0)
Purp.pop(0)
Purp.pop(0)
Purp.pop(0)
colors = ["#75968f", "#a5bab7", "#c9d9d3", "#e2e2e2", "#dfccce", "#ddb7b1", "#cc7878", "#933b41", "#550b1d"]
mapper = LinearColorMapper(palette=Purp, low=Acres['5YearCamperAvg'].min(), high=Acres['5YearCamperAvg'].max())
p = figure(plot_width = 700, plot_height = 600, title="NPS Visitors vs. Acreage", tools = [hover], x_axis_type="log",
y_axis_type='log')
p.circle(source = source, x = 'Acres', y = '5YearVisitAvg', fill_color={'field': '5YearCamperAvg', 'transform': mapper},
line_color=None, size = 9)
color_bar = ColorBar(title= '# of Campers', title_text_font_size = "7pt",color_mapper=mapper, major_label_text_font_size="5pt",
ticker=BasicTicker(desired_num_ticks=len(Purp)-4),
formatter=NumeralTickFormatter(format="0,0"),
border_line_color=None, location=(0, 0), orientation = 'vertical')
p.yaxis.formatter=NumeralTickFormatter(format="0,0")
p.xaxis.formatter=NumeralTickFormatter(format="0,0")
p.yaxis.axis_label = '5 Year Visitor Avg'
p.xaxis.axis_label = 'Acres'
p.toolbar_location = None
p.add_layout(color_bar, 'right')
output_file("NPSAcreage.html", title="Acres")
show(p)
'''
'''
import plotly.offline as offline
from plotly.graph_objs import *
Acres = Acres[Acres.Acres != 0 ]
Acres = Acres[Acres['5YearVisitAvg'] != 0 ]
Acres = Acres[Acres['5YearCamperAvg'] != 0 ]
Parks = list(Acres.index)
Parks = Parks[:-1]
Final = ""
for p in Parks:
temp = "\"" + p + "<br>Fuck" + "\"," # + str(Acres.Acres[p]) + "<br>Campers: " + str(Acres['5YearCamperAvg'][p]) + ","
Final = Final + temp
print Final
'''