Skip to content

Commit

Permalink
fix: sort x values of line plot (#782)
Browse files Browse the repository at this point in the history
### Summary of Changes

x values of line plots are now sorted properly to prevent crisscrossing
lines.

---------

Co-authored-by: megalinter-bot <[email protected]>
  • Loading branch information
lars-reimann and megalinter-bot authored May 17, 2024
1 parent f61cceb commit 74d8649
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/safeds/data/tabular/plotting/_table_plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,8 @@ def line_plot(self, x_name: str, y_name: str) -> Image:
import polars as pl

grouped = (
self._table._lazy_frame.group_by(x_name, maintain_order=True)
self._table._lazy_frame.sort(x_name)
.group_by(x_name)
.agg(
mean=pl.mean(y_name),
count=pl.count(y_name),
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.
20 changes: 16 additions & 4 deletions tests/safeds/data/tabular/containers/_table/test_plot_lineplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,22 @@
from syrupy import SnapshotAssertion


def test_should_match_snapshot(snapshot_png_image: SnapshotAssertion) -> None:
table = Table({"A": [1, 2, 3], "B": [2, 4, 7]})
lineplot = table.plot.line_plot("A", "B")
assert lineplot == snapshot_png_image
@pytest.mark.parametrize(
("table", "x_name", "y_name"),
[
(Table({"A": [1, 2, 3], "B": [2, 4, 7]}), "A", "B"),
(Table({"A": [1, 1, 2, 2], "B": [2, 4, 6, 8]}), "A", "B"),
(Table({"A": [2, 1, 3, 3, 1, 2], "B": [6, 2, 5, 5, 4, 8]}), "A", "B"),
],
ids=[
"functional",
"sorted grouped",
"unsorted grouped",
],
)
def test_should_match_snapshot(table: Table, x_name: str, y_name: str, snapshot_png_image: SnapshotAssertion) -> None:
line_plot = table.plot.line_plot(x_name, y_name)
assert line_plot == snapshot_png_image


@pytest.mark.parametrize(
Expand Down

0 comments on commit 74d8649

Please sign in to comment.