Hello! I'm a chemist trying to write some software for my lab. I'm still 
new to coding. The software I'm writing plots unit cell parameters for a 
large amount of data as a scatter plot with each individual data containing 
error bars using pyqtgraph 0.12.4, Pyside6 6.2.3, and pandas 1.4.1. The 
problem I'm having is when I click on the legend to hide a particular 
scatter plot, the error bars are left on the plot. I'm not sure if this is 
a bug, but it sure is annoying! I managed to create a fix; however, that 
fix seems to produce another error that corrects itself but is super 
annoying! Here is my fix by subclassing a few pyqtgraph items:

*import *pyqtgraph *as *pg

*class Custom**LegendItem**(*pg.LegendItem*)**:*

    *def **__init__**(*self, ***args, ****kwargs*)*
*:         *super*()*.__init__*(****args, ****kwargs
*)         *self.sampleType *= *DIXBIPItemSample

*class Custom**ScatterPlotItem**(*pg.ScatterPlotItem*)*
*:     **def **__init__**(*self, ***args, ****kwargs*)*
*:         *super*()*.__init__*(****args, ****kwargs
*)         *self.error_item *= *kwargs.pop*(**"error_item"*, *None*

*) **class Custom**ErrorBarItem**(*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 Custom**ItemSample**(*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.

Is there a better way to add error bars to a scatter plot and be able to 
toggle their visibility through the legend? If not, is there anyway to 
update my fix to avoid that error?

Thanks for any and all help!






-- 
You received this message because you are subscribed to the Google Groups 
"pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/pyqtgraph/fd28fdaf-bb2f-479f-a1d7-22fb3d25254cn%40googlegroups.com.

Reply via email to