简体   繁体   中英

Scrolling/Zooming within a Plotwidget

i am playing around with a Plotwidget and i cant figure out how i can find the min-max positions from my list when zooming in and out.

Lets say i have a list which has 1000 values. When im scrolling/zooming i can only figure out the viewbox range and not the positions i am in my list.

Example:

  • On init i am at x[0], x[1000]
  • Scrolling once im at x[?], x[?]

Is it possible to find out the min-max index (if i am not directly on an index, whats the closest one)? I also would like to know, if it is possible to change the zooming factor when scrolling and can i set the viewbox padding to 0? I only figured out that u can change this with setXRange(..., padding=0)

import sys
from random import randint
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget
from PyQt5.QtCore import Qt
import pyqtgraph as pg

class Win(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.graph = pg.PlotWidget()
        self.setCentralWidget(self.graph)   
        self.graph.sigRangeChanged.connect(self.viewboxChanged)

        x = list()
        y = list()
        pos = 0
        for i in range(1000):
            r = randint(0,5)
            y.append(r)
            x.append(pos)
            pos +=r
        self.graph.plot(x, y)

    def viewboxChanged(self, view, range):
        print(range)
        pass


if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = Win()
    win.show()
    sys.exit( app.exec_() )

Your question is more like how to interpret the values in your range variable. Note: Consider changing that variable name, because it can be confusing because there is a python's built-in-function called like that.

Your range variable has this structure:

[ [x_0, x_1], [y_0, y_1] ]

Where:

  • x_0 and x_1 are the minimum and maximum values that are visible in the x-axis .
  • y_0 and y_1 are the minimum and maximum values that are visible in the y-axis .

Now, what you want are the indexes, and that can be done using this answer . Finally, the implementation of all of the above in your code will look like this:

class Win(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.graph = pg.PlotWidget()
        self.setCentralWidget(self.graph)   
        self.graph.sigRangeChanged.connect(self.viewboxChanged)
        ## Change the variables from local to an instance attribute
        ## to acces from the other functions
        self.x = list()
        self.y = list()
        pos = 0
        for i in range(1000):
            r = randint(0,5)
            self.y.append(r)
            self.x.append(pos)
            pos +=r
        self.graph.plot(self.x, self.y)

    def viewboxChanged(self, view, range_v):
        ## range_v[0] will return the list: [x_0, x_1]
        x0 = range_v[0][0]
        x1 = range_v[0][1]
        x0_index = min(self.x, key = lambda x: abs(x-x0))
        x1_index = min(self.x, key = lambda x: abs(x-x1))
        print(x0_index, x1_index)
        pass

There are comments inside explaining the changes I did.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM