pyqtgraph 限制缩放到轴的上限/下限 [英] pyqtgraph limit zoom to upper/lower bound of axes
问题描述
所以 Pyqtgraph 会自动计算轴并在缩放时重新缩放,这很好.但是我有两个轴,频率和小时.频率可以采用 0-100 之间的任何值,小时可以采用 0-39 之间的任何值.如何将轴限制在这些上限/下限,以便用户缩放或平移时不能超出这些值?
so Pyqtgraph automatically computes the axis and rescales upon zooming, and this is fine. However I have two axes, frequency and hour. Frequency can take any value between 0-100 and hour can take any value between 0-39. How can I limit the axis to these upper/lower bounds so that when the user zooms or pans they cannot go outside of these values?
我的代码如下(对于 3 行,我的实际代码会绘制更多):
My code is as follows (for 3 lines, my actual code will plot a lot more):
from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pyqtgraph as pg
pg.setConfigOption('background', 'w')
pg.setConfigOption('foreground', 'k')
from random import randint
class CustomViewBox(pg.ViewBox):
def __init__(self, *args, **kwds):
pg.ViewBox.__init__(self, *args, **kwds)
self.setMouseMode(self.RectMode)
## reimplement right-click to zoom out
def mouseClickEvent(self, ev):
if ev.button() == QtCore.Qt.RightButton:
#self.autoRange()
self.setXRange(0,5)
self.setYRange(0,10)
def mouseDragEvent(self, ev):
if ev.button() == QtCore.Qt.RightButton:
ev.ignore()
else:
pg.ViewBox.mouseDragEvent(self, ev)
app = pg.mkQApp()
vb = CustomViewBox()
graph = pg.PlotWidget(viewBox=vb, enableMenu=False)
colour = []
for i in range(0,3):
colourvalue = [randint(0,255), randint(0,255), randint(0,255)]
tuple(colourvalue)
colour.append(colourvalue)
y_data = [
[['a',0],['b',1],['c',None],['d',6],['e',7]],
[['a',5],['b',2],['c',1],['d',None],['e',1]],
[['a',3],['b',None],['c',4],['d',9],['e',None]],
]
x_data = [0, 1, 2, 3, 4]
for i in range(3):
xv = []
yv = []
for j, v in enumerate(row[i][1] for row in y_data):
if v is not None:
xv.append(int(j))
yv.append(float(v))
graph.plot(xv, yv, pen = colour[i], name=y_data[0][i][0])
graph.show()
graph.setWindowTitle('Hourly Frequency Graph')
graph.setXRange(0,5)
graph.setYRange(0,10)
graph.setLabel('left', "Frequency", units='%')
graph.setLabel('bottom', "Hour")
graph.showGrid(x=True, y=True)
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
预先感谢您的任何帮助和建议!
Thanks in advance for any help and advice!
推荐答案
我知道这是一个旧方案,但以防万一其他人需要解决方案.通过获取当前轴(假设为最大缩小)并将这些值设置为视图框的范围,可以阻止用户缩小超过轴限制.
I know this is an old one but just in case anyone else needs a solution. The user can be stopped from zooming out past the axis limits by getting the current axis (assumed to be the maximum out-zoom) and setting these values as the range for the view box.
例如
range_ = graph.getViewBox().viewRange()
graph.getViewBox().setLimits(xMin=range_[0][0], xMax=range_[0][1],
yMin=range_[1][0], yMax=range_[1][1])
文档:https://pyqtgraph.readthedocs.io/en/latest/graphicsItems/viewbox.html
这篇关于pyqtgraph 限制缩放到轴的上限/下限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!