Py.Cafe

PyBluePanda/

figure-friday-air-pollution

Figure Friday - Visualizing Global Air Pollution Trends

DocsPricing
  • air-pollution.csv
  • air-pollution2.csv
  • app.py
  • requirements.txt
app.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import dash
from dash import dcc, html, Input, Output
import dash_bootstrap_components as dbc
import pandas as pd
import numpy as np
import plotly.figure_factory as ff
import plotly.colors as pc
import plotly.graph_objects as go


# def genSankey(df, cat_cols=[], value_cols='', c_scale='', region ='', year='', shipmode=''):
#     labelList = []
#     colorNumList = []
#     for catCol in cat_cols:
#         labelListTemp = list(set(df[catCol].values))
#         colorNumList.append(len(labelListTemp))
#         labelList = labelList + labelListTemp

#     # Remove duplicates from labelList
#     labelList = list(dict.fromkeys(labelList))

#     # Define base colors using Viridis color scale
#     num_labels = len(labelList)
#     color_scale = getattr(pc.sequential, c_scale)
#     base_colors = pc.sample_colorscale(color_scale, [i / (num_labels - 1) for i in range(num_labels)])
    
#     # Assign colors directly
#     color_dict = {label: color for label, color in zip(labelList, base_colors)}

#     # Transform df into a source-target pair
#     for i in range(len(cat_cols) - 1):
#         if i == 0:
#             sourceTargetDf = df[[cat_cols[i], cat_cols[i + 1], value_cols]]
#             sourceTargetDf.columns = ['source', 'target', 'count']
#         else:
#             tempDf = df[[cat_cols[i], cat_cols[i + 1], value_cols]]
#             tempDf.columns = ['source', 'target', 'count']
#             sourceTargetDf = pd.concat([sourceTargetDf, tempDf])
        
#         sourceTargetDf = sourceTargetDf.groupby(['source', 'target']).agg({'count': lambda x: round(x.sum(), 2)}).reset_index()

#     # Add index for source-target pair
#     sourceTargetDf['sourceID'] = sourceTargetDf['source'].apply(lambda x: labelList.index(x))
#     sourceTargetDf['targetID'] = sourceTargetDf['target'].apply(lambda x: labelList.index(x))

#     # Create a list of colors for the links with alpha 0.5
#     link_colors = [color_dict[src].replace('rgb(', 'rgba(').replace(')', ', 0.5)') for src in sourceTargetDf['source']]

#     # Creating the sankey diagram
#     data = dict(
#         type='sankey',
#         node=dict(
#             pad=15,
#             thickness=20,
#             line=dict(
#                 color="black",
#                 width=0.5
#             ),
#             label=labelList,
#             color=[color_dict[label] for label in labelList]
#         ),
#         link=dict(
#             source=sourceTargetDf['sourceID'],
#             target=sourceTargetDf['targetID'],
#             value=sourceTargetDf['count'].apply(lambda x: float("{:.2f}".format(x))),
#             color=link_colors
#         )
#     )

#     layout = dict(template='seaborn',
#                   title=dict(
#                   text=f'Sankey of {"all years" if year == "All" else year} sales data, '
#                         f'for {"all regions" if region == "All" else "the " + region.lower() + " region"}, '
#                         f'and {"all shipping modes" if shipmode == "All" else shipmode.lower() + " shipping mode"}',
#                     ),
#                     width=900,
#                     height=500,
#                     font=dict(color='#FFFFFF'),
#                     margin=dict(b=35, t=35, l=0, r=0),
#                     paper_bgcolor='rgba(0,0,0,0)',
#                     plot_bgcolor='rgba(0,0,0,0)'
                  
#     )

#     fig = dict(data=[data], layout=layout)

#     return fig


# def sales_map(df,selected_region, selected_year, selected_shipmode):
#     fig = ff.create_hexbin_mapbox(
#         data_frame=df, lat="Lat", lon="Long",
#         nx_hexagon=20,
#         opacity=0.6,
#         color="Sales", 
#         agg_func=np.sum,
#         labels={"color": "Sales"}, 
#         mapbox_style='open-street-map', 
#         min_count=1, 
#         color_continuous_scale='plasma',
#     )

#     fig.update_layout(title=dict(
#                     text=f'Map of {"all years" if selected_year == "All" else selected_year} sales data, '
#                         f'for {"all regions" if selected_region == "All" else "the " + selected_region.lower() + " region"}, '
#                         f'and {"all shipping modes" if selected_shipmode == "All" else selected_shipmode.lower() + " shipping mode"}',
#                     ),
#                     width=900,
#                     height=500,
#                     font=dict(color='#FFFFFF'),
#                     margin=dict(b=35, t=35, l=0, r=0),
#                     paper_bgcolor='rgba(0,0,0,0)',
#                     plot_bgcolor='rgba(0,0,0,0)'
#                     )

#     return fig


# def sankey_figure(df, selected_region, selected_year, selected_shipmode):
#     grouped_df = df.groupby(['Segment','State/Province','Category']).agg({
#         'Sales': 'sum',
#     #     'Profit': 'sum'
#     }).reset_index()

#     cat_columns = grouped_df.columns.tolist()
#     x = len(cat_columns)-1
#     cat_columns.pop(x)
#     cat_columns

#     fig = genSankey(df, cat_cols=cat_columns,value_cols='Sales',c_scale='Darkmint', region=selected_region, year=selected_year, shipmode=selected_shipmode)  

#     return fig

def air_map(df):
    years = list(range(2011, 2022))  # Generate years from 1850 to 2021
    fig = go.Figure()

    # Loop over each year to create a scatter (bubble) map trace
    for year in years:
        fig.add_trace(go.Scattermapbox(
            lat=df['lat'],
            lon=df['long'],
            mode='markers',
            marker=go.scattermapbox.Marker(
                size=df[str(year)],  # Size of bubbles based on the year's data
                color=df[str(year)],  # Color based on the year's data
                colorscale='plasma',
                showscale=True,
                sizemode='area',
                sizeref=2.*max(df[str(year)])/100,  # Adjust this for scaling
                sizemin=3
            ),
            # Tooltip text with city, country, and the year's data
            text=df['city'] + ', ' + df['county'] + '<br>' + 'Value: ' + df[str(year)].astype(str),
            hoverinfo="text",
            name=str(year),
            visible=False  # Set all traces to not visible initially
        ))

    # Add a slider to control visibility of different year traces
    steps = []
    for i, year in enumerate(years):
        step = dict(
            method='restyle',
            args=['visible', [False] * len(fig.data)],  # Set all to not visible
        )
        step['args'][1][i] = True  # Make the ith trace visible
        steps.append(step)

    sliders = [dict(
        active=0,
        currentvalue={"prefix": "Year: "},
        pad={"t": 50},
        steps=steps
    )]

    # Update layout for the map and sliders
    fig.update_layout(
        sliders=sliders,
        title=dict(
            text='Bubble Map from 2011 to 2021',
        ),
        width=900,
        height=500,
        font=dict(color='#FFFFFF'),
        margin=dict(b=35, t=35, l=0, r=0),
        paper_bgcolor='rgba(0,0,0,0)',
        plot_bgcolor='rgba(0,0,0,0)',
        mapbox=dict(
            style="open-street-map",
            zoom=1,
            center=dict(lat=20, lon=0)  # Adjust the center for your data
        )
    )

    # Set the first trace (1850) to be visible
    fig.data[0].visible = True

    return fig

# Load data
data = pd.read_csv('air-pollution2.csv')
amapg1 = air_map(data)
# data_df['Order Date'] = pd.to_datetime(data_df['Order Date'], dayfirst=True)
# data_df['order_year'] = data_df['Order Date'].dt.year

# # Extract unique values for filters
# regions = sorted(data_df['Region'].unique())
# regions.insert(0, 'All')
# orderyears = sorted(data_df['order_year'].unique(), reverse=True)
# orderyear_options = [{'label': str(orderyear), 'value': orderyear} for orderyear in orderyears]
# orderyear_options.insert(0, {'label': 'All', 'value': 'All'})

# shipmodes = sorted(data_df['Ship Mode'].unique())
# shipmodes.insert(0, 'All')

# Initialize the Dash app
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])

# Define custom theme
custom_theme = {
    "primary": "#75C9BE",
    "secondary": "#d6e8e7",
    "info": "#3B9ECB",
    "gray": "#adb5bd",
    "success": "#8BE3AA",
    "warning": "#F9F871",
    "danger": "#c0003e",
    "body_bg": "#1F5869",
    "content_bg": "#153F4C",
    "text_color": "#CBE2E2"
}

# Define the layout
app.layout = dbc.Container(
    [
        dbc.Row(
            dbc.Col(
                html.H1(
                    'Figure Friday - Week 36 - Air Pollution Data',
                    style={'textAlign': 'center', 'color': custom_theme["text_color"]}
                ),
                width=12
            ),
            justify='center'
        ),
        dbc.Row(
            [
                dbc.Col(
                    [
                        # html.Label('Select a Region', style={'color': custom_theme["text_color"]}),
                        # dcc.Dropdown(id='region-filter', options=[{'label': region, 'value': region} for region in regions], value='All')
                    ],
                    width=4,
                    className="my-4",
                    style={'padding': '0 5px'}
                ),
                dbc.Col(
                    [
                        # html.Label('Select an Order Year', style={'color': custom_theme["text_color"]}),
                        # dcc.Dropdown(id='sales_year-filter', options=orderyear_options, value='All')
                    ],
                    width=4,
                    className="my-4",
                    style={'padding': '0 5px'}
                ),
                dbc.Col(
                    [
                        # html.Label('Select a Shipping Mode', style={'color': custom_theme["text_color"]}),
                        # dcc.Dropdown(id='ship_mode-filter', options=[{'label': shipmode, 'value': shipmode} for shipmode in shipmodes], value='All')
                    ],
                    width=4,
                    className="my-4",
                    style={'padding': '0 5px'}
                ),
            ],
            justify='center',
            style={'margin-bottom': '20px'}
        ),
        dbc.Row(
            [
                dbc.Col(
                    dcc.Graph(figure=amapg1),#id='airmap_graph1'),
                    width=12, #6
                    className="my-4",
                    style={'padding': '0 5px'}
                ),
                # dbc.Col(
                #     dcc.Graph(id='sankey'),
                #     width=6,
                #     className="my-4",
                #     style={'padding': '0 5px'}
                # ),
            ]
        )
    ],
    fluid=True,
    style={'backgroundColor': custom_theme["body_bg"]}
)



# Define callbacks to update figures based on selected filters
# @app.callback(
#     [Output('sales_graph1', 'figure'),
#      Output('sankey', 'figure')],
#     [Input('region-filter', 'value'),
#      Input('sales_year-filter', 'value'),
#      Input('ship_mode-filter', 'value')]
# )
# def update_figures(selected_region, selected_year, selected_shipmode):
#     filtered_df = data_df
    
#     if selected_region != 'All':
#         filtered_df = filtered_df[filtered_df['Region'] == selected_region]
    
#     if selected_year != 'All':
#         filtered_df = filtered_df[filtered_df['order_year'] == selected_year]
    
#     if selected_shipmode != 'All':
#         filtered_df = filtered_df[filtered_df['Ship Mode'] == selected_shipmode]
    
#     sales_map_fig = sales_map(filtered_df, selected_region, selected_year, selected_shipmode)
#     sankey_fig = sankey_figure(filtered_df, selected_region, selected_year, selected_shipmode)
    
#     return sales_map_fig, sankey_fig


if __name__ == '__main__':
    app.run_server(debug=True)