correlation plots for best bc

This commit is contained in:
2026-02-04 10:46:31 +01:00
parent ad1d8c6e58
commit e17646eb70
2 changed files with 164 additions and 78 deletions

View File

@@ -253,9 +253,15 @@ class QualtricsPlotsMixin:
return chart.properties(title=title_config)
def _save_plot(self, chart: alt.Chart, title: str) -> alt.Chart:
def _save_plot(self, chart: alt.Chart, title: str, filename: str | None = None) -> alt.Chart:
"""Save chart to PNG file if fig_save_dir is set.
Args:
chart: The Altair chart to save
title: Chart title (used for filename if filename not provided)
filename: Optional explicit filename (without extension). If provided,
this is used instead of deriving from title.
Returns the (potentially modified) chart with filter footnote added.
"""
# Add filter footnote - returns combined chart if filters active
@@ -270,8 +276,10 @@ class QualtricsPlotsMixin:
if not path.exists():
path.mkdir(parents=True, exist_ok=True)
filename = f"{self._sanitize_filename(title)}.png"
# Use explicit filename if provided, otherwise derive from title
base_name = filename if filename else self._sanitize_filename(title)
filename = f"{base_name}.png"
filepath = path / filename
# Use vl_convert directly with theme config for consistent rendering
@@ -397,6 +405,7 @@ class QualtricsPlotsMixin:
self,
data: pl.LazyFrame | pl.DataFrame | None = None,
title: str = "General Impression (1-10)\nPer Voice with Number of Participants Who Rated It",
filename: str | None = None,
x_label: str = "Stimuli",
y_label: str = "Average General Impression Rating (1-10)",
color: str = ColorPalette.PRIMARY,
@@ -408,6 +417,7 @@ class QualtricsPlotsMixin:
"""Create a bar plot showing average scores and count of non-null values for each column.
Parameters:
filename: Optional explicit filename (without extension) for saving.
color_gender: If True, color bars by voice gender (blue=male, pink=female).
"""
df = self._ensure_dataframe(data)
@@ -484,7 +494,7 @@ class QualtricsPlotsMixin:
height=height or getattr(self, 'plot_height', 400)
)
chart = self._save_plot(chart, title)
chart = self._save_plot(chart, title, filename=filename)
return chart
def plot_top3_ranking_distribution(
@@ -803,6 +813,7 @@ class QualtricsPlotsMixin:
self,
data: pl.LazyFrame | pl.DataFrame | None = None,
title: str = "Weighted Popularity Score\n(1st=3pts, 2nd=2pts, 3rd=1pt)",
filename: str | None = None,
x_label: str = "Character Personality",
y_label: str = "Total Weighted Score",
color: str = ColorPalette.PRIMARY,
@@ -813,6 +824,7 @@ class QualtricsPlotsMixin:
"""Create a bar chart showing the weighted ranking score for each character.
Parameters:
filename: Optional explicit filename (without extension) for saving.
color_gender: If True, color bars by voice gender (blue=male, pink=female).
"""
weighted_df = self._ensure_dataframe(data).to_pandas()
@@ -863,7 +875,7 @@ class QualtricsPlotsMixin:
height=height or getattr(self, 'plot_height', 400)
)
chart = self._save_plot(chart, title)
chart = self._save_plot(chart, title, filename=filename)
return chart
def plot_voice_selection_counts(
@@ -1179,16 +1191,22 @@ class QualtricsPlotsMixin:
chart = self._save_plot(chart, title)
return chart
def plot_speaking_style_correlation(
def plot_speaking_style_scale_correlation(
self,
style_color: str,
style_traits: list[str],
data: pl.LazyFrame | pl.DataFrame | None = None,
title: str | None = None,
filename: str | None = None,
width: int | str | None = None,
height: int | None = None,
) -> alt.Chart:
"""Plots correlation between Speaking Style Trait Scores (1-5) and Voice Scale (1-10)."""
"""Plots correlation between Speaking Style Trait Scores (1-5) and Voice Scale (1-10).
Args:
filename: Optional explicit filename (without extension) for saving.
If not provided, filename is derived from title.
"""
df = self._ensure_dataframe(data)
if title is None:
@@ -1235,13 +1253,14 @@ class QualtricsPlotsMixin:
height=height or 350
)
chart = self._save_plot(chart, title)
chart = self._save_plot(chart, title, filename=filename)
return chart
def plot_speaking_style_color_correlation(
self,
data: pl.LazyFrame | pl.DataFrame | None = None,
title: str = "Speaking Style and Voice Scale 1-10 Correlations<br>(Average by Color)",
filename: str | None = None,
width: int | str | None = None,
height: int | None = None,
) -> alt.Chart:
@@ -1255,6 +1274,8 @@ class QualtricsPlotsMixin:
data: DataFrame with columns [Color, correlation, n_traits] from
utils.transform_speaking_style_color_correlation
title: Chart title (supports <br> for line breaks)
filename: Optional explicit filename (without extension) for saving.
If not provided, filename is derived from title.
width: Chart width in pixels
height: Chart height in pixels
@@ -1289,7 +1310,7 @@ class QualtricsPlotsMixin:
height=height or 350
)
chart = self._save_plot(chart, title)
chart = self._save_plot(chart, title, filename=filename)
return chart
def plot_demographic_distribution(
@@ -1415,10 +1436,16 @@ class QualtricsPlotsMixin:
style_traits: list[str],
data: pl.LazyFrame | pl.DataFrame | None = None,
title: str | None = None,
filename: str | None = None,
width: int | str | None = None,
height: int | None = None,
) -> alt.Chart:
"""Plots correlation between Speaking Style Trait Scores (1-5) and Voice Ranking Points (0-3)."""
"""Plots correlation between Speaking Style Trait Scores (1-5) and Voice Ranking Points (0-3).
Args:
filename: Optional explicit filename (without extension) for saving.
If not provided, filename is derived from title.
"""
df = self._ensure_dataframe(data)
if title is None:
@@ -1462,7 +1489,7 @@ class QualtricsPlotsMixin:
height=height or 350
)
chart = self._save_plot(chart, title)
chart = self._save_plot(chart, title, filename=filename)
return chart
def plot_traits_wordcloud(