Skip to content

Commit be2a456

Browse files
committed
use scatterplot for ranking page as well.
1 parent 676232d commit be2a456

3 files changed

Lines changed: 22 additions & 69 deletions

File tree

backend.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ def get_data_for_name(name: str) -> pd.DataFrame:
2525
return df_all.loc[df_all["Name"] == name].copy()
2626

2727

28-
def get_data_for_year(year: int) -> pd.DataFrame:
29-
return df_all.loc[df_all["Year"] == year].copy()
30-
31-
3228
def get_data_by_geo(
3329
include_nation: bool,
3430
include_states: bool,

streamlit_app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
with col_p:
6464
include_places = st.checkbox("Cities", True)
6565

66-
fig = viz.get_single_year_violinplot(
66+
fig = viz.get_single_year_scatterplot(
6767
year,
6868
column,
6969
location,

visualizations.py

Lines changed: 21 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def _get_ranking_hovertext(column: str) -> str:
4747
)
4848

4949

50-
def get_single_year_violinplot(
50+
def get_single_year_scatterplot(
5151
year: int,
5252
column: str,
5353
name_to_highlight: str | None = None,
@@ -56,49 +56,55 @@ def get_single_year_violinplot(
5656
include_counties: bool = True,
5757
include_places: bool = True,
5858
) -> go.Figure:
59-
df = be.get_data_for_year(year)
60-
6159
df = be.get_data_by_geo(
6260
include_nation, include_states, include_counties, include_places
6361
)
64-
df = df[df["Year"] == year]
62+
df = df[df["Year"] == year].reset_index(drop=True)
63+
64+
rng = np.random.default_rng(seed=42)
65+
jitter = rng.uniform(-0.3, 0.3, size=len(df))
6566

6667
fig = go.Figure()
6768

6869
fig.add_trace(
69-
go.Violin(
70+
go.Scatter(
7071
y=df[column],
71-
x=[column] * len(df),
72-
box_visible=True,
73-
meanline_visible=False,
74-
points="all",
75-
hoveron="points", # Removes the violin/box stat hovers, keeps point hovers
72+
x=jitter,
73+
mode="markers",
74+
marker=dict(size=8, opacity=0.5),
7675
customdata=df[["Name", column]].values,
7776
hovertemplate=_get_ranking_hovertext(column),
7877
name=column,
7978
showlegend=False,
8079
)
8180
)
8281

83-
# Optionally highlight a point
8482
if name_to_highlight:
8583
hdf = df[df["Name"] == name_to_highlight]
8684

8785
fig.add_trace(
8886
go.Scatter(
89-
x=[column],
87+
x=[0],
9088
y=hdf[column],
9189
mode="markers",
92-
marker=dict(color="red", size=12, symbol="star"),
90+
marker=dict(
91+
color="gold",
92+
size=14,
93+
symbol="star",
94+
line=dict(color="darkorange", width=1.5),
95+
),
9396
name=name_to_highlight,
9497
customdata=hdf[["Name", column]].values,
9598
hovertemplate=_get_ranking_hovertext(column),
9699
)
97100
)
98101

99102
fig.update_layout(
100-
title=f"{column} in {year}",
101-
xaxis=dict(visible=False),
103+
title=(
104+
f"{column}, {year}<br><sup>Each point represents a location. "
105+
"Hover to explore.</sup>"
106+
),
107+
xaxis=dict(visible=False, range=[-1, 1]),
102108
yaxis=dict(title=column, tickformat=","),
103109
)
104110

@@ -116,55 +122,6 @@ def _get_compare_hovertext() -> str:
116122
)
117123

118124

119-
def get_compare_violinplot(
120-
year1: int, year2: int, column: str, name_to_highlight: str | None = None
121-
) -> go.Figure:
122-
df = be.get_compare_df(year1, year2, column)
123-
124-
fig = go.Figure()
125-
126-
fig.add_trace(
127-
go.Violin(
128-
y=df["Percent Change"],
129-
x=["Percent Change"] * len(df),
130-
box_visible=True,
131-
meanline_visible=False,
132-
points="all",
133-
hoveron="points", # Removes the violin/box stat hovers, keeps point hovers
134-
customdata=df[["Name", "Percent Change"]].values,
135-
hovertemplate=_get_compare_hovertext(),
136-
name="Percent Change",
137-
showlegend=False,
138-
)
139-
)
140-
141-
# Optionally highlight a point
142-
if name_to_highlight:
143-
hdf = df[df["Name"] == name_to_highlight]
144-
145-
fig.add_trace(
146-
go.Scatter(
147-
x=["Percent Change"],
148-
y=hdf["Percent Change"],
149-
mode="markers",
150-
marker=dict(color="red", size=12, symbol="star"),
151-
name=name_to_highlight,
152-
customdata=hdf[["Name", "Percent Change"]].values,
153-
hovertemplate=_get_compare_hovertext(),
154-
)
155-
)
156-
157-
fig.update_layout(
158-
title=f"Percent Change in {column}<br><sup>{year1}{year2}</sup>",
159-
xaxis=dict(visible=False),
160-
yaxis=dict(title="Percent Change", tickformat=","),
161-
)
162-
163-
_add_source_footer(fig)
164-
165-
return fig
166-
167-
168125
def get_compare_scatterplot(
169126
year1: int, year2: int, column: str, name_to_highlight: str | None = None
170127
) -> go.Figure:

0 commit comments

Comments
 (0)