Repository: pyqtgraph/pyqtgraph

Issue:
Invalid data incorrectly identified/drawn
### Short description
As per discussion at https://groups.google.com/g/pyqtgraph/c/5TMt_vC-lUI, when I draw a line graph that contains invalid points, the valid points either side of the invalid region are sometimes connected by a straight line.  I would expect that the line should stop before the invalid region and start again afterwards.

### Code to reproduce
```python
import sys
from PySide6 import QtWidgets, QtCore
import pyqtgraph as pg
import logging


class SENSORDemo(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()

        # central widget
        central_widget = QtWidgets.QWidget()

        # create widgets
        self.update_timer = QtCore.QTimer()

        plot_box = QtWidgets.QGroupBox()
        plot_box_layout = QtWidgets.QGridLayout()
        self.halt_graph_update = QtWidgets.QCheckBox("Update graphs")
        self.halt_graph_update.setChecked(True)

        pg.setConfigOption("background", "w")
        pg.setConfigOption("foreground", "k")
        pos_plot_widget = pg.GraphicsLayoutWidget()
        vel_plot_widget = pg.GraphicsLayoutWidget()
        pos_plot = pos_plot_widget.addPlot(title="P")
        vel_plot = vel_plot_widget.addPlot(title="V")
        # pos_plot = self.plot.addPlot(row=0, col=0)
        # vel_plot = self.plot.addPlot(row=0, col=1)
        pos_plot.getViewBox().setMouseMode(pg.ViewBox.RectMode)
        pos_plot.getViewBox().setMouseEnabled(x=False, y=True)
        pos_plot.getViewBox().setLimits(xMin=0, xMax=1024, minXRange=1024)
        pos_plot.getAxis("left").setStyle(
            autoExpandTextSpace=True, autoReduceTextSpace=False
        )
        vel_plot.getViewBox().setMouseMode(pg.ViewBox.RectMode)
        vel_plot.getViewBox().setMouseEnabled(x=False, y=True)
        vel_plot.getViewBox().setLimits(xMin=0, xMax=1024, minXRange=1024)
        vel_plot.getAxis("left").setStyle(
            autoExpandTextSpace=True, autoReduceTextSpace=False
        )
        self.pos_plot_line = pos_plot.plot()
        self.vel_plot_line = vel_plot.plot()
        plot_box_layout.addWidget(
            self.halt_graph_update, 0, 0, 1, 2, QtCore.Qt.AlignCenter
        )
        plot_box_layout.addWidget(pos_plot_widget, 2, 0, 1, 1)
        plot_box_layout.addWidget(vel_plot_widget, 2, 1, 1, 1)
        plot_box.setLayout(plot_box_layout)

        # layout widgets
        central_layout = QtWidgets.QGridLayout()
        central_layout.addWidget(plot_box, 4, 0, 1, 2)
        central_widget.setLayout(central_layout)
        self.setCentralWidget(central_widget)

        # connect signals/slots
        self.update_timer.timeout.connect(self.update_position)
        self.update_timer.start(1000)

    @QtCore.Slot()
    def update_position(self):
        if self.halt_graph_update.isChecked():
            samples = [x % 10 if x < 100 or x > 150 else float("nan") for x in range(1024)]

            self.pos_plot_line.setData(
                [x for x in samples],
                pen=(0, 0, 255),
            )
            self.vel_plot_line.setData(
                [x for x in samples],
                pen=(0, 0, 255),
            )


if __name__ == "__main__":
    logging.basicConfig(level=logging.DEBUG)

    app = QtWidgets.QApplication(sys.argv)
    app.setApplicationName("Read Sensor")

    sensordemo = SENSORDemo()
    sensordemo.resize(800, 600)
    sensordemo.show()

    sys.exit(app.exec())
```

### Expected behavior
Invalid region should have no line drawn across it

### Real behavior
A line is drawn across the invalid region of the graph (between x=100 and x=150). 
![image](https://user-images.githubusercontent.com/2292983/193595754-6b0339b1-5bc2-4bf8-8ba4-8309bf125b1f.png)
Changing the zoom level using the mouse causes the graph to redraw correctly (until the next call to `setData()`).

### Tested environment(s)

 * PyQtGraph version: 0.12.4
 * Qt Python binding: PySide6 6.3.1 Qt 6.3.1
 * Python version: 3.10.4
 * NumPy version: 1.22.4
 * Operating system: Windows 11
 * Installation method: pip

### Additional context



Discussion:
@alanbowman Try passing `connect="finite"` in both calls to `setData`: https://pyqtgraph.readthedocs.io/en/latest/api_reference/graphicsItems/plotcurveitem.html#pyqtgraph.PlotCurveItem.setData

Closing for now, but if you still have issues feel free to leave a followup comment.
My apologies, I didn't see the full linked discussion. I was unable to reproduce on my end but I can continue to investigate
Sorry, maybe I didn't summarise it very well.  I see from the link that the default value of connect is "all", but I was sure I'd read it as "auto" - I'm trying to re-read to understand it better.  Whichever it is, the current behaviour seems inconsistent, as sometimes the line is drawn and sometimes it isn't.
No problem -- the default is indeed `all`, attempting to connect all points. Explicitly passing `connect="finite"` should result in the behavior you're looking for. Can you let me know if the issue persists in that case?

Also, can you let me know which version of PySide6 is used in your issue?
Ah - I'd been reading the description for connect in PlotDataItem rather than PlotCurveItem, which describes the default as auto.

Explicitly setting `connect="finite"` does give the behaviour I'm after.  Does that mean that the default behaviour is not working correctly, because the line isn't always joined in that case?

I believe it's PySide 6.3.1.
Indeed -- there is a discrepancy between the docs cases, and `auto` should be able to (but doesn't in this case) detect finite-ness of values per your issue. Thanks for your patience to explain the details.
No problem, sorry it took a few attempts to explain.

Which should be the correct default?  While I know what to put to make it explicit, what should I expect if I wasn't to pass the connect parameter?
`auto` should work per your original intuition. My confusion was from looking at the documentation for `PlotCurveItem` instead of `PlotDataItem` as you noted. Thus, you should expect it to detect finite values correctly
The following snippet triggers the bug for me. The initial plot is all connected. On zoom, it becomes disconnected.
```python
import math
import pyqtgraph as pg

pg.mkQApp()
pw = pg.PlotWidget()
pw.show()
data = [0, 2, 1, math.nan, 1, 2, 0]
pdi = pw.plot(data)
pdi.setData(data)
pg.exec()
```

Resolving patch:
diff --git a/pyqtgraph/graphicsItems/PlotDataItem.py b/pyqtgraph/graphicsItems/PlotDataItem.py
--- a/pyqtgraph/graphicsItems/PlotDataItem.py
+++ b/pyqtgraph/graphicsItems/PlotDataItem.py
@@ -271,8 +271,8 @@ def __init__(self, *args, **kargs):
                               values exist, unpredictable behavior will occur. The data may not be
                               displayed or the plot may take a significant performance hit.
                               
-                              In the default 'auto' connect mode, `PlotDataItem` will apply this 
-                              setting automatically.
+                              In the default 'auto' connect mode, `PlotDataItem` will automatically
+                              override this setting.
             ================= =======================================================================
 
         **Meta-info keyword arguments:**
@@ -877,14 +877,17 @@ def updateItems(self, styleUpdate=True):
             ): # draw if visible...
             # auto-switch to indicate non-finite values as interruptions in the curve:
             if isinstance(curveArgs['connect'], str) and curveArgs['connect'] == 'auto': # connect can also take a boolean array
-                if dataset.containsNonfinite is None:
-                    curveArgs['connect'] = 'all' # this is faster, but silently connects the curve across any non-finite values
-                else:
-                    if dataset.containsNonfinite:
-                        curveArgs['connect'] = 'finite'
-                    else:
-                        curveArgs['connect'] = 'all' # all points can be connected, and no further check is needed.
-                        curveArgs['skipFiniteCheck'] = True
+                if dataset.containsNonfinite is False:
+                    # all points can be connected, and no further check is needed.
+                    curveArgs['connect'] = 'all'
+                    curveArgs['skipFiniteCheck'] = True
+                else:   # True or None
+                    # True: (we checked and found non-finites)
+                    #   don't connect non-finites
+                    # None: (we haven't performed a check for non-finites yet)
+                    #   use connect='finite' in case there are non-finites.
+                    curveArgs['connect'] = 'finite'
+                    curveArgs['skipFiniteCheck'] = False
             self.curve.setData(x=x, y=y, **curveArgs)
             self.curve.show()
         else: # ...hide if not.


Test patch: