Skip to content
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
18 changes: 6 additions & 12 deletions src/safeds/data/tabular/containers/_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -975,22 +975,16 @@ def shuffle(self) -> Table:

def correlation_heatmap(self) -> None:
"""
Plot a correlation heatmap of an entire table. This function can only plot real numerical data.

Raises
-------
TypeError
If the table contains non-numerical data or complex data.
Plot a correlation heatmap for all numerical columns of this `Table`.
"""
for column in self.to_columns():
if not column.type.is_numeric():
raise NonNumericColumnError(column.name)
only_numerical = Table.from_columns(self.list_columns_with_numerical_values())

sns.heatmap(
data=self._data.corr(),
data=only_numerical._data.corr(),
vmin=-1,
vmax=1,
xticklabels=self.get_column_names(),
yticklabels=self.get_column_names(),
xticklabels=only_numerical.get_column_names(),
yticklabels=only_numerical.get_column_names(),
cmap="vlag",
)
plt.tight_layout()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import _pytest
import matplotlib.pyplot as plt
import pandas as pd
import pytest
from safeds.data.tabular.containers import Table
from safeds.exceptions import NonNumericColumnError


def test_correlation_heatmap_non_numeric() -> None:
with pytest.raises(NonNumericColumnError):
table = Table(pd.DataFrame(data={"A": [1, 2, "A"], "B": [1, 2, "A"]}))
table.correlation_heatmap()
def test_correlation_heatmap_non_numeric(monkeypatch: _pytest.monkeypatch) -> None:
monkeypatch.setattr(plt, "show", lambda: None)
table = Table(pd.DataFrame(data={"A": [1, 2, "A"], "B": [1, 2, 3]}))
table.correlation_heatmap()


def test_correlation_heatmap(monkeypatch: _pytest.monkeypatch) -> None:
Expand Down