Skip to content

Commit

Permalink
ggplot hist draft fix (#711)
Browse files Browse the repository at this point in the history
  • Loading branch information
bbeat2782 authored Jul 24, 2023
1 parent c433e53 commit a2fcc39
Show file tree
Hide file tree
Showing 19 changed files with 33 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
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):
"""
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
Binary file modified src/tests/baseline_images/test_ggplot/facet_wrap_nulls_data.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_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.

0 comments on commit a2fcc39

Please sign in to comment.