简体   繁体   English

Python pyqtgraph 如何在图形上设置 x 和 y 轴限制,没有自动调整范围

[英]Python pyqtgraph how to set x and y axis limits on graph, no autorange

I would like to know how I can set the x and y axis limits that are displayed for a pyqtgraph.GraphicsWindow.addPlot object.我想知道如何设置为 pyqtgraph.GraphicsWindow.addPlot 对象显示的 x 和 y 轴限制。 I need to display a lot of data inside a loop (hence using pyqtgraph) but I would rather preallocate my axes as opposed to allowing autorange to potentially enhance speed.我需要在循环中显示大量数据(因此使用 pyqtgraph),但我宁愿预先分配我的轴,而不是允许自动范围潜在地提高速度。 As an example,举个例子,

from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pyqtgraph as pg
app = QtGui.QApplication([])
win = pg.GraphicsWindow(title="My plotting examples")
win.resize(1000,600)
win.setWindowTitle('pyqtgraph example: Plotting')
p1 = win.addPlot(title="plot1")
p2 = win.addPlot(title="plot2")
curve1 = p1.plot(pen='y')
curve2 = p1.plot(pen='r')
curve3 = p2.plot(pen='b')
x = np.linspace(0,10,1000)
x_current = x[0]
p1.setXRange((5,20), padding=0)
for i in range(1,len(x)):
    x_current = np.append(x_current,x[i])
    curve1.setData(x_current,np.sin(x_current))
    curve2.setData(x_current,np.cos(x_current))
    curve3.setData(x_current,np.tan(x_current))
    app.processEvents()

if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()

The problem lies in the line p1.setXRange((5,20),padding=0) .问题在于p1.setXRange((5,20),padding=0) This results in the error: TypeError: setXRange() takes at least 3 arguments (3 given)这会导致错误:TypeError: setXRange() 需要至少 3 个参数(给出 3 个参数)

I think this should be a very simple question, just setting the axis ranges before plotting.我认为这应该是一个非常简单的问题,只需在绘图前设置轴范围。

As the error message indicates, you have not supplied the correct number of arguments to setXRange() .正如错误消息所示,您没有为setXRange()提供正确数量的参数。 The correct line should look like:正确的行应如下所示:

p1.setXRange(5, 20, padding=0)

Documentation here: http://www.pyqtgraph.org/documentation/graphicsItems/viewbox.html#pyqtgraph.ViewBox.setXRange此处的文档: http : //www.pyqtgraph.org/documentation/graphicsItems/viewbox.html#pyqtgraph.ViewBox.setXRange

Michael.迈克尔。

The easiest way would be to simply specify your ranges as elements of a list.最简单的方法是简单地将范围指定为列表的元素。 Replace the p1.setXRange with something like this:用这样的东西替换p1.setXRange

p1.setRange(xRange=[5,20])

And that's it!就是这样!

Check the documentation of this class in: http://www.pyqtgraph.org/documentation/graphicsItems/viewbox.html#pyqtgraph.ViewBox.setRange检查此类的文档: http : //www.pyqtgraph.org/documentation/graphicsItems/viewbox.html#pyqtgraph.ViewBox.setRange

The questions asks how to set a limit to the view which none of the responses to this point answers.这些问题询问如何设置对这一点的回答都没有回答的观点的限制。 For others who come across this the view of the plot can be limited to a section of the axis like this:对于遇到此问题的其他人,该图的视图可以限制为轴的一部分,如下所示:

p1.plotItem.vb.setLimits(xMin=a, xMax=b, yMin=c, yMax=d)

Documentation: http://www.pyqtgraph.org/documentation/graphicsItems/viewbox.html文档: http : //www.pyqtgraph.org/documentation/graphicsItems/viewbox.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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