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: outline around dots of scatterplot #785

Merged
merged 2 commits into from
May 17, 2024
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
11 changes: 5 additions & 6 deletions src/safeds/data/tabular/plotting/_table_plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from safeds._utils import _figure_to_image
from safeds._validation import _check_columns_exist
from safeds._validation._check_columns_are_numeric import _check_columns_are_numeric
from safeds.exceptions import NonNumericColumnError

if TYPE_CHECKING:
Expand Down Expand Up @@ -322,19 +323,17 @@ def scatter_plot(self, x_name: str, y_name: str) -> Image:
>>> image = table.plot.scatter_plot("a", "b")
"""
_check_columns_exist(self._table, [x_name, y_name])

# TODO: pass list of columns names + extract validation
if not self._table.get_column(x_name).is_numeric:
raise NonNumericColumnError(x_name)
if not self._table.get_column(y_name).is_numeric:
raise NonNumericColumnError(y_name)
_check_columns_are_numeric(self._table, [x_name, y_name])

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.scatter(
x=self._table.get_column(x_name)._series,
y=self._table.get_column(y_name)._series,
s=64, # marker size
linewidth=1,
edgecolor="white",
)
ax.set(
xlabel=x_name,
Expand Down
Binary file not shown.
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.
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
import pytest
from safeds.data.tabular.containers import Table
from safeds.exceptions import ColumnNotFoundError
from safeds.exceptions import ColumnNotFoundError, ColumnTypeError
from syrupy import SnapshotAssertion


def test_should_match_snapshot(snapshot_png_image: SnapshotAssertion) -> None:
table = Table({"A": [1, 2, 3], "B": [2, 4, 7]})
scatterplot = table.plot.scatter_plot("A", "B")
@pytest.mark.parametrize(
("table", "x_name", "y_name"),
[
(Table({"A": [1, 2, 3], "B": [2, 4, 7]}), "A", "B"),
(
Table(
{
"A": [1, 0.99, 0.99, 2],
"B": [1, 0.99, 1.01, 2],
},
),
"A",
"B",
),
],
ids=[
"functional",
"overlapping",
],
)
def test_should_match_snapshot(table: Table, x_name: str, y_name: str, snapshot_png_image: SnapshotAssertion) -> None:
scatterplot = table.plot.scatter_plot(x_name, y_name)
assert scatterplot == snapshot_png_image


Expand All @@ -23,3 +42,15 @@ def test_should_match_snapshot(snapshot_png_image: SnapshotAssertion) -> None:
def test_should_raise_if_column_does_not_exist(table: Table, col1: str, col2: str) -> None:
with pytest.raises(ColumnNotFoundError):
table.plot.scatter_plot(col1, col2)


@pytest.mark.parametrize(
("table", "x_name", "y_name"),
[
(Table({"A": ["a", "b", "c"], "B": [2, 4, 7]}), "A", "B"),
(Table({"A": [1, 2, 3], "B": ["a", "b", "c"]}), "A", "B"),
],
)
def test_should_raise_if_columns_are_not_numeric(table: Table, x_name: str, y_name: str) -> None:
with pytest.raises(ColumnTypeError):
table.plot.scatter_plot(x_name, y_name)