class CustomLegendItem(pg.LegendItem):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.sampleType = DIXBIPItemSample
class CustomScatterPlotItem(pg.ScatterPlotItem):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.error_item = kwargs.pop("error_item",
None)
class CustomErrorBarItem(pg.ErrorBarItem):
def __init__(self, **opts):
super().__init__(**opts)
self.path = None
def setData(self, **opts):
self.opts.update(opts)
if self.isVisible():
self.setVisible(all(self.opts[ax] is not None for ax in ['x', 'y']))
else:
self.setVisible(not all(self.opts[ax] is not None for ax
in ['x', 'y']))
self.path = None
self.update()
self.prepareGeometryChange()
self.informViewBoundsChanged()
class CustomItemSample(pg.ItemSample):
def __init__(self, *args, **kwargs):
ItemSample.__init__(self, *args, **kwargs)
def mouseClickEvent(self, event):
if event.button() == Qt.MouseButton.LeftButton:
visible = self.item.isVisible()
self.item.setVisible(not visible)
if isinstance(self.item.error_item, CustomErrorBarItem):
self.item.error_item.setVisible(not visible)
event.accept()
self.update()
I can now use a custom class for the legend, scatter plot, and error bar when adding to a plot widget. This is accomplished by passing a custom error bar item (error_item) to a custom scatter plot item as an argument.
Here is the error I receive using my fix:
Traceback (most recent call last):
File "C:\Python310\lib\site-packages\pyqtgraph\graphicsItems\ErrorBarItem.py", line 155, in boundingRect
self.drawPath()
File "C:\Python310\lib\site-packages\pyqtgraph\graphicsItems\ErrorBarItem.py", line 87, in drawPath
verticalLines = fn.arrayToQPath(xs, y1_y2, connect="pairs")
File "C:\Python310\lib\site-packages\pyqtgraph\functions.py", line 2147, in arrayToQPath
isfinite = np.isfinite(x) & np.isfinite(y)
ValueError: operands could not be broadcast together with shapes (92,) (94,)
A lot of times, a single error like the one above occurs multiple times in succession which causes the error bars to flicker in the plot (same error with shapes (92,) (94,) for example). It eventually stops but occurs later at a random data point. Even though this error occurs, I can still toggle the data points and error bars using the legend.