relim of matplotlib axes (embedded in pyqt) doesn't work is I previously zoomed in

relim of matplotlib axes (embedded in pyqt)doesn't work is I previously zoomed in

在下面的示例中,我更新了具有不同信号长度的图形。除非我放大图形,否则 relim 函数可以正常工作。如果我放大,relim 不会重置正确的限制并且轴保持放大,无论我绘制的信号长度如何。我不知道我在这里做错了什么。

from PyQt5.QtWidgets import *
import sys
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
import numpy as np

class SurfViewer(QMainWindow):
    def __init__(self, parent=None):
        super(SurfViewer, self).__init__()
        self.parent = parent
        self.centralWidget = QWidget()
        self.setCentralWidget(self.centralWidget)
        self.setFixedWidth(600)
        self.setFixedHeight(600)
        self.layout = QVBoxLayout()
        self.button = QPushButton('redraw')
        self.myscene= Viewer_signal(self)
        self.layout.addWidget(self.button)
        self.layout.addWidget(self.myscene)
        self.centralWidget.setLayout(self.layout)

        self.button.clicked.connect(self.load)

    def load(self):
        self.myscene.update_Signal(np.random.rand(np.random.randint(1000,100000)),1000)


class Viewer_signal(QGraphicsView):
    def __init__(self, parent=None):
        super(Viewer_signal, self).__init__(parent)
        self.parent=parent
        self.scene = QGraphicsScene(self)
        self.setScene(self.scene)
        self.figure = Figure(facecolor='white')
        self.figure.subplots_adjust(left=0.08, bottom=0.03, right=0.98, top=0.99, wspace=0.28 , hspace=0.30)
        self.canvas = FigureCanvas(self.figure)
        self.toolbar = NavigationToolbar(self.canvas, self)

        self.axes_Sig = self.figure.add_subplot(111)
        self.axes_Sig.set_xlabel("Time [s]")
        self.axes_Sig.set_ylabel("Amp [mV]")
        self.Line_,= self.axes_Sig.plot(0,0 )

        self.canvas.setGeometry(0, 0, 1500, 500)
        layout = QVBoxLayout()
        layout.addWidget(self.toolbar)
        layout.addWidget(self.canvas)
        self.setLayout(layout)
        self.figure.tight_layout()

    def update_Signal(self,data,Fs):
        t = np.arange(data.shape[0])/Fs
        self.Line_.set_data(t, data)
        self.axes_Sig.relim()
        self.axes_Sig.autoscale_view(True,True,True)
        self.canvas.draw()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = SurfViewer(app)
    ex.setWindowTitle('window')
    ex.show()
    sys.exit(app.exec_( ))

更新

即使通过 和 设置限制也无济于事:

def update_Signal(self,data,Fs):
        t = np.arange(data.shape[0])/Fs
        self.Line_.set_data(t, data)

        miniy = min(data)
        maxiy = max(data)
        self.axes_Sig.update_datalim( ((0, miniy), (t[-1], maxiy)))

        self.axes_Sig.relim()
        self.axes_Sig.autoscale_view(True,True,True)
        self.canvas.draw()

我想你想使用 autoscale() 而不是 autoscale_view()

def update_Signal(self,data,Fs):
    t = np.arange(data.shape[0])/Fs
    self.Line_.set_data(t, data)
    self.axes_Sig.relim()
    self.axes_Sig.autoscale()
    self.canvas.draw()