如何使用鼠标滚动应用放大/缩小?

时间:2020-08-28 10:46:54

标签: python matplotlib

我正在尝试使用以下代码通过鼠标滚轮实现放大/缩小的功能:Matplotlib plot zooming with scroll wheelzoom_factory文件中有zoom.py) 不幸的是,它不能像我下面那样工作。我没有太多经验,我不知道该怎么做。你能帮忙吗?

import sys
from PyQt5.QtWidgets import QDialog, QApplication, QPushButton, QVBoxLayout 
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas 
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar 
import matplotlib.pyplot as plt
import pandas as pd

from datetime import datetime

from zoom import zoom_factory

class Window(QDialog):

    def __init__(self):
        super().__init__()

        title = "Graphs"

        self.setWindowTitle(title)

        # a figure instance to plot on 
        self.figure = plt.figure() 
   
        # this is the Canvas Widget that  
        # displays the 'figure'it takes the 
        # 'figure' instance as a parameter to __init__ 
        self.canvas = FigureCanvas(self.figure) 
   
        # this is the Navigation widget 
        # it takes the Canvas widget and a parent 
        self.toolbar = NavigationToolbar(self.canvas, self) 
   
        # Just some button connected to 'plot' method 
        self.button = QPushButton('Plot') 
           
        # adding action to the button 
        self.button.clicked.connect(self.plot) 
   
        # creating a Vertical Box layout 
        layout = QVBoxLayout() 
           
        # adding tool bar to the layout 
        layout.addWidget(self.toolbar) 
           
        # adding canvas to the layout 
        layout.addWidget(self.canvas) 
           
        # adding push button to the layout 
        layout.addWidget(self.button) 
           
        # setting layout to the main window 
        self.setLayout(layout)

        self.showMaximized()

        


    def plot(self):
        df = pd.read_csv('testdata.txt', sep='\t', header=None, parse_dates=[0])

        # clearing old figure 
        self.figure.clear() 
   
        # create an axis 
        ax = self.figure.add_subplot(111) 

        # plot data
        ax.plot(df[0], df[1], c='r', label='temperature')
        
        scale = 1.5
        f = zoom_factory(ax,base_scale = scale)
     
        # refresh canvas 
        self.canvas.draw()
        
        

# driver code 
if __name__ == '__main__': 
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    # loop 
    sys.exit(app.exec_()) 

0 个答案:

没有答案
相关问题