Skip to content

Commit 48c6c20

Browse files
author
santi-rios
committed
Refactor contribution choropleth plot: adjust layout, increase height, and reposition buttons for improved usability and aesthetics.
1 parent 4c3f68b commit 48c6c20

File tree

1 file changed

+89
-83
lines changed

1 file changed

+89
-83
lines changed

utils/functions.py

Lines changed: 89 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,12 +1303,10 @@ def create_contribution_choropleth(data: pd.DataFrame):
13031303
z=avg_data['total_percentage'],
13041304
locationmode='ISO-3',
13051305
colorscale='Viridis',
1306-
# colorscale=[[0, 'rgb(10, 49, 97)'], [1, 'rgb(238, 28, 37)']],
13071306
reversescale=False,
13081307
zmid=None, # Let plotly handle the midpoint
13091308
zmin=color_range[0],
13101309
zmax=color_range[1],
1311-
# legendwidth = 20,
13121310
hovertemplate=(
13131311
"<b>%{customdata[0]}</b> (%{location})<br>" +
13141312
"Avg Contribution: %{z:.2f}%<br>" +
@@ -1322,14 +1320,9 @@ def create_contribution_choropleth(data: pd.DataFrame):
13221320
y=0.02,
13231321
xanchor="left",
13241322
x=0.1,
1325-
# tickangle =0,
1326-
# tickformat = ".2f",
13271323
ticklabelstep = 4,
13281324
ticksuffix = "%",
1329-
# titleside="right",
13301325
tickmode="linear",
1331-
# xanchor ='right',
1332-
# yanchor ='bottom',
13331326
tick0=color_range[0],
13341327
dtick=max(0.5, (color_range[1] - color_range[0]) / 10)
13351328
),
@@ -1347,88 +1340,101 @@ def create_contribution_choropleth(data: pd.DataFrame):
13471340
lakecolor='rgba(127,205,255,0.1)'
13481341
),
13491342
template='plotly_white',
1350-
height=500,
1351-
margin=dict(l=20, r=20, t=60, b=20),
1343+
height=550, # Increased height to accommodate buttons
1344+
margin=dict(l=20, r=20, t=80, b=20), # Increased top margin for buttons
13521345
modebar_remove=['zoom', 'pan', 'lasso', 'select']
13531346
)
13541347

1355-
# Add dropdowns
1356-
button_layer_1_height = 1.02
1348+
# Position buttons in top corners to avoid overlap
13571349
fig.update_layout(
1358-
updatemenus=[
1359-
dict(
1360-
buttons=list([
1361-
dict(
1362-
args=["colorscale", "Viridis"],
1363-
label="Viridis",
1364-
method="restyle"
1365-
),
1366-
dict(
1367-
args=["colorscale", "Cividis"],
1368-
label="Cividis",
1369-
method="restyle"
1370-
),
1371-
dict(
1372-
args=["colorscale", "Reds"],
1373-
label="Reds",
1374-
method="restyle"
1375-
),
1376-
dict(
1377-
args=["colorscale", "Blues"],
1378-
label="Blues",
1379-
method="restyle"
1380-
),
1381-
dict(
1382-
args=["colorscale", "Greens"],
1383-
label="Greens",
1384-
method="restyle"
1385-
),
1386-
dict(
1387-
args=["colorscale", "Jet"],
1388-
label="Rainbow",
1389-
method="restyle"
1390-
),
1391-
]),
1392-
direction="down",
1393-
pad={"r": 10, "t": 10},
1394-
showactive=True,
1395-
x=0.1,
1396-
xanchor="left",
1397-
y=button_layer_1_height,
1398-
yanchor="top"
1399-
),
1400-
dict(
1401-
buttons=list([
1402-
dict(
1403-
args=["reversescale", False],
1404-
label="False",
1405-
method="restyle"
1406-
),
1407-
dict(
1408-
args=["reversescale", True],
1409-
label="True",
1410-
method="restyle"
1411-
)
1412-
]),
1413-
direction="down",
1414-
pad={"r": 10, "t": 10},
1415-
showactive=True,
1416-
x=0.37,
1417-
xanchor="left",
1418-
y=button_layer_1_height,
1419-
yanchor="top"
1420-
),
1421-
]
1350+
updatemenus=[
1351+
dict(
1352+
buttons=list([
1353+
dict(
1354+
args=["colorscale", "Viridis"],
1355+
label="Viridis",
1356+
method="restyle"
1357+
),
1358+
dict(
1359+
args=["colorscale", "Cividis"],
1360+
label="Cividis",
1361+
method="restyle"
1362+
),
1363+
dict(
1364+
args=["colorscale", "Reds"],
1365+
label="Reds",
1366+
method="restyle"
1367+
),
1368+
dict(
1369+
args=["colorscale", "Blues"],
1370+
label="Blues",
1371+
method="restyle"
1372+
),
1373+
dict(
1374+
args=["colorscale", "Greens"],
1375+
label="Greens",
1376+
method="restyle"
1377+
),
1378+
dict(
1379+
args=["colorscale", "Jet"],
1380+
label="Rainbow",
1381+
method="restyle"
1382+
),
1383+
]),
1384+
direction="down",
1385+
pad={"r": 10, "t": 10},
1386+
showactive=True,
1387+
x=0.02, # Left side
1388+
xanchor="left",
1389+
y=0.98, # Top
1390+
yanchor="top"
1391+
),
1392+
dict(
1393+
buttons=list([
1394+
dict(
1395+
args=["reversescale", False],
1396+
label="Normal",
1397+
method="restyle"
1398+
),
1399+
dict(
1400+
args=["reversescale", True],
1401+
label="Reversed",
1402+
method="restyle"
1403+
)
1404+
]),
1405+
direction="down",
1406+
pad={"r": 10, "t": 10},
1407+
showactive=True,
1408+
x=0.98, # Right side
1409+
xanchor="right",
1410+
y=0.98, # Top
1411+
yanchor="top"
1412+
),
1413+
],
1414+
annotations=[
1415+
dict(
1416+
text="Colorscale",
1417+
x=0.02,
1418+
xref="paper",
1419+
y=1.02,
1420+
yref="paper",
1421+
align="left",
1422+
showarrow=False,
1423+
font=dict(size=12)
1424+
),
1425+
dict(
1426+
text="Reverse",
1427+
x=0.98,
1428+
xref="paper",
1429+
y=1.02,
1430+
yref="paper",
1431+
align="right",
1432+
showarrow=False,
1433+
font=dict(size=12)
1434+
)
1435+
]
14221436
)
14231437

1424-
fig.update_layout(
1425-
annotations=[
1426-
dict(text="Colorscale", x=0, xref="paper", y=1.04, yref="paper",
1427-
align="left", showarrow=False),
1428-
dict(text="Reverse Colorscale", x=0.25, xref="paper", y=1.04,
1429-
yref="paper", showarrow=False)
1430-
])
1431-
14321438
return fig
14331439

14341440
def create_summary_dataframe(data: pd.DataFrame, mode: str) -> pd.DataFrame:

0 commit comments

Comments
 (0)