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 push function to live plots #3

Merged
merged 2 commits into from
Aug 16, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Added

- LivePlot: Add `legend` shorthand function
- LivePlot: Add `push` function
- LivePlot: Add `reset` function
- Sketch: Add a `reset` function

Expand Down
24 changes: 20 additions & 4 deletions matplotlive/live_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,14 @@ def add_right(self, name: str, *args, **kwargs) -> None:
self.__add(name, "right", *args, **kwargs)

def send(self, name: str, value: float) -> None:
"""Send a new value for a given time series.
"""Send a new value to a time series, adding it if needed.

Args:
name: Name of the time series.
value: New value for the series.
args: If adding, positional arguments for ``pyplot.plot``.
kwargs: If adding, keyword arguments for ``pyplot.plot``.
"""
if name not in self.series:
self.add_left(name) # used in readme example
self.add_left(name)
# Deleting and appending is slightly faster than rolling an array of
# size 20 (mean ± std. dev. of 7 runs, 100,000 loops each):
#
Expand All @@ -132,6 +130,24 @@ def send(self, name: str, value: float) -> None:
self.__nb_updates[name] += 1
self.__max_updates = max(self.__max_updates, self.__nb_updates[name])

def push(self, name: str, value: float) -> None:
"""Send a new value to an existing time series.

Args:
name: Name of the time series.
value: New value for the series.

Note:
The difference between :func:`send` and :func:`push` happens when
the series was not added: :func:`send` will create it, while
:func:`push` will skip. Pushing is convenient when monitoring many
signals: you can push all of them in your program, and maintain a
separate list of those to plot when adding them.
"""
if name not in self.series:
return
return self.send(name, value)

def update(self) -> None:
"""Update plot with latest time-series values.

Expand Down
12 changes: 10 additions & 2 deletions tests/test_live_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,21 @@ def test_add_twice(self):
def test_send(self):
plot = self.make_test_plot()
plot.send("foo", 1.0)
self.assertFalse("foo" in plot.series)
plot.add_left("foo")
self.assertTrue("foo" in plot.series)
plot.send("foo", 2.0)
plot.send("foo", 3.0)
self.assertAlmostEqual(plot.series["foo"][-1], 3.0)

def test_push(self):
plot = self.make_test_plot()
plot.push("foo", 1.0)
self.assertFalse("foo" in plot.series)
plot.add_left("foo")
self.assertTrue("foo" in plot.series)
plot.push("foo", 2.0)
plot.push("foo", 3.0)
self.assertAlmostEqual(plot.series["foo"][-1], 3.0)

def test_update(self):
plot = self.make_test_plot()
plot.add_left("foo")
Expand Down
Loading