PyQtGraph library providing thread-safe plot curves with underlying (ring) buffers.
Project description
DvG_PyQtGraph_ThreadSafe
PyQtGraph library providing thread-safe plot curves with underlying (ring) buffers.
Installation:
pip install dvg-pyqtgraph-threadsafe
Classes HistoryChartCurve, BufferedPlotCurve & PlotCurve wrap around a pyqtgraph.PlotDataItem instance, called a curve for convenience. Data can be safely appended or set from out of any thread.
The (x, y)-curve data is buffered internally to the class, relying on either a circular/ring buffer or a regular array buffer:
- HistoryChartCurve
Ring buffer. The plotted x-data will be shifted such that the right-side is always set to 0. I.e., when x denotes time, the data is plotted backwards in time, hence the name history chart. The most recent data is on the right-side of the ring buffer.
- BufferedPlotCurve
Ring buffer. Data will be plotted as is. Can also act as a Lissajous figure.
- PlotCurve
Regular array buffer. Data will be plotted as is.
Usage:
import sys from PyQt5 import QtWidgets import pyqtgraph as pg from dvg_pyqtgraph_threadsafe import HistoryChartCurve class MainWindow(QtWidgets.QWidget): def __init__(self, parent=None, **kwargs): super().__init__(parent, **kwargs) self.gw = pg.GraphicsLayoutWidget() self.plot_1 = self.gw.addPlot() # Create a HistoryChartCurve and have it wrap around a new PlotDataItem # as set by argument `linked_curve`. self.tscurve_1 = HistoryChartCurve( capacity=5, linked_curve=self.plot_1.plot(pen=pg.mkPen('r')), ) grid = QtWidgets.QGridLayout(self) grid.addWidget(self.gw) app = QtWidgets.QApplication(sys.argv) window = MainWindow() # The following line could have been executed from inside of another thread: window.tscurve_1.extendData([1, 2, 3, 4, 5], [10, 20, 30, 20, 10]) # Draw the curve from out of the main thread window.tscurve_1.update() window.show() sys.exit(app.exec_())
API
class ThreadSafeCurve(capacity: int, linked_curve: pg.PlotDataItem, shift_right_x_to_zero: bool = False, use_ringbuffer: bool = True)
Provides the base class for a thread-safe plot curve to which (x, y)-data can be safely appended or set from out of any thread. It will wrap around the passed argument linked_curve of type pyqtgraph.PlotDataItem and will manage the (x, y)-data buffers underlying the curve.
Intended multi-threaded operation: One or more threads push new data into the ThreadSafeCurve-buffers. Another thread performs the GUI refresh by calling update() which will redraw the curve according to the current buffer contents.
- Args:
- capacity (int):
Maximum number of (x, y)-data points the buffer can store.
- linked_curve (pyqtgraph.PlotDataItem):
Instance of pyqtgraph.PlotDataItem to plot the buffered data out into.
- shift_right_x_to_zero (bool, optional):
When plotting, should the x-data be shifted such that the right-side is always set to 0? Useful for history charts.
Default: False
- use_ringbuffer (bool, optional):
When True, the (x, y)-data buffers are each a ring buffer. New readings are placed at the end (right-side) of the buffer, pushing out the oldest readings when the buffer has reached its maximum capacity (FIFO). Use methods appendData() and extendData() to push in new data.
When False, the (x, y)-data buffers are each a regular array buffer. Use method setData() to set the data.
Default: True
- Attributes:
- x_axis_divisor (float):
The x-data in the buffer will be divided by this factor when the plot curve is drawn. Useful to, e.g., transform the x-axis units from milliseconds to seconds or minutes.
Default: 1
- y_axis_divisor (float):
Same functionality as x_axis_divisor.
Default: 1
Methods
- appendData(x, y)
Append a single (x, y)-data point to the ring buffer.
- extendData(x_list, y_list)
Extend the ring buffer with a list of (x, y)-data points.
- setData(x_list, y_list)
Set the (x, y)-data of the regular array buffer.
- update(create_snapshot: bool = True)
Update the data behind the curve by creating a snapshot of the current contents of the buffer, and redraw the curve on screen.
You can suppress updating the data behind the curve by setting parameter create_snapshot to False. The curve will then only be redrawn based on the old data. This is useful when the plot is paused.
- clear()
Clear the contents of the curve and redraw.
- name()
Get the name of the curve.
isVisible() -> bool
setVisible(state: bool = True)
- setDownsampling(*args, **kwargs)
All arguments will be passed onto method pyqtgraph.PlotDataItem.setDownsampling() of the underlying curve.
Properties
- size -> Tuple[int, int]:
Number of elements currently contained in the underlying (x, y)- buffers of the curve. Note that this is not necessarily the number of elements of the currently drawn curve. Instead, it reflects the current sizes of the data buffers behind it that will be drawn onto screen by the next call to update().
class HistoryChartCurve(capacity: int, linked_curve: pg.PlotDataItem)
Bases: ThreadSafeCurve
Provides a thread-safe curve with underlying ring buffers for the (x, y)-data. New readings are placed at the end (right-side) of the buffer, pushing out the oldest readings when the buffer has reached its maximum capacity (FIFO). Use methods appendData() and extendData() to push in new data.
The plotted x-data will be shifted such that the right-side is always set to 0. I.e., when x denotes time, the data is plotted backwards in time, hence the name history chart.
See class ThreadSafeCurve for more details.
class BufferedPlotCurve(capacity: int, linked_curve: pg.PlotDataItem)
Bases: ThreadSafeCurve
Provides a thread-safe curve with underlying ring buffers for the (x, y)-data. New readings are placed at the end (right-side) of the buffer, pushing out the oldest readings when the buffer has reached its maximum capacity (FIFO). Use methods appendData() and extendData() to push in new data.
See class ThreadSafeCurve for more details.
class PlotCurve(capacity: int, linked_curve: pg.PlotDataItem)
Bases: ThreadSafeCurve
Provides a thread-safe curve with underlying regular array buffers for the (x, y)-data. Use method setData() to set the data.
See class ThreadSafeCurve for more details.
Changelog
3.0.0 (2020-08-07)
Renamed parameter LegendSelect.curves to LegendSelect.linked_curves
Changed base of class LegendSelect() from QWidget to QObject
Added class PlotManager()
2.0.1 (2020-08-03)
Workaround: PyQt5 >= 5.12.3 causes a bug in PyQtGraph where a curve won’t render if it contains NaNs (but only in the case when OpenGL is disabled). The curve will now be displayed correctly, i.e., fragmented whenever a NaN is encountered. When OpenGL is enabled, linear interpolation will occur at the gaps as per pyqtgraph.plotCurveItem.paintGL().
2.0.0 (2020-08-02)
Method names are now conform the PyQtGraph naming style. I.e. setData() vs. set_data(), etc.
The default values of PyQtGraph are no longer being overwritten.
Added class LegendSelect
1.0.0 (2020-07-30)
First release on PyPI
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for dvg-pyqtgraph-threadsafe-3.0.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | d9518c820d4b3c2aa4b90accd595ea6a411356d9babe6f6e0556e9dcaf0f276e |
|
MD5 | a28a8cffcfe30a12d8abd8eb9d25a728 |
|
BLAKE2b-256 | 2164054b9d32031eecb590eb0c1d57686185a520f391e604ab740bd09969e98c |
Hashes for dvg_pyqtgraph_threadsafe-3.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b7a1c45b432acd46892d1b7756b5daebe53417469452f14bc5f589d279305a1e |
|
MD5 | 5dead64d418f9942a8daf45a55a7effe |
|
BLAKE2b-256 | 045a184c7e5108e77935b3861d2a61274858365ea64ba481d586eae450d20f18 |