Skip to content

Commit

Permalink
feat: Column.plot_histogram() using Table.plot_histograms for con…
Browse files Browse the repository at this point in the history
…sistent results (#726)

### Summary of Changes
- `Column.histogram()` now uses the same function as
`Table.histograms()` and fixed that way the same problems as
`Table.histograms()` had bevor the change
- update Test snapshots
  • Loading branch information
SamanHushi authored May 5, 2024
1 parent 215a472 commit 576492c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 20 deletions.
29 changes: 9 additions & 20 deletions src/safeds/data/tabular/containers/_column.py
Original file line number Diff line number Diff line change
Expand Up @@ -1011,10 +1011,15 @@ def plot_boxplot(self) -> Image:
buffer.seek(0)
return Image.from_bytes(buffer.read())

def plot_histogram(self) -> Image:
def plot_histogram(self, *, number_of_bins: int = 10) -> Image:
"""
Plot a column in a histogram.
Parameters
----------
number_of_bins:
The number of bins to use in the histogram. Default is 10.
Returns
-------
plot:
Expand All @@ -1026,25 +1031,9 @@ def plot_histogram(self) -> Image:
>>> column = Column("test", [1, 2, 3])
>>> histogram = column.plot_histogram()
"""
import matplotlib.pyplot as plt
import seaborn as sns

fig = plt.figure()
ax = sns.histplot(data=self._data)
ax.set_xticks(ax.get_xticks())
ax.set(xlabel=self.name)
ax.set_xticklabels(
ax.get_xticklabels(),
rotation=45,
horizontalalignment="right",
) # rotate the labels of the x Axis to prevent the chance of overlapping of the labels
plt.tight_layout()

buffer = io.BytesIO()
fig.savefig(buffer, format="png")
plt.close() # Prevents the figure from being displayed directly
buffer.seek(0)
return Image.from_bytes(buffer.read())
from safeds.data.tabular.containers import Table

return Table({self._name: self._data}).plot_histograms(number_of_bins=number_of_bins)

# ------------------------------------------------------------------------------------------------------------------
# Conversion
Expand Down
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 576492c

Please sign in to comment.