背景:
问题:
当我使用NavigationToolbar2QT放大绘图时,我使用哪些matplotlib对象/方法来识别绘图当前显示的数据子集?
我认为 get_data_interval()会起作用,但我无法确定如何返回我的绘图的X / YAxis对象(因为我使用了figure.add_subplot(111))?
from PyQt5 import QtWidgets
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
import matplotlib.widgets as mwidgets
import numpy as np
class PlotWin(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(PlotWin, self).__init__(parent)
QtWidgets.QMainWindow.__init__(self, parent)
self.win = QtWidgets.QWidget(self)
self.setCentralWidget(self.win)
layout = QtWidgets.QVBoxLayout(self.win)
self.canvas = FigureCanvas(Figure())
layout.addWidget(self.canvas)
self.addToolBar(NavigationToolbar(self.canvas, self))
self.ax1 = self.canvas.figure.add_subplot(111)
self.x = np.linspace(1, 100, 100)
self.y = np.random.rand(100, 1)
self.ax1.plot(self.x, self.y)
self.span = mwidgets.SpanSelector(self.ax1, self.on_select, 'horizontal', useblit=True,
rectprops=dict(alpha=0.5, facecolor='red'), button=[1])
def on_select(self, xmin, xmax):
indmin, indmax = np.searchsorted(self.x, (xmin, xmax))
indmax = min(len(self.x) - 1, indmax)
sel_x = self.x[indmin:indmax]
sel_y = self.y[indmin:indmax]
self.ax1.plot(sel_x, sel_y, "o")
self.canvas.draw()
if __name__ == '__main__':
import sys
if not QtWidgets.QApplication.instance():
app = QtWidgets.QApplication(sys.argv)
else:
app = QtWidgets.QApplication.instance()
window = PlotWin()
window.show()
app.exec_()