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

feat: replace seaborn with matplotlib for box_plot #863

Merged
merged 18 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
364 changes: 204 additions & 160 deletions docs/tutorials/data_visualization.ipynb
Muellersen marked this conversation as resolved.
Show resolved Hide resolved

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
63 changes: 41 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,30 +52,49 @@ 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]
number_of_columns = 3
number_of_rows = ceil(len(columns) / number_of_columns)

fig, axs = plt.subplots(nrows=number_of_rows, ncols=number_of_columns)
fig.set_size_inches(10, 6)

line = 0
for i, column in enumerate(columns):
if i % number_of_columns == 0 and i != 0:
line += 1

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:
fig.delaxes(axs[i])
else:
fig.delaxes(axs[number_of_rows - 1, i])

fig.tight_layout()

return _figure_to_image(fig)

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.