-
Notifications
You must be signed in to change notification settings - Fork 0
/
p3.py
294 lines (241 loc) · 11.3 KB
/
p3.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
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
282
283
284
285
286
287
288
289
290
291
292
293
import pandas as pd
import numpy as np
import plotly.express as px
import requests
import folium
import plotly.figure_factory as ff
import streamlit as st
from streamlit_option_menu import option_menu
from streamlit_folium import folium_static
import plotly.graph_objects as go
from PIL import Image
import subprocess
# Set page config to wide layout
st.set_page_config(page_title="Población de carros eléctricos en Washington", layout="wide", page_icon="https://cdn-icons-png.flaticon.com/512/1996/1996729.png")
col1, col2, col3 = st.columns(3)
col1.metric("Clean source", "70 %", "-30%")
col2.metric("Tesla population", "50%", "-5%")
col3.metric("King County", "50%", "2.4%")
bg_img = '''
<style>
[data-testid="stAppViewContainer"] {
background-image: url('https://images.rawpixel.com/image_800/cHJpdmF0ZS9sci9pbWFnZXMvd2Vic2l0ZS8yMDIyLTA1L3BmLXMxMjQtYWstMjY4MV8yLmpwZw.jpg');
background-size: cover;
background-repeat: no-repeat;
color: black;
}
</style>
'''
st.markdown(bg_img, unsafe_allow_html=True)
image = Image.open('UK.png')
# Importing Google Fonts
st.markdown(
"""
<link href='https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap' rel='stylesheet'>
<style>
.st-cc h1 {
color: black;
font-family: 'Roboto', sans-serif;
}
</style>
""",
unsafe_allow_html=True
)
# Mostrar el título in a more professional font
st.markdown('<h1 style="color: black; font-family: \'Buffalo\';">Población de carros eléctricos en Washington</h1>', unsafe_allow_html=True)
# Contenido del sidebar
selected2 = option_menu(None, ["Home", "Map", "KPIs", 'References'],
icons=['house', 'map', "chart-pie", 'book'],
menu_icon="cast", default_index=0, orientation="horizontal")
selected2
# Load data and perform preprocessing
#mapa
df = pd.read_excel('Electric_Vehicle_Population_Data.xlsx')
fips_df = pd.read_csv('FIP.csv')
df = df.dropna()
df.rename(columns={"Electric Vehicle Type": "elec", "Clean Alternative Fuel Vehicle (CAFV) Eligibility": "clean"}, inplace=True)
df.replace({'Plug-in Hybrid Electric Vehicle (PHEV)': 'PHEV', 'Battery Electric Vehicle (BEV)': 'BEV',
'Clean Alternative Fuel Vehicle Eligible': 'Clean Alt Fuel', 'Eligibility unknown as battery range has not been researched': 'Unknown'
, 'Not eligible due to low battery range': 'Not Clean'}, inplace=True)
county_counts = df['County'].value_counts().to_dict()
df['Count'] = df['County'].map(county_counts)
df3 = pd.merge(df,fips_df, on='County')
state_geo = requests.get("https://raw.githubusercontent.com/python-visualization/folium/master/tests/us-counties.json").json()
excel_url = "https://github.com/SVterry2023/al/raw/main/df3.xlsx"
state_data = pd.read_excel(excel_url, engine="openpyxl")
m = folium.Map(location=[0, 0], zoom_start=1)
#Frecuencias absolutas
tfa = pd.Series(df['clean']).value_counts()
tt= pd.DataFrame(tfa)
#Tabla de frecuencias relativas
t = df['clean'].shape[0]
tfr = tfa/t
tf = pd.DataFrame(tfr)
tf['name'] = tf.index
#Frecuencias absolutas
tfm = pd.Series(df['Make']).value_counts()
tm= pd.DataFrame(tfm)
#Tabla de frecuencias relativas
tm = df['Make'].shape[0]
tfrm = tfm/tm
tfm = pd.DataFrame(tfrm)
tfm['name'] = tfm.index
top_5_values = tfm.head(5)
#Frecuencias absolutas
tfz = pd.Series(df['County']).value_counts()
tz= pd.DataFrame(tfz)
#Tabla de frecuencias relativas
tz = df['County'].shape[0]
tfrz = tfz/tz
tfz = pd.DataFrame(tfrz)
tfz['name'] = tfz.index
top_5 = tfz.head(5)
if selected2 == "Home":
heading_text = '<h2 style="color: black;"> La situación actual</h2>'
st.markdown(heading_text, unsafe_allow_html=True)
st.markdown('<p style="font-family: \'Times New Roman\'; font-size: 18px; color: black;"> La producción de carros eléctricos ha aumentado, pero esto no necesariamente representa una mejora para el medio ambiente .</p>', unsafe_allow_html=True)
st.image("UK.png")
def compute_Sankey_chart():
# Extract unique values
elec_values = df['elec'].unique()
clean_values = df['clean'].unique()
# Create a list of unique labels
unique_labels = list(elec_values) + list(clean_values)
# Map labels to indices
label_indices = {label: idx for idx, label in enumerate(unique_labels)}
node_colors = ["rgba(0, 128, 255, 0.8)", "rgba(0, 255, 0, 0.8)", "rgba(255, 0, 0, 0.8)", "rgba(169, 169, 169, 0.8)"]
# Create Sankey diagram data
source_indices = [label_indices[elec] for elec in df['elec']]
target_indices = [label_indices[clean] for clean in df['clean']]
link_values = [1] * len(df)
# Create Sankey diagram
fig = go.Figure(data=[go.Sankey(
valueformat=".0f",
valuesuffix="TWh",
node=dict(
pad=15,
thickness=15,
line=dict(color="black", width=0.5),
label=unique_labels,
color=node_colors
),
link=dict(
source=source_indices,
target=target_indices,
value=link_values,
color="rgba(173, 216, 230, 0.5)"
))])
fig.update_layout({
'plot_bgcolor': 'rgba(0, 0, 0, 0)',
'paper_bgcolor': 'rgba(0, 0, 0, 0)',
'margin': dict(l=0, r=0, b=0, t=0, pad=0),
'height': 500, # Adjust the height as needed
'width': 800, # Adjust the width as needed
})
return fig
fig4 = compute_Sankey_chart()
heading_text11 = '<h3 style="color: black;"> Diagrama diluvial</h3>'
st.markdown(heading_text11, unsafe_allow_html=True)
st.plotly_chart(fig4, use_container_width=True)
if selected2== 'References':
heading_text1 = '<h2 style="color: black;">Referencias</h2>'
st.markdown(heading_text1, unsafe_allow_html=True)
st.markdown('<p style="font-family: \'Times New Roman\'; font-size: 18px; color: black;"> Gupta, S. (2021). Electric Vehicle Population Data. Kaggle. https://www.kaggle.com/code/shubhamgupta012/electric-vehicle-population-data .</p>', unsafe_allow_html=True)
st.markdown('<p style="font-family: \'Times New Roman\'; font-size: 18px; color: black;"> Tabuchi, H. (2021, March 2). Electric Vehicles Are Better for the Environment. The New York Times. https://www.nytimes.com/2021/03/02/climate/electric-vehicles-environment.html .</p>', unsafe_allow_html=True)
if selected2 == "Map":
folium.Choropleth(
geo_data=state_geo,
name="choropleth",
data=state_data,
columns=["FIP", "Count"],
key_on="feature.id",
fill_color="YlGn",
fill_opacity=0.7,
line_opacity=0.2,
legend_name="Cantidad de carros eléctricos",
).add_to(m)
folium.LayerControl().add_to(m)
heading_text2 = '<h2 style="color: black;"> Mapa de condados en Estados Unidos </h2>'
st.markdown(heading_text2, unsafe_allow_html=True)
folium_static(m)
st.markdown('<p style="font-family: \'Times New Roman\'; font-size: 18px; color: black;"> Análisis de los condados de Estados Unidos .</p>', unsafe_allow_html=True)
# Sunburst chart centered on the webpage
if selected2 == "KPIs":
opcion = st.sidebar.selectbox('Escoge la sección', ['¿La fuente es sustentable?', 'Distribucion de marcas y modelos', 'Distribución de condados'])
if opcion == "¿La fuente es sustentable?":
def compute_sunburst():
fig = px.sunburst(df3, path=['elec', 'clean', 'Make'], color='clean', color_discrete_sequence=px.colors.qualitative.Set1)
fig.update_layout({
'plot_bgcolor': 'rgba(0, 0, 0, 0)',
'paper_bgcolor': 'rgba(0, 0, 0, 0)',
'margin': dict(l=0, r=0, b=0, t=0, pad=0),
'height': 500, # Adjust the height as needed
'width': 800, # Adjust the width as needed
})
return fig
def compute_p2():
fig = px.pie(tf, values='count', names='name', color_discrete_sequence=px.colors.sequential.Blugrn)
fig.update_layout({
'plot_bgcolor': 'rgba(0, 0, 0, 0)',
'paper_bgcolor': 'rgba(0, 0, 0, 0)',
'margin': dict(l=0, r=0, b=0, t=0, pad=0),
'height': 500, # Adjust the height as needed
'width': 800, # Adjust the width as needed
})
return fig
figv = compute_p2()
fig3 = compute_sunburst()
heading_text20 = '<h2 style="color: black;"> ¿La fuente es sustentable? </h2>'
st.markdown(heading_text20, unsafe_allow_html=True)
heading_text3 = '<h3 style="color: black;"> Sunburst</h3>'
st.markdown(heading_text3, unsafe_allow_html=True)
st.markdown('<p style="font-family: \'Times New Roman\'; font-size: 18px; color: black;"> En las siguientes gráficas se puede observar la proporción de vehículos con energía limpia .</p>', unsafe_allow_html=True)
heading_text4 = '<h3 style="color: black;"> Proporcion de carros sustentables</h3>'
st.markdown(heading_text4, unsafe_allow_html=True)
st.plotly_chart(figv, use_container_width=True)
if opcion == "Distribucion de marcas y modelos":
def compute_Treemap():
fig = px.treemap(df, path=[px.Constant("Car model proportions"), 'Model Year', 'Make', 'Model'], color='Model', color_discrete_sequence=px.colors.diverging.Portland)
fig.update_layout({
'plot_bgcolor': 'rgba(0, 0, 0, 0)',
'paper_bgcolor': 'rgba(0, 0, 0, 0)',
'margin': dict(l=0, r=0, b=0, t=0, pad=0),
'height': 500, # Adjust the height as needed
'width': 800, # Adjust the width as needed
})
return fig
def compute_p1():
fig = px.pie(top_5_values, values='Make', names='name', color_discrete_sequence=px.colors.sequential.RdBu)
fig.update_layout({
'plot_bgcolor': 'rgba(0, 0, 0, 0)',
'paper_bgcolor': 'rgba(0, 0, 0, 0)',
'margin': dict(l=0, r=0, b=0, t=0, pad=0),
'height': 500, # Adjust the height as needed
'width': 800, # Adjust the width as needed
})
return fig
figr = compute_p1()
heading_text21 = '<h2 style="color: black;"> Distribucion de marcas y modelos </h2>'
st.markdown(heading_text21, unsafe_allow_html=True)
heading_text5 = '<h3 style="color: black;"> Treemap</h3>'
st.markdown(heading_text5, unsafe_allow_html=True)
heading_text6 = '<h3 style="color: black;"> Marcas </h3>'
st.markdown(heading_text6, unsafe_allow_html=True)
st.plotly_chart(figr, use_container_width=True)
if opcion == "Distribución de condados":
def compute_p3():
fig = px.pie(top_5, values='County', names='name', color_discrete_sequence=px.colors.sequential.Burgyl)
fig.update_layout({
'plot_bgcolor': 'rgba(0, 0, 0, 0)',
'paper_bgcolor': 'rgba(0, 0, 0, 0)',
'margin': dict(l=0, r=0, b=0, t=0, pad=0),
'height': 500, # Adjust the height as needed
'width': 800, # Adjust the width as needed
})
return fig
figk = compute_p3()
heading_text22 = '<h2 style="color: black;"> Distribución de condados </h2>'
st.markdown(heading_text22, unsafe_allow_html=True)
heading_text15 = '<h3 style="color: black;"> Condados </h3>'
st.markdown(heading_text15, unsafe_allow_html=True)
st.plotly_chart(figk, use_container_width=True)