Update correlation_explorer_app.py
Browse files- correlation_explorer_app.py +64 -65
correlation_explorer_app.py
CHANGED
|
@@ -4,6 +4,8 @@ import plotly.graph_objects as go
|
|
| 4 |
from dash import Dash, dcc, html, Input, Output, State, ctx
|
| 5 |
import dash_daq as daq
|
| 6 |
import numpy as np
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# 1. Data Loading & Preprocessing
|
| 9 |
|
|
@@ -51,89 +53,86 @@ def get_variables(scope):
|
|
| 51 |
}
|
| 52 |
|
| 53 |
# 3. App Layout
|
| 54 |
-
app
|
| 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 |
-
html.Div([
|
| 83 |
-
html.Div([
|
| 84 |
html.Label("X-axis Variable"),
|
| 85 |
-
dcc.Dropdown(id='x-axis-dropdown')
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
html.Label("Year Range"),
|
| 91 |
dcc.RangeSlider(
|
| 92 |
id='year-slider',
|
| 93 |
min=df['year'].min(), max=df['year'].max(),
|
| 94 |
value=[df['year'].min(), df['year'].max()],
|
| 95 |
marks={str(year): str(year) for year in range(df['year'].min(), df['year'].max()+1, 5)},
|
| 96 |
-
step=1
|
| 97 |
),
|
| 98 |
html.Label("View Mode", style={'marginTop': '10px'}),
|
| 99 |
dcc.RadioItems(
|
| 100 |
id='view-mode',
|
| 101 |
-
options=[
|
|
|
|
|
|
|
|
|
|
| 102 |
value="Monthly",
|
| 103 |
labelStyle={"display": "inline-block", "margin-right": "10px"}
|
| 104 |
)
|
| 105 |
-
],
|
| 106 |
-
],
|
| 107 |
-
'width': '100%',
|
| 108 |
-
'maxWidth': '1200px',
|
| 109 |
-
'margin': '0 auto',
|
| 110 |
-
'fontSize': '16px'
|
| 111 |
-
}),
|
| 112 |
-
|
| 113 |
-
html.Div([
|
| 114 |
-
html.H4("What You're Seeing", style={"marginTop": "20px"}),
|
| 115 |
-
html.P("This interactvie tool allows you to explore the statistical relationships between 🌱CO₂ emissions, 🌊sea level rise, and temperature anomalies."),
|
| 116 |
-
html.P("You can start by switching in between Global, Northern, or Southern Hemisphere views and use the dropdowns and sliders to select a timeframe and two indicators to compare."),
|
| 117 |
-
html.P("The scatter plot shows how the selected variables move together over time. A regression line is added to show the trend, and the R² value indicates how well one variable explains the other."),
|
| 118 |
-
html.P("Pearson's r (shown in the heatmap and above the scatter plot) ranges from -1 to +1. A value close to +1 means a strong positive correlation; close to -1 indicates a strong negative one; and close to 0 implies little to no correlation."),
|
| 119 |
-
html.P("The heatmap provides an overview of how all selected variables relate to one another within the chosen scope and time range. It uses Pearson’s correlation coefficients (r) to reveal linear relationships.")
|
| 120 |
-
], style={
|
| 121 |
-
|
| 122 |
-
"backgroundColor": "#f5f5f5",
|
| 123 |
-
"padding": "15px",
|
| 124 |
-
"border": "1px solid #ccc",
|
| 125 |
-
"borderRadius": "6px",
|
| 126 |
-
"marginTop": "20px",
|
| 127 |
-
"maxWidth": "900px",
|
| 128 |
-
"fontSize": "16px"
|
| 129 |
-
}),
|
| 130 |
|
| 131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
|
|
|
|
| 133 |
dcc.Graph(id='scatter-plot'),
|
| 134 |
-
html.H4("Correlation Matrix (Pearson)",
|
| 135 |
dcc.Graph(id='correlation-heatmap')
|
| 136 |
-
])
|
|
|
|
| 137 |
# 4. Callbacks
|
| 138 |
@app.callback(
|
| 139 |
Output('x-axis-dropdown', 'options'),
|
|
|
|
| 4 |
from dash import Dash, dcc, html, Input, Output, State, ctx
|
| 5 |
import dash_daq as daq
|
| 6 |
import numpy as np
|
| 7 |
+
import dash_bootstrap_components as db
|
| 8 |
+
|
| 9 |
|
| 10 |
# 1. Data Loading & Preprocessing
|
| 11 |
|
|
|
|
| 53 |
}
|
| 54 |
|
| 55 |
# 3. App Layout
|
| 56 |
+
app = Dash(__name__, external_stylesheets=[dbc.themes.FLATLY])
|
| 57 |
+
app.title = "Correlation & Insight Explorer"
|
| 58 |
+
|
| 59 |
+
app.layout = dbc.Container([
|
| 60 |
+
dbc.Row([
|
| 61 |
+
dbc.Col(html.H2("Correlation & Insight Explorer", className="text-center"), width=12)
|
| 62 |
+
], className="my-3"),
|
| 63 |
+
|
| 64 |
+
dbc.Row([
|
| 65 |
+
dbc.Col([
|
| 66 |
+
html.Label("Scope"),
|
| 67 |
+
dcc.RadioItems(id="scope-selector", options=[
|
| 68 |
+
{"label": "🌍 Global", "value": "global"},
|
| 69 |
+
{"label": "🌎 Northern Hemisphere", "value": "nh"},
|
| 70 |
+
{"label": "🌏 Southern Hemisphere", "value": "sh"}
|
| 71 |
+
], value="global", labelStyle={"display": "block"})
|
| 72 |
+
], md=6),
|
| 73 |
+
|
| 74 |
+
dbc.Col([
|
| 75 |
+
html.Label("Theme"),
|
| 76 |
+
daq.ToggleSwitch(id='theme-toggle', label=['Light', 'Dark'], value=False)
|
| 77 |
+
], md=6)
|
| 78 |
+
], className="mb-3"),
|
| 79 |
+
|
| 80 |
+
dbc.Row([
|
| 81 |
+
dbc.Col([
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
html.Label("X-axis Variable"),
|
| 83 |
+
dcc.Dropdown(id='x-axis-dropdown')
|
| 84 |
+
], md=6),
|
| 85 |
+
dbc.Col([
|
| 86 |
+
html.Label("Y-axis Variable"),
|
| 87 |
+
dcc.Dropdown(id='y-axis-dropdown')
|
| 88 |
+
], md=6)
|
| 89 |
+
], className="mb-3"),
|
| 90 |
+
|
| 91 |
+
dbc.Row([
|
| 92 |
+
dbc.Col([
|
| 93 |
html.Label("Year Range"),
|
| 94 |
dcc.RangeSlider(
|
| 95 |
id='year-slider',
|
| 96 |
min=df['year'].min(), max=df['year'].max(),
|
| 97 |
value=[df['year'].min(), df['year'].max()],
|
| 98 |
marks={str(year): str(year) for year in range(df['year'].min(), df['year'].max()+1, 5)},
|
| 99 |
+
step=1
|
| 100 |
),
|
| 101 |
html.Label("View Mode", style={'marginTop': '10px'}),
|
| 102 |
dcc.RadioItems(
|
| 103 |
id='view-mode',
|
| 104 |
+
options=[
|
| 105 |
+
{"label": "Monthly", "value": "Monthly"},
|
| 106 |
+
{"label": "Seasonal", "value": "Seasonal"}
|
| 107 |
+
],
|
| 108 |
value="Monthly",
|
| 109 |
labelStyle={"display": "inline-block", "margin-right": "10px"}
|
| 110 |
)
|
| 111 |
+
], md=12)
|
| 112 |
+
], className="mb-4"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
+
dbc.Card([
|
| 115 |
+
dbc.CardHeader("🔍 What You’re Seeing – Climate Insights"),
|
| 116 |
+
dbc.CardBody([
|
| 117 |
+
html.P("This interactive tool allows you to explore the statistical relationships between climate indicators:"),
|
| 118 |
+
html.Ul([
|
| 119 |
+
html.Li("🌱 Human-induced CO₂ emissions heat the planet — mostly from fossil fuel use."),
|
| 120 |
+
html.Li("🌡️ Rising CO₂ leads to higher land and ocean temperatures."),
|
| 121 |
+
html.Li("🌊 Warmer climates cause sea level rise through ice melt and ocean expansion.")
|
| 122 |
+
]),
|
| 123 |
+
html.P("Switch views (Global, Northern, Southern Hemisphere) and select indicators and years to compare."),
|
| 124 |
+
html.P("The scatter plot shows how the selected variables change together, with a regression trendline and R² value."),
|
| 125 |
+
html.P("Pearson's r (seen on the heatmap and above the scatter) helps you evaluate correlation strength."),
|
| 126 |
+
html.P("The correlation heatmap reveals how all indicators relate within your selected range.")
|
| 127 |
+
])
|
| 128 |
+
], className="mb-4"),
|
| 129 |
|
| 130 |
+
html.Div(id='correlation-note', style={'padding': '10px', 'fontSize': '16px'}),
|
| 131 |
dcc.Graph(id='scatter-plot'),
|
| 132 |
+
html.H4("Correlation Matrix (Pearson)", className="text-center mt-4"),
|
| 133 |
dcc.Graph(id='correlation-heatmap')
|
| 134 |
+
], fluid=True)
|
| 135 |
+
|
| 136 |
# 4. Callbacks
|
| 137 |
@app.callback(
|
| 138 |
Output('x-axis-dropdown', 'options'),
|