Skip to content

Commit 1d94315

Browse files
committed
Add shape diagnostic function
1 parent 96385a5 commit 1d94315

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

src/data_morph/data/dataset.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ def plot(
201201
ax: Axes | None = None,
202202
show_bounds: bool = True,
203203
title: str | None = 'default',
204+
alpha: Number = 1,
204205
) -> Axes:
205206
"""
206207
Plot the dataset and its bounds.
@@ -214,6 +215,8 @@ def plot(
214215
title : str | ``None``, optional
215216
Title to use for the plot. The default will call ``str()`` on the
216217
Dataset. Pass ``None`` to leave the plot untitled.
218+
alpha : Number, default ``1``
219+
The transparency to use for the points in the plot.
217220
218221
Returns
219222
-------
@@ -225,7 +228,7 @@ def plot(
225228
fig.get_layout_engine().set(w_pad=0.2, h_pad=0.2)
226229

227230
ax.axis('equal')
228-
ax.scatter(self.data.x, self.data.y, s=2, color='black')
231+
ax.scatter(self.data.x, self.data.y, s=2, color='black', alpha=alpha)
229232
ax.set(xlabel='', ylabel='', title=self if title == 'default' else title)
230233

231234
if show_bounds:
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
"""Diagnostic plot to visualize a shape superimposed on the dataset."""
2+
3+
from __future__ import annotations
4+
5+
from typing import TYPE_CHECKING
6+
7+
from ..plotting.style import plot_with_custom_style
8+
9+
if TYPE_CHECKING:
10+
from numbers import Number
11+
12+
from matplotlib.axes import Axes
13+
14+
from ..data.dataset import Dataset
15+
from ..shapes.bases.shape import Shape
16+
17+
18+
@plot_with_custom_style
19+
def plot_shape_on_dataset(
20+
dataset: Dataset,
21+
shape: Shape,
22+
show_bounds: bool = False,
23+
alpha: Number = 0.25,
24+
) -> Axes:
25+
"""
26+
Plot a shape superimposed on a dataset to evaluate heuristics.
27+
28+
Parameters
29+
----------
30+
dataset : Dataset
31+
The dataset that ``shape`` was instantiated with.
32+
shape : Shape
33+
The shape that was instantiated with ``dataset``.
34+
show_bounds : bool, default ``False``
35+
Whether to include the dataset's bounds in the plot.
36+
alpha : Number, default ``0.25``
37+
The transparency to use for the dataset's points.
38+
39+
Returns
40+
-------
41+
matplotlib.axes.Axes
42+
The :class:`~matplotlib.axes.Axes` object containing the plot.
43+
44+
Examples
45+
--------
46+
47+
.. plot::
48+
:scale: 75
49+
:include-source:
50+
:caption:
51+
Visualization of the :class:`.Star` when calculated based on the
52+
music :class:`.Dataset`, with the dataset's bounds.
53+
54+
from data_morph.data.loader import DataLoader
55+
from data_morph.plotting.diagnostics import plot_shape_on_dataset
56+
from data_morph.shapes.lines import Star
57+
58+
dataset = DataLoader.load_dataset('music')
59+
shape = Star(dataset)
60+
plot_shape_on_dataset(dataset, shape, show_bounds=True, alpha=0.1)
61+
62+
.. plot::
63+
:scale: 75
64+
:include-source:
65+
:caption:
66+
Visualization of the :class:`.Heart` when calculated based on the
67+
music :class:`.Dataset`, without the dataset's bounds.
68+
69+
from data_morph.data.loader import DataLoader
70+
from data_morph.plotting.diagnostics import plot_shape_on_dataset
71+
from data_morph.shapes.points import Heart
72+
73+
dataset = DataLoader.load_dataset('music')
74+
shape = Heart(dataset)
75+
plot_shape_on_dataset(dataset, shape, alpha=0.1)
76+
"""
77+
ax = dataset.plot(show_bounds=show_bounds, title=None, alpha=alpha)
78+
shape.plot(ax=ax)
79+
return ax

0 commit comments

Comments
 (0)