![](/img/trans.png)
[英]Is there a way to set the limits on the y axis in python (Matplotlib)?
[英]Python pyqtgraph how to set x and y axis limits on graph, no autorange
我想知道如何設置為 pyqtgraph.GraphicsWindow.addPlot 對象顯示的 x 和 y 軸限制。 我需要在循環中顯示大量數據(因此使用 pyqtgraph),但我寧願預先分配我的軸,而不是允許自動范圍潛在地提高速度。 舉個例子,
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_()
問題在於p1.setXRange((5,20),padding=0)
。 這會導致錯誤:TypeError: setXRange() 需要至少 3 個參數(給出 3 個參數)
我認為這應該是一個非常簡單的問題,只需在繪圖前設置軸范圍。
正如錯誤消息所示,您沒有為setXRange()
提供正確數量的參數。 正確的行應如下所示:
p1.setXRange(5, 20, padding=0)
此處的文檔: http : //www.pyqtgraph.org/documentation/graphicsItems/viewbox.html#pyqtgraph.ViewBox.setXRange
邁克爾。
最簡單的方法是簡單地將范圍指定為列表的元素。 用這樣的東西替換p1.setXRange
:
p1.setRange(xRange=[5,20])
就是這樣!
檢查此類的文檔: http : //www.pyqtgraph.org/documentation/graphicsItems/viewbox.html#pyqtgraph.ViewBox.setRange
這些問題詢問如何設置對這一點的回答都沒有回答的觀點的限制。 對於遇到此問題的其他人,該圖的視圖可以限制為軸的一部分,如下所示:
p1.plotItem.vb.setLimits(xMin=a, xMax=b, yMin=c, yMax=d)
文檔: http : //www.pyqtgraph.org/documentation/graphicsItems/viewbox.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.