Skip to content

Commit

Permalink
[MNT] Switch to 1 decimal place for all plots (#94)
Browse files Browse the repository at this point in the history
* set global # decimal places

* add param for # decimals in all plotting funcs

* specify global # decimals when calling plotting funcs

* update type hint
  • Loading branch information
alyssadai authored Sep 20, 2024
1 parent 4ab4413 commit 80cbefd
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 21 deletions.
5 changes: 5 additions & 0 deletions climate_emotions_map/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from .utility import ( # IMPACT_COLORMAP,; OPINION_COLORMAP,
ALL_STATES_LABEL,
DEFAULT_QUESTION,
NUM_DECIMALS,
SECTION_TITLES,
)

Expand Down Expand Up @@ -141,6 +142,7 @@ def update_sample_descriptive_plot(state):
"""Update the sample descriptive plot based on the selected state."""
return make_descriptive_plots(
state=state,
decimals=NUM_DECIMALS,
)


Expand Down Expand Up @@ -212,6 +214,7 @@ def update_map(question_value, state, impact):
impact=impact,
colormap_range_padding=MAP_LAYOUT["colormap_range_padding"],
margins=MAP_LAYOUT["margin"],
decimals=NUM_DECIMALS,
# opinion_colormap=OPINION_COLORMAP,
# impact_colormap=IMPACT_COLORMAP,
)
Expand Down Expand Up @@ -247,6 +250,7 @@ def update_selected_question_bar_plot(
state=state,
stratify=is_party_stratify_checked,
threshold=threshold,
decimals=NUM_DECIMALS,
fig_kw=SINGLE_SUBQUESTION_FIG_KW,
)
return figure
Expand Down Expand Up @@ -319,6 +323,7 @@ def update_stacked_bar_plots(
state=state,
stratify=is_party_stratify_checked,
threshold=threshold,
decimals=NUM_DECIMALS,
)
figures.append(figure)

Expand Down
5 changes: 5 additions & 0 deletions climate_emotions_map/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from .utility import ( # IMPACT_COLORMAP,; OPINION_COLORMAP,
ALL_STATES_LABEL,
DEFAULT_QUESTION,
NUM_DECIMALS,
SECTION_TITLES,
)

Expand Down Expand Up @@ -140,6 +141,7 @@ def create_sample_descriptive_plot():
id="sample-descriptive-plot",
figure=make_descriptive_plots(
state=None,
decimals=NUM_DECIMALS,
),
config=DCC_GRAPH_CONFIG,
# TODO: Revisit
Expand Down Expand Up @@ -368,6 +370,7 @@ def create_map_plot():
outcome=DEFAULT_QUESTION["outcome"],
colormap_range_padding=MAP_LAYOUT["colormap_range_padding"],
margins=MAP_LAYOUT["margin"],
decimals=NUM_DECIMALS,
# opinion_colormap=OPINION_COLORMAP,
),
# vh = % of viewport height
Expand Down Expand Up @@ -443,6 +446,7 @@ def create_bar_plots_for_question(question_id: str, subquestion_id: str):
state=None,
stratify=False,
threshold=DEFAULT_QUESTION["outcome"],
decimals=NUM_DECIMALS,
),
config=DCC_GRAPH_CONFIG,
),
Expand Down Expand Up @@ -470,6 +474,7 @@ def create_selected_question_bar_plot():
state=None,
stratify=False,
threshold=DEFAULT_QUESTION["outcome"],
decimals=NUM_DECIMALS,
fig_kw=SINGLE_SUBQUESTION_FIG_KW,
),
config=DCC_GRAPH_CONFIG,
Expand Down
36 changes: 23 additions & 13 deletions climate_emotions_map/make_descriptive_plots.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env python
from functools import partial
from textwrap import wrap
from typing import Optional

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -138,8 +137,6 @@
Q2_LABEL: (11, 1),
}

DECIMALS = 0


def get_categories_dict(df: pd.DataFrame) -> dict:
"""
Expand Down Expand Up @@ -179,8 +176,9 @@ def get_category_to_display(category: str, demographic_variable: str):
def make_descriptive_plot_traces(
df: pd.DataFrame,
demographic_variable: str,
marker_color=None,
reverse=True,
marker_color: str | None = None,
reverse: bool = True,
decimals: int = 1,
) -> go.Bar:
"""
Make a single plot for a descriptive demographic variable.
Expand All @@ -192,6 +190,8 @@ def make_descriptive_plot_traces(
and "percentage".
demographic_variable : str
Demographic variable to plot.
decimals : int, optional
Number of decimal places to display, by default 1
"""
# subset the data
df: pd.DataFrame = df.loc[
Expand Down Expand Up @@ -233,7 +233,7 @@ def make_descriptive_plot_traces(
# for n, percentage in zip(df[COL_N], df[COL_PERCENTAGE])
# ],
hovertemplate=(
f"<b>%{{customdata[1]}}</b>: %{{x:.{DECIMALS}f}}% (%{{customdata[0]}})"
f"<b>%{{customdata[1]}}</b>: %{{x:.{decimals}f}}% (%{{customdata[0]}})"
"<extra></extra>"
),
marker_color=marker_color,
Expand All @@ -259,7 +259,7 @@ def wrap_text_label(text: str, width: int) -> pd.DataFrame:


def make_impact_plot_traces(
df: pd.DataFrame, marker_color=None, text_wrap_width=10
df: pd.DataFrame, marker_color=None, text_wrap_width=10, decimals=1
):
data_impact = df.loc[
df[COL_DEMOGRAPHIC_VARIABLE].isin(IMPACT_VARIABLES)
Expand Down Expand Up @@ -290,7 +290,7 @@ def make_impact_plot_traces(
customdata=list(zip(x, data_category[COL_N])),
hovertemplate=(
"<b>%{customdata[0]}</b>"
f": %{{y:.{DECIMALS}f}}% (%{{customdata[1]}})"
f": %{{y:.{decimals}f}}% (%{{customdata[1]}})"
"<extra></extra>"
),
marker_color=marker_color,
Expand All @@ -303,9 +303,10 @@ def make_impact_plot_traces(

def make_descriptive_plots(
state: str | None = None,
margins: Optional[dict] = None,
text_wrap_width=14,
colors: list[str] = None,
margins: dict | None = None,
text_wrap_width: int = 14,
colors: list[str] | None = None,
decimals: int = 1,
) -> go.Figure:
"""Make the sample descriptive plots
Expand All @@ -319,6 +320,8 @@ def make_descriptive_plots(
Maximum width for wrapping some text labels, by default 14
colors : list[str], optional
List of colors for the bar plots, by default None (uses the turbo colorscale)
decimals : int, optional
Number of decimal places to display, by default 1
Returns
-------
Expand Down Expand Up @@ -363,11 +366,18 @@ def make_descriptive_plots(

if demographic_variable == IMPACTS_LABEL:
traces = make_impact_plot_traces(
data, text_wrap_width=text_wrap_width, marker_color=color
data,
text_wrap_width=text_wrap_width,
marker_color=color,
decimals=decimals,
)
else:
traces = make_descriptive_plot_traces(
data, demographic_variable, reverse=True, marker_color=color
data,
demographic_variable,
reverse=True,
marker_color=color,
decimals=decimals,
)

for trace in traces:
Expand Down
4 changes: 2 additions & 2 deletions climate_emotions_map/make_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def make_map(
impact_marker_size_scale: float = 1.0,
colormap_range_padding: int = 10,
margins: dict = None,
decimals: int = 0,
decimals: int = 1,
) -> go.Figure:
"""Generate choropleth map showing opinion and/or impact data.
Expand Down Expand Up @@ -97,7 +97,7 @@ def make_map(
margins : dict | None, optional
Margins for the Plotly figure, by default 30 everywhere
decimals : int, optional
Number of decimals to show in the hoverbox, by default 0
Number of decimals to show in the hoverbox, by default 1
Returns
-------
Expand Down
12 changes: 6 additions & 6 deletions climate_emotions_map/make_stacked_bar_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def plot_bars(
y="question",
color="outcome",
title=None, # TODO: remove this argument?
round_to=2, # NOTE: This is the number of decimal places to round the data to, BEFORE multiplying by 100.
decimals=1,
sort_order="descending",
facet_order=None,
palette=None,
Expand All @@ -145,11 +145,7 @@ def plot_bars(
"""Make a stacked bar plot of the opinions of the whole sample, split by state and party."""
facet_var = "sub_question"

# Determine the appropriate number of decimal places to use after converting data
# to percentages based on the applied rounding, to use in hover text
decimals = max(0, round_to - 2)

plot_df[x] = plot_df[x].round(round_to) * 100
plot_df[x] = plot_df[x] * 100

# sort by subquestion

Expand Down Expand Up @@ -331,6 +327,7 @@ def make_stacked_bar(
state: str | None = None,
stratify: bool = False,
threshold: str | None = None,
decimals: int = 1,
palettes: dict = None,
fig_kw: dict = None,
) -> px.bar:
Expand All @@ -350,6 +347,8 @@ def make_stacked_bar(
Whether to stratify the data by party. The default is False.
threshold : str, optional
The outcome ID for the Likert endorsement level to threshold at (e.g. "3+"). The default is None.
decimals : int, optional
The number of decimal places to display for the percentage values. The default is 1.
palettes : dict, optional
A dictionary of color palettes for different numbers of outcomes. The default is None.
fig_kw : dict, optional
Expand Down Expand Up @@ -449,6 +448,7 @@ def make_stacked_bar(
y=y,
facet_order=facet_order,
sort_order=sort_order,
decimals=decimals,
palette=palette,
fig_kw=fig_kw,
)
Expand Down
2 changes: 2 additions & 0 deletions climate_emotions_map/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"all_questions": "Select a survey domain to view responses for",
"demographics": "Sample Characteristics",
}
# Number of decimal places to round to in all plots
NUM_DECIMALS = 1

# We have not yet decided on the best colormaps to use
# OPINION_COLORMAP = "OrRd"
Expand Down

0 comments on commit 80cbefd

Please sign in to comment.