PlotPyStack / guiqwt

Efficient 2D plotting Python library based on PythonQwt
https://pypi.python.org/pypi/guiqwt
Other
80 stars 20 forks source link

Unable to select range in CurveWidget #82

Closed qusuda closed 2 years ago

qusuda commented 5 years ago

When embedding CurveWidget in to a QT window i am not able to select ranges.

If initiated as standalone dialog it work as expected

from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout
from guiqwt.plot import CurveWidget
from guiqwt.builder import make
import numpy as np

app = QApplication([])
window = QWidget()
layout = QVBoxLayout()
cw = CurveWidget()
cw.parent = layout
layout.addWidget(cw)
window.setLayout(layout)

x = np.zeros(500)
y = np.zeros(len(x))

for i in range(0, 500):
    x[i] = i
    y[i] = np.random.randint(500)

curve = make.curve(x, y, "test")
range1 = make.range(x[200], x[300])
disp0 = make.range_info_label(range1, 'TR', "x = %.1f + %.1f ms", lambda x, dx: ((x-dx), (2*dx)), title="Selected range")

disp2 = make.computations(range1, "TL",
                            [
                            (curve, "min=%.3f mA", lambda x, y: y.min()),
                            (curve, "max=%.3f mA", lambda x, y: y.max()),
                            (curve, "avg=%.3f mA", lambda x, y: y.mean())
                            ],
                            title="Measurements")
cw.plot.add_item(curve)
cw.plot.add_item(disp0)
cw.plot.add_item(disp2)
cw.plot.add_item(range1)
window.show()
app.exec_()
PierreRaybaut commented 2 years ago

Just add:

cw.register_all_curve_tools()

after cw = CurveWidget().

And add curve item specifying z to ensure it's in background:

cw.plot.add_item(curve, z=-1)

So, the full working code is the following:

from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout
from guiqwt.plot import CurveWidget
from guiqwt.builder import make
import numpy as np

app = QApplication([])
window = QWidget()
layout = QVBoxLayout()
cw = CurveWidget()
cw.register_all_curve_tools()
cw.parent = layout
layout.addWidget(cw)
window.setLayout(layout)

x = np.zeros(500)
y = np.zeros(len(x))

for i in range(0, 500):
    x[i] = i
    y[i] = np.random.randint(500)

curve = make.curve(x, y, "test")
range1 = make.range(x[200], x[300])
disp0 = make.range_info_label(
    range1,
    "TR",
    "x = %.1f + %.1f ms",
    lambda x, dx: ((x - dx), (2 * dx)),
    title="Selected range",
)

disp2 = make.computations(
    range1,
    "TL",
    [
        (curve, "min=%.3f mA", lambda x, y: y.min()),
        (curve, "max=%.3f mA", lambda x, y: y.max()),
        (curve, "avg=%.3f mA", lambda x, y: y.mean()),
    ],
    title="Measurements",
)
cw.plot.add_item(curve, z=-1)
cw.plot.add_item(disp0)
cw.plot.add_item(disp2)
cw.plot.add_item(range1)
window.show()
app.exec_()