diff --git a/CHANGELOG.md b/CHANGELOG.md index bd518c6b6..4c6d8e742 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ ## 0.8.1dev * [Fix] Fix error that was incorrectly converted into a print message +* [Fix] Fixed vertical color breaks in histograms (#702) + ## 0.8.0 (2023-07-18) * [Feature] Modified `TableDescription` to add styling, generate messages and format the calculated outputs ([#459](https://github.com/ploomber/jupysql/issues/459)) diff --git a/doc/user-guide/ggplot.md b/doc/user-guide/ggplot.md index 4100c4582..f2f292310 100644 --- a/doc/user-guide/ggplot.md +++ b/doc/user-guide/ggplot.md @@ -5,7 +5,7 @@ jupytext: extension: .md format_name: myst format_version: 0.13 - jupytext_version: 1.14.5 + jupytext_version: 1.14.7 kernelspec: display_name: Python 3 (ipykernel) language: python @@ -185,6 +185,12 @@ We can show a histogram of multiple columns by setting `x=['cut', 'color']` (ggplot("diamonds", aes(x=["cut", "color"])) + geom_histogram()) ``` +We can also plot histograms for a combination of categorical and numerical columns. + +```{code-cell} ipython3 +(ggplot("diamonds", aes(x=["color", "carat"])) + geom_histogram(bins=30)) +``` + Apply a custom color with `color` and `fill` ```{code-cell} ipython3 diff --git a/src/sql/plot.py b/src/sql/plot.py index 8ddbf9a3d..e5c31fbfd 100644 --- a/src/sql/plot.py +++ b/src/sql/plot.py @@ -306,14 +306,30 @@ def _are_numeric_values(*values): return all([isinstance(value, (int, float)) for value in values]) -def _get_bar_width(ax, bins): +def _get_bar_width(ax, bins, bin_size): """ Return a single bar width based on number of bins If bins values are str, calculate value based on figure size. + + Parameters + ---------- + ax : matplotlib.Axes + Generated plot + + bins : tuple + Contains bins' midpoints as float + + bin_size : int or None + Calculated bin_size from the _histogram function + + Returns + ------- + width : float + A single bar width """ - if _are_numeric_values(bins[-1], bins[-2]): - width = bins[-1] - bins[-2] + if _are_numeric_values(bin_size): + width = bin_size else: fig = plt.gcf() bbox = ax.get_window_extent() @@ -398,7 +414,7 @@ def histogram( raise ValueError("Column name has not been specified") bin_, height, bin_size = _histogram(table, column, bins, with_=with_, conn=conn) - width = _get_bar_width(ax, bin_) + width = _get_bar_width(ax, bin_, bin_size) data = _histogram_stacked( table, column, category, bin_, bin_size, with_=with_, conn=conn, facet=facet ) @@ -441,10 +457,10 @@ def histogram( ax.set_title(f"Histogram from {table!r}") ax.legend() elif isinstance(column, str): - bin_, height, _ = _histogram( + bin_, height, bin_size = _histogram( table, column, bins, with_=with_, conn=conn, facet=facet ) - width = _get_bar_width(ax, bin_) + width = _get_bar_width(ax, bin_, bin_size) ax.bar( bin_, @@ -460,10 +476,10 @@ def histogram( else: for i, col in enumerate(column): - bin_, height, _ = _histogram( + bin_, height, bin_size = _histogram( table, col, bins, with_=with_, conn=conn, facet=facet ) - width = _get_bar_width(ax, bin_) + width = _get_bar_width(ax, bin_, bin_size) if isinstance(color, list): color_ = color[i] diff --git a/src/tests/baseline_images/test_ggplot/facet_wrap_nulls_data.png b/src/tests/baseline_images/test_ggplot/facet_wrap_nulls_data.png index 27e46d789..be657a3af 100644 Binary files a/src/tests/baseline_images/test_ggplot/facet_wrap_nulls_data.png and b/src/tests/baseline_images/test_ggplot/facet_wrap_nulls_data.png differ diff --git a/src/tests/baseline_images/test_ggplot/histogram_default.png b/src/tests/baseline_images/test_ggplot/histogram_default.png index 5ab80bad2..e613ae478 100644 Binary files a/src/tests/baseline_images/test_ggplot/histogram_default.png and b/src/tests/baseline_images/test_ggplot/histogram_default.png differ diff --git a/src/tests/baseline_images/test_ggplot/histogram_numeric_categorical_combined.png b/src/tests/baseline_images/test_ggplot/histogram_numeric_categorical_combined.png index b4a8a7d17..8628c4e2d 100644 Binary files a/src/tests/baseline_images/test_ggplot/histogram_numeric_categorical_combined.png and b/src/tests/baseline_images/test_ggplot/histogram_numeric_categorical_combined.png differ diff --git a/src/tests/baseline_images/test_ggplot/histogram_numeric_categorical_combined_custom_fill.png b/src/tests/baseline_images/test_ggplot/histogram_numeric_categorical_combined_custom_fill.png index 72e46248e..052cccc5a 100644 Binary files a/src/tests/baseline_images/test_ggplot/histogram_numeric_categorical_combined_custom_fill.png and b/src/tests/baseline_images/test_ggplot/histogram_numeric_categorical_combined_custom_fill.png differ diff --git a/src/tests/baseline_images/test_ggplot/histogram_numeric_categorical_combined_custom_multi_color.png b/src/tests/baseline_images/test_ggplot/histogram_numeric_categorical_combined_custom_multi_color.png index 7eae712ca..736217c8e 100644 Binary files a/src/tests/baseline_images/test_ggplot/histogram_numeric_categorical_combined_custom_multi_color.png and b/src/tests/baseline_images/test_ggplot/histogram_numeric_categorical_combined_custom_multi_color.png differ diff --git a/src/tests/baseline_images/test_ggplot/histogram_numeric_categorical_combined_custom_multi_fill.png b/src/tests/baseline_images/test_ggplot/histogram_numeric_categorical_combined_custom_multi_fill.png index c15a20452..dd4e7a9ed 100644 Binary files a/src/tests/baseline_images/test_ggplot/histogram_numeric_categorical_combined_custom_multi_fill.png and b/src/tests/baseline_images/test_ggplot/histogram_numeric_categorical_combined_custom_multi_fill.png differ diff --git a/src/tests/baseline_images/test_magic_plot/hist.png b/src/tests/baseline_images/test_magic_plot/hist.png index 880bba341..43b200728 100644 Binary files a/src/tests/baseline_images/test_magic_plot/hist.png and b/src/tests/baseline_images/test_magic_plot/hist.png differ diff --git a/src/tests/baseline_images/test_magic_plot/hist_bin.png b/src/tests/baseline_images/test_magic_plot/hist_bin.png index 64607a982..47f1d6cdf 100644 Binary files a/src/tests/baseline_images/test_magic_plot/hist_bin.png and b/src/tests/baseline_images/test_magic_plot/hist_bin.png differ diff --git a/src/tests/baseline_images/test_magic_plot/hist_custom.png b/src/tests/baseline_images/test_magic_plot/hist_custom.png index 63fc49f67..287d48ecb 100644 Binary files a/src/tests/baseline_images/test_magic_plot/hist_custom.png and b/src/tests/baseline_images/test_magic_plot/hist_custom.png differ diff --git a/src/tests/baseline_images/test_magic_plot/hist_null.png b/src/tests/baseline_images/test_magic_plot/hist_null.png index 040f68d84..612fe9e49 100644 Binary files a/src/tests/baseline_images/test_magic_plot/hist_null.png and b/src/tests/baseline_images/test_magic_plot/hist_null.png differ diff --git a/src/tests/baseline_images/test_magic_plot/hist_two.png b/src/tests/baseline_images/test_magic_plot/hist_two.png index b87eaa241..cdc3564a5 100644 Binary files a/src/tests/baseline_images/test_magic_plot/hist_two.png and b/src/tests/baseline_images/test_magic_plot/hist_two.png differ diff --git a/src/tests/integration/baseline_images/test_questDB/custom_engine_histogram.png b/src/tests/integration/baseline_images/test_questDB/custom_engine_histogram.png index bac4357e1..311a8635b 100644 Binary files a/src/tests/integration/baseline_images/test_questDB/custom_engine_histogram.png and b/src/tests/integration/baseline_images/test_questDB/custom_engine_histogram.png differ diff --git a/src/tests/integration/baseline_images/test_questDB/histogram_numeric_categorical_combined.png b/src/tests/integration/baseline_images/test_questDB/histogram_numeric_categorical_combined.png index 4567b38de..653ae6562 100644 Binary files a/src/tests/integration/baseline_images/test_questDB/histogram_numeric_categorical_combined.png and b/src/tests/integration/baseline_images/test_questDB/histogram_numeric_categorical_combined.png differ diff --git a/src/tests/integration/baseline_images/test_questDB/histogram_numeric_categorical_combined_custom_fill.png b/src/tests/integration/baseline_images/test_questDB/histogram_numeric_categorical_combined_custom_fill.png index 19a93dc38..13c6a500a 100644 Binary files a/src/tests/integration/baseline_images/test_questDB/histogram_numeric_categorical_combined_custom_fill.png and b/src/tests/integration/baseline_images/test_questDB/histogram_numeric_categorical_combined_custom_fill.png differ diff --git a/src/tests/integration/baseline_images/test_questDB/histogram_numeric_categorical_combined_custom_multi_color.png b/src/tests/integration/baseline_images/test_questDB/histogram_numeric_categorical_combined_custom_multi_color.png index b2a914d7e..630ac4d17 100644 Binary files a/src/tests/integration/baseline_images/test_questDB/histogram_numeric_categorical_combined_custom_multi_color.png and b/src/tests/integration/baseline_images/test_questDB/histogram_numeric_categorical_combined_custom_multi_color.png differ diff --git a/src/tests/integration/baseline_images/test_questDB/histogram_numeric_categorical_combined_custom_multi_fill.png b/src/tests/integration/baseline_images/test_questDB/histogram_numeric_categorical_combined_custom_multi_fill.png index cb82b2dd2..1e2f6a489 100644 Binary files a/src/tests/integration/baseline_images/test_questDB/histogram_numeric_categorical_combined_custom_multi_fill.png and b/src/tests/integration/baseline_images/test_questDB/histogram_numeric_categorical_combined_custom_multi_fill.png differ