-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualizations.py
More file actions
142 lines (122 loc) · 4.16 KB
/
Copy pathvisualizations.py
File metadata and controls
142 lines (122 loc) · 4.16 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
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.express as px
import plotly.graph_objects as go
import pandas as pd
import numpy as np
from typing import Dict, Any
def create_time_series_plot(df: pd.DataFrame, date_column: str, value_column: str) -> go.Figure:
"""Create an interactive time series plot"""
fig = px.line(df, x=date_column, y=value_column,
title=f'{value_column} Over Time')
fig.update_layout(
xaxis_title=date_column,
yaxis_title=value_column,
template='plotly_white'
)
return fig
def create_correlation_heatmap(df: pd.DataFrame) -> go.Figure:
"""Create an interactive correlation heatmap"""
numeric_df = df.select_dtypes(include=[np.number])
correlation_matrix = numeric_df.corr()
fig = go.Figure(data=go.Heatmap(
z=correlation_matrix,
x=correlation_matrix.columns,
y=correlation_matrix.columns,
colorscale='RdBu',
zmin=-1,
zmax=1
))
fig.update_layout(
title='Correlation Heatmap',
template='plotly_white'
)
return fig
def create_distribution_plots(df: pd.DataFrame, column: str) -> go.Figure:
"""Create distribution plots for numerical columns"""
fig = go.Figure()
fig.add_trace(go.Histogram(x=df[column], name='Histogram'))
fig.add_trace(go.Box(x=df[column], name='Box Plot'))
fig.update_layout(
title=f'Distribution Analysis: {column}',
template='plotly_white'
)
return fig
def create_summary_dashboard(kpis: Dict[str, float]) -> go.Figure:
"""Create a dashboard of KPI metrics"""
# Filter out non-numeric KPIs
numeric_kpis = {k: v for k, v in kpis.items() if isinstance(v, (int, float))}
# Create a subplot for each KPI
fig = go.Figure()
# Calculate layout dimensions
n_kpis = len(numeric_kpis)
n_rows = (n_kpis + 1) // 2 # 2 columns layout
for idx, (metric, value) in enumerate(numeric_kpis.items()):
row = idx // 2 + 1
col = idx % 2 + 1
fig.add_trace(go.Indicator(
mode="number+gauge+delta",
value=value,
title={'text': metric},
domain={'row': row, 'column': col},
gauge={
'axis': {'range': [0, max(100, value * 1.2)]},
'bar': {'color': "royalblue"},
'steps': [
{'range': [0, value], 'color': 'lightgray'}
]
}
))
fig.update_layout(
grid={'rows': n_rows, 'columns': 2, 'pattern': "independent"},
height=200 * n_rows,
title='Financial KPIs Dashboard',
template='plotly_white'
)
return fig
def create_trend_analysis(df: pd.DataFrame, date_column: str, metrics: list) -> go.Figure:
"""Create trend analysis for multiple metrics"""
fig = go.Figure()
for metric in metrics:
fig.add_trace(go.Scatter(
x=df[date_column],
y=df[metric],
name=metric,
mode='lines+markers'
))
fig.update_layout(
title='Trend Analysis',
xaxis_title=date_column,
yaxis_title='Value',
template='plotly_white',
showlegend=True
)
return fig
def create_categorical_analysis(df: pd.DataFrame, cat_column: str, value_column: str) -> go.Figure:
"""Create analysis for categorical variables"""
grouped_data = df.groupby(cat_column)[value_column].agg(['mean', 'sum', 'count']).reset_index()
fig = go.Figure()
# Bar chart for total values
fig.add_trace(go.Bar(
x=grouped_data[cat_column],
y=grouped_data['sum'],
name='Total',
marker_color='royalblue'
))
# Line chart for average values
fig.add_trace(go.Scatter(
x=grouped_data[cat_column],
y=grouped_data['mean'],
name='Average',
mode='lines+markers',
line=dict(color='red')
))
fig.update_layout(
title=f'{value_column} by {cat_column}',
xaxis_title=cat_column,
yaxis_title=value_column,
template='plotly_white',
showlegend=True,
barmode='group'
)
return fig