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

Add show_plot_traces method to plotly #1004

Merged
merged 2 commits into from
May 3, 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
26 changes: 22 additions & 4 deletions stonesoup/plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1497,24 +1497,42 @@ def plot_sensors(self, sensors, mapping=[0, 1], sensor_label="Sensors", **kwargs
sensor_xy = np.array([sensor.position[mapping, 0] for sensor in sensors])
self.fig.add_scatter(x=sensor_xy[:, 0], y=sensor_xy[:, 1], **sensor_kwargs)

def hide_plot_traces(self, items_to_hide: set):
def hide_plot_traces(self, items_to_hide=None):
"""Hide Plot Traces

This function allows plotting items to be invisible as default. Users can toggle the plot
trace to visible.

Parameters
----------
items_to_hide : set[str]
items_to_hide : Iterable[str]
The legend label (`legendgroups`) for the plot traces that should be invisible as
default
default. If left as ``None`` no traces will be shown.
"""
for fig_data in self.fig.data:
if fig_data.legendgroup in items_to_hide:
if items_to_hide is None or fig_data.legendgroup in items_to_hide:
fig_data.visible = "legendonly"
else:
fig_data.visible = None

def show_plot_traces(self, items_to_show=None):
"""Show Plot Traces

This function allows specific plotting items to be shown as default. All labels not
mentioned in `items_to_show` will be invisible and can be manually toggled on.

Parameters
----------
items_to_show : Iterable[str]
The legend label (`legendgroups`) for the plot traces that should be shown as
default. If left as ``None`` all traces will be shown.
"""
for fig_data in self.fig.data:
if items_to_show is None or fig_data.legendgroup in items_to_show:
fig_data.visible = None
else:
fig_data.visible = "legendonly"


class PolarPlotterly(_Plotter):

Expand Down
54 changes: 54 additions & 0 deletions stonesoup/tests/test_plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,3 +317,57 @@ def test_plotterly_wrong_dimension(dim, mapping):

with pytest.raises(TypeError):
plotter.plot_tracks(track, mapping)


@pytest.mark.parametrize("labels", [
None, ["Tracks"], ["Ground Truth", "Tracks"],
["Ground Truth", "Measurements<br>(Detections)", "Tracks"]])
def test_hide_plot(labels):
plotter = Plotterly()
plotter.plot_ground_truths(truth, [0, 1])
plotter.plot_measurements(all_measurements, [0, 1])
plotter.plot_tracks(track, [0, 1])

plotter.hide_plot_traces(labels)

hidden = 0
showing = 0

for fig_data in plotter.fig.data:
if fig_data["visible"] == "legendonly":
hidden += 1
elif fig_data["visible"] is None:
showing += 1

if labels is None:
assert hidden == 3
else:
assert hidden == len(labels)
assert hidden + showing == 3


@pytest.mark.parametrize("labels", [
None, ["Tracks"], ["Ground Truth", "Tracks"],
["Ground Truth", "Measurements<br>(Detections)", "Tracks"]])
def test_show_plot(labels):
plotter = Plotterly()
plotter.plot_ground_truths(truth, [0, 1])
plotter.plot_measurements(all_measurements, [0, 1])
plotter.plot_tracks(track, [0, 1])

plotter.show_plot_traces(labels)

showing = 0
hidden = 0

for fig_data in plotter.fig.data:
if fig_data["visible"] == "legendonly":
hidden += 1
elif fig_data["visible"] is None:
showing += 1

if labels is None:
assert showing == 3
else:
assert showing == len(labels)
assert showing + hidden == 3