Skip to content

Commit

Permalink
feat: outline around dots of scatterplot (#785)
Browse files Browse the repository at this point in the history
### Summary of Changes

Draw a white outline around the dots of a scatterplot to easily view
overlapping dots.

---------

Co-authored-by: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com>
  • Loading branch information
lars-reimann and megalinter-bot authored May 17, 2024
1 parent 46d38b9 commit ee8acf7
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 10 deletions.
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)

0 comments on commit ee8acf7

Please sign in to comment.