diff --git a/doc/changelog.qmd b/doc/changelog.qmd index 24b646a3c..d8472909e 100644 --- a/doc/changelog.qmd +++ b/doc/changelog.qmd @@ -48,6 +48,9 @@ title: Changelog - Fixed bug in [](:class:`~plotnine.geoms.geom_segment`) where the lineend parameter was ignored. ({{< issue 727 >}}) +- Fixed [](:class:`~plotnine.stat_summary_bin`) to work with continuous `x` + aesthetic when specifying the `binwidth` or `breaks`. ({{< issue 824 >}}) + ## v0.13.6 (2024-05-09) diff --git a/plotnine/stats/stat_summary_bin.py b/plotnine/stats/stat_summary_bin.py index 991bccecd..e07e56a4b 100644 --- a/plotnine/stats/stat_summary_bin.py +++ b/plotnine/stats/stat_summary_bin.py @@ -141,6 +141,7 @@ def compute_group(cls, data, scales, **params): ) breaks = fuzzybreaks(scales.x, breaks, boundary, binwidth, bins) + bins = len(breaks) - 1 data["bin"] = pd.cut( data["x"], bins=breaks, # pyright: ignore diff --git a/tests/baseline_images/test_stat_summary_bin/setting_binwidth.png b/tests/baseline_images/test_stat_summary_bin/setting_binwidth.png new file mode 100644 index 000000000..cea54b481 Binary files /dev/null and b/tests/baseline_images/test_stat_summary_bin/setting_binwidth.png differ diff --git a/tests/test_stat_summary_bin.py b/tests/test_stat_summary_bin.py index 5956e13a3..a80e63be9 100644 --- a/tests/test_stat_summary_bin.py +++ b/tests/test_stat_summary_bin.py @@ -26,3 +26,8 @@ def test_continuous_x(): ) assert p == "continuous_x" + + +def test_setting_binwidth(): + p = ggplot(data, aes("xc", "y")) + stat_summary_bin(binwidth=3, geom="bar") + assert p == "setting_binwidth"