Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ggplot histograms vertical breaks fix #711

Merged
merged 1 commit into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## 0.8.1dev

* [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))
Expand Down
8 changes: 7 additions & 1 deletion doc/user-guide/ggplot.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
32 changes: 24 additions & 8 deletions src/sql/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
neelasha23 marked this conversation as resolved.
Show resolved Hide resolved
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()
Expand Down Expand Up @@ -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
)
Expand Down Expand Up @@ -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_,
Expand All @@ -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]
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/tests/baseline_images/test_ggplot/histogram_default.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/tests/baseline_images/test_magic_plot/hist.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/tests/baseline_images/test_magic_plot/hist_bin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/tests/baseline_images/test_magic_plot/hist_custom.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/tests/baseline_images/test_magic_plot/hist_null.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/tests/baseline_images/test_magic_plot/hist_two.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading