-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvisualization.py
254 lines (229 loc) · 7 KB
/
visualization.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
import plotly.express as px
import plotly.graph_objects as go
# from policyengine_core.charts import format_fig
from constants import TEAL_ACCENT
def create_tax_plot(
df,
income,
donation_amount,
tax_at_donation,
donation_column="charitable_cash_donations",
):
"""Creates a plot showing taxes vs donation amount."""
y_col = "income_tax_after_donations"
fig = px.line(
df,
x=donation_column,
y=y_col,
labels={
donation_column: "Donations",
y_col: "Net taxes (taxes minus benefits)",
},
)
# Update line style
fig.update_traces(
line_color="rgb(180, 180, 180)", # Light gray line
showlegend=False,
hovertemplate="Donations=$%{x:,.0f}<br>Net taxes=$%{y:,.0f}<br><extra></extra>",
)
# Add semi-transparent marker for current donation
fig.add_trace(
go.Scatter(
x=[donation_amount],
y=[tax_at_donation],
mode="markers",
marker=dict(color=TEAL_ACCENT, size=8, opacity=0.7, symbol="circle"),
showlegend=False,
hovertemplate="Your donation: $%{x:,.0f}<br>Net taxes: $%{y:,.0f}<br><extra></extra>",
)
)
fig.update_layout(
xaxis_tickformat="$,",
yaxis_tickformat="$,",
xaxis_range=[0, income],
yaxis_range=[
min(min(df[y_col]) * 1.05, 0),
max(max(df[y_col]) * 1.05, 0),
],
xaxis=dict(zeroline=True, zerolinewidth=1, zerolinecolor="gray"),
yaxis=dict(zeroline=True, zerolinewidth=1, zerolinecolor="gray"),
showlegend=False,
plot_bgcolor="white",
margin=dict(t=10), # Reduce top margin since title is in markdown
)
return format_fig(fig)
def create_marginal_savings_plot(
df,
donation_amount,
marginal_savings,
donation_column="charitable_cash_donations",
):
"""Creates a plot showing the marginal giving discount."""
fig = px.line(
df,
x=donation_column,
y="marginal_savings",
labels={
donation_column: "Donations",
"marginal_savings": "Marginal giving discount",
},
)
# Update line style
fig.update_traces(
line_color="rgb(180, 180, 180)", # Light gray line
showlegend=False,
hovertemplate=(
"Donations=$%{x:,.0f}<br>"
"Marginal giving discount: $%{y:.2f}<br>"
"<extra></extra>"
),
)
# Add semi-transparent marker for current donation
fig.add_trace(
go.Scatter(
x=[donation_amount],
y=[marginal_savings],
mode="markers",
marker=dict(color=TEAL_ACCENT, size=8, opacity=0.7, symbol="circle"),
showlegend=False,
hovertemplate=(
"Your donation: $%{x:,.0f}<br>"
"Marginal giving discount: $%{y:.2f}<br>"
"<extra></extra>"
),
)
)
fig.update_layout(
xaxis_tickformat="$,",
yaxis_tickformat=".0%",
xaxis_range=[0, max(df[donation_column])],
yaxis_range=[0, 1],
xaxis=dict(zeroline=True, zerolinewidth=1, zerolinecolor="gray"),
yaxis=dict(zeroline=True, zerolinewidth=1, zerolinecolor="gray"),
showlegend=False,
plot_bgcolor="white",
margin=dict(t=10), # Reduce top margin since title is in markdown
)
return format_fig(fig)
def create_net_income_plot(
df,
initial_donation,
initial_net_income,
required_donation,
required_donation_net_income,
donation_column="charitable_cash_donations",
):
"""Creates a plot showing net income vs donation amount."""
fig = px.line(
df,
x=donation_column,
y="net_income",
labels={
donation_column: "Donations",
"net_income": "Net income after taxes, transfers, and donations",
},
)
# Update line style
fig.update_traces(
line_color="rgb(180, 180, 180)", # Light gray line
showlegend=False,
hovertemplate=(
"Donations=$%{x:,.0f}<br>" "Net income=$%{y:,.0f}<br>" "<extra></extra>"
),
)
# Add markers for initial and required donations
points = [
(
initial_donation,
initial_net_income,
"rgb(120, 120, 120)",
"Initial donation",
), # Gray
(
required_donation,
required_donation_net_income,
TEAL_ACCENT,
"Required donation",
), # Teal
]
# Calculate the donation that is closest to the required net income change.
for donation, net_income, color, name in points:
fig.add_trace(
go.Scatter(
x=[donation],
y=[net_income],
mode="markers",
name=name,
marker=dict(color=color, size=8, opacity=0.7, symbol="circle"),
hovertemplate=(
f"{name}:<br>"
"Donation amount ($)=$%{x:,.0f}<br>"
"Net income ($)=$%{y:,.0f}<br>"
"<extra></extra>"
),
)
)
fig.update_layout(
xaxis_tickformat="$,",
yaxis_tickformat="$,",
xaxis_range=[0, max(df[donation_column])],
yaxis_range=[
min(df["net_income"]) * 0.95,
max(df["net_income"]) * 1.05,
],
xaxis=dict(zeroline=True, zerolinewidth=1, zerolinecolor="gray"),
yaxis=dict(zeroline=True, zerolinewidth=1, zerolinecolor="gray"),
showlegend=True,
legend=dict(
yanchor="top",
y=0.99,
xanchor="right",
x=0.99,
bgcolor="rgba(255, 255, 255, 0.8)",
itemsizing="constant", # Added to ensure consistent marker size in legend
),
plot_bgcolor="white",
margin=dict(t=10),
)
return format_fig(fig)
def format_fig(fig: go.Figure) -> go.Figure:
"""Format a plotly figure to match the PolicyEngine style guide."""
fig.update_layout(
font=dict(
family="Roboto Serif",
color="black",
),
margin=dict(
l=50, # Reduced left margin since we don't need space for left logo
r=100,
t=50,
b=120,
pad=4,
),
)
fig.add_layout_image(
dict(
source="https://raw.githubusercontent.com/PolicyEngine/policyengine-app/master/src/images/logos/policyengine/blue.png",
xref="paper",
yref="paper",
x=1.0,
y=-0.10,
sizex=0.10,
sizey=0.10,
xanchor="right",
yanchor="bottom",
)
)
# Rest of the layout settings remain the same
fig.update_layout(
template="plotly_white",
height=600,
width=800,
)
fig.update_layout(
modebar=dict(
bgcolor="rgba(0,0,0,0)",
color="rgba(0,0,0,0)",
)
)
return fig