voice plots

This commit is contained in:
2026-02-03 00:12:18 +01:00
parent 97c4b07208
commit a7ee854ed0
3 changed files with 113 additions and 24 deletions

View File

@@ -385,13 +385,13 @@ class QualtricsPlotsMixin:
def plot_ranking_distribution(
self,
data: pl.LazyFrame | pl.DataFrame | None = None,
title: str = "Rankings Distribution\n(1st to 4th Place)",
title: str = "Rankings Distribution\n(1st to 3rd Place)",
x_label: str = "Item",
y_label: str = "Number of Votes",
height: int | None = None,
width: int | str | None = None,
) -> alt.Chart:
"""Create a stacked bar chart showing the distribution of rankings (1st to 4th)."""
"""Create a stacked bar chart showing the distribution of rankings (1st to 3rd)."""
df = self._ensure_dataframe(data)
stats = []
@@ -401,15 +401,15 @@ class QualtricsPlotsMixin:
r1 = df.filter(pl.col(col) == 1).height
r2 = df.filter(pl.col(col) == 2).height
r3 = df.filter(pl.col(col) == 3).height
r4 = df.filter(pl.col(col) == 4).height
total = r1 + r2 + r3 + r4
# r4 = df.filter(pl.col(col) == 4).height
total = r1 + r2 + r3
if total > 0:
label = self._clean_voice_label(col)
stats.append({'item': label, 'rank': 'Rank 1 (Best)', 'count': r1, 'rank1': r1})
stats.append({'item': label, 'rank': 'Rank 2', 'count': r2, 'rank1': r1})
stats.append({'item': label, 'rank': 'Rank 3', 'count': r3, 'rank1': r1})
stats.append({'item': label, 'rank': 'Rank 4 (Worst)', 'count': r4, 'rank1': r1})
# stats.append({'item': label, 'rank': 'Rank 4 (Worst)', 'count': r4, 'rank1': r1})
if not stats:
return alt.Chart(pd.DataFrame({'text': ['No data']})).mark_text().encode(text='text:N')
@@ -423,8 +423,8 @@ class QualtricsPlotsMixin:
x=alt.X('item:N', title=x_label, sort=alt.EncodingSortField(field='rank1', order='descending')),
y=alt.Y('count:Q', title=y_label, stack='zero'),
color=alt.Color('rank:N',
scale=alt.Scale(domain=['Rank 1 (Best)', 'Rank 2', 'Rank 3', 'Rank 4 (Worst)'],
range=[ColorPalette.RANK_1, ColorPalette.RANK_2, ColorPalette.RANK_3, ColorPalette.RANK_4]),
scale=alt.Scale(domain=['Rank 1 (Best)', 'Rank 2', 'Rank 3'],
range=[ColorPalette.RANK_1, ColorPalette.RANK_2, ColorPalette.RANK_3]),
legend=alt.Legend(orient='top', direction='horizontal', title=None)),
order=alt.Order('rank:N', sort='ascending'),
opacity=alt.condition(selection, alt.value(1), alt.value(0.2)),