Skip to content

Commit

Permalink
feat: replace seaborn with matplotlib for box_plot (#863)
Browse files Browse the repository at this point in the history
Closes #805
Closes #849 

### Summary of Changes

This pull request applies the changes of replacing the last import of
seaborn with matplotlib, so that seaborn will be removed as dependency
with this pull request.

We also replaced the snapshot images of the box_plot test, to ensure
that the test run successfully.

---------

Co-authored-by: thelenf0 <s1frthel@uni-bonn.de>
Co-authored-by: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com>
  • Loading branch information
3 people committed Jun 28, 2024
1 parent 8f78163 commit 4ef078e
Show file tree
Hide file tree
Showing 7 changed files with 252 additions and 206 deletions.
364 changes: 204 additions & 160 deletions docs/tutorials/data_visualization.ipynb

Large diffs are not rendered by default.

26 changes: 3 additions & 23 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ matplotlib = "^3.6.3"
pillow = ">=9.5,<11.0"
polars = {extras = ["numpy", "pyarrow"], version = "^0.20.25"}
scikit-learn = "^1.2.0"
seaborn = "^0.13.0"
statsmodels = "^0.14.1"
torch = "^2.3.0"
torchvision = "^0.18.0"
Expand Down
67 changes: 45 additions & 22 deletions src/safeds/data/tabular/plotting/_table_plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ def __init__(self, table: Table):

def box_plots(self) -> Image:
"""
Plot a boxplot for every numerical column.
Create a box plot for every numerical column.
Returns
-------
plot:
The plot as an image.
The box plot(s) as an image.
Raises
------
Expand All @@ -52,31 +52,54 @@ def box_plots(self) -> Image:
>>> table = Table({"a":[1, 2], "b": [3, 42]})
>>> image = table.plot.box_plots()
"""
# TODO: implement using matplotlib and polars
import matplotlib.pyplot as plt
import seaborn as sns

numerical_table = self._table.remove_non_numeric_columns()
if numerical_table.column_count == 0:
raise NonNumericColumnError("This table contains only non-numerical columns.")
col_wrap = min(numerical_table.column_count, 3)
from math import ceil

data = numerical_table._lazy_frame.melt(value_vars=numerical_table.column_names).collect()
grid = sns.FacetGrid(data, col="variable", col_wrap=col_wrap, sharex=False, sharey=False)
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
message="Using the boxplot function without specifying `order` is likely to produce an incorrect plot.",
)
grid.map(sns.boxplot, "variable", "value")
grid.set_xlabels("")
grid.set_ylabels("")
grid.set_titles("{col_name}")
for axes in grid.axes.flat:
axes.set_xticks([])
plt.tight_layout()
fig = grid.fig
import matplotlib.pyplot as plt

columns = numerical_table.to_columns()
columns = [column._series.drop_nulls() for column in columns]
max_width = 3
number_of_columns = len(columns) if len(columns) <= max_width else max_width
number_of_rows = ceil(len(columns) / number_of_columns)

fig, axs = plt.subplots(nrows=number_of_rows, ncols=number_of_columns)
line = 0
for i, column in enumerate(columns):
if i % number_of_columns == 0 and i != 0:
line += 1

if number_of_columns == 1:
axs.boxplot(
column,
patch_artist=True,
labels=[numerical_table.column_names[i]],
)
break

if number_of_rows == 1:
axs[i].boxplot(
column,
patch_artist=True,
labels=[numerical_table.column_names[i]],
)

else:
axs[line, i % number_of_columns].boxplot(
column,
patch_artist=True,
labels=[numerical_table.column_names[i]],
)

# removes unused ax indices, so there wont be empty plots
last_filled_ax_index = len(columns) % number_of_columns
for i in range(last_filled_ax_index, number_of_columns):
if number_of_rows != 1 and last_filled_ax_index != 0:
fig.delaxes(axs[number_of_rows - 1, i])

fig.tight_layout()
return _figure_to_image(fig)

def correlation_heatmap(self) -> Image:
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.
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 4ef078e

Please sign in to comment.