Allow log scale with 0 values

29 views
Skip to first unread message

Efrem Braun

unread,
Dec 3, 2020, 7:49:15 PM12/3/20
to pyqtgraph
I'd like to allow my software's users to view their data with a log scale. However, very often, their data includes 0 values (negative values should never occur). When the user right-clicks->Plot-Options->Transforms->Log-X, the pen disappears (see the minimal working example below, removing the pseudocode lines).

I'd like to put in a simple data filter such that when the user tries to put in a transform on the y-axis, I remove the <=0 points from the data that should be plotted. How can I attach a signal to the user performing right-clicks->Plot-Options->Transforms->Log-X such that I can change the data? Should be something like the following, but I don't know what the slot would be:

```
from PySide2 import QtGui
import pyqtgraph as pg
import numpy as np

x = np.array([1, 2, 3])
y = np.array([10, 0, 6])

app = QtGui.QApplication([])
plot = pg.PlotWidget()
line_ref = plot.plot(x, y)

# begin pseudocode
plot.logY_Slot.connect(update_log_plot)      # What should this be?
def update_log_plot():
    if plot.logY:                                                     # What should this be?
        line_ref.setData(x[y>0], y[y>0])
    else:
        line_ref.setData(x, y)
# end pseudocode

plot.show()
app.exec_()
```

Patrick

unread,
Dec 3, 2020, 9:05:25 PM12/3/20
to pyqtgraph
Hi,

Not sure the best way to do that, but maybe you could connect to one or more of the ViewBox signals and print some debug output and see if they trigger when you need. They may not, but might be worth giving a shot.

    sigYRangeChanged = QtCore.Signal(object, object)
    sigXRangeChanged = QtCore.Signal(object, object)
    sigRangeChangedManually = QtCore.Signal(object)
    sigRangeChanged = QtCore.Signal(object, object)
    sigStateChanged = QtCore.Signal(object)
    sigTransformChanged = QtCore.Signal(object)
    sigResized = QtCore.Signal(object)

Patrick

Efrem Braun

unread,
Dec 3, 2020, 11:41:30 PM12/3/20
to pyqt...@googlegroups.com
I tested these signals by taking the working example from before and adding:
```
plot.sigYRangeChanged.connect(lambda : print('hi 1'))
plot.sigXRangeChanged.connect(lambda : print('hi 2'))
plot.sigRangeChanged.connect(lambda : print('hi 3'))
plot.sigTransformChanged.connect(lambda : print('hi 4'))
```

The results are:
`sigYRangeChanged`: Signals if the user changes the y-axis
`sigXRangeChanged`: Signals if the user changes the x-axis
`sigRangeChanged`: Signals if the user changes either axis
`sigTransformChanged`: I don't know; I did everything to the plot, but this never signaled for me. I see that this signal is emitted in https://github.com/pyqtgraph/pyqtgraph/blob/master/pyqtgraph/widgets/GraphicsView.py when updateMatrix() is called, but I'm not sure when that occurs.

`plot.plotItem.ctrl.logYCheck.toggled.connect(lambda : print('hi5'))`
sends a signal when the user toggles that checkbox.

So my final code that gets everything working is:
```
from PySide2 import QtGui
import pyqtgraph as pg
import numpy as np

x = np.array([1, 2, 3])
y = np.array([10, 0, 6])

app = QtGui.QApplication([])
plot = pg.PlotWidget()
line_ref = plot.plot(x, y)

def update_log_plot():
    if plot.plotItem.ctrl.logYCheck.isChecked():

        line_ref.setData(x[y>0], y[y>0])
    else:
        line_ref.setData(x, y)
plot.plotItem.ctrl.logYCheck.toggled.connect(update_log_plot)

plot.show()
app.exec_()
```

Thanks Patrick!

Efrem Braun


--
You received this message because you are subscribed to a topic in the Google Groups "pyqtgraph" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pyqtgraph/pKs_VZ25WOs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pyqtgraph+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/8fd9abd6-6a08-4265-a7df-ad66b2d4ca9dn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages