dimanche 26 juin 2016

matplotlib's zoom functionality inside a tkinter canvas


I have been trying to transfer some scripts behind a GUI (using Tkinter) and so far have made it thus far that any data that is opened is shown in a Tkinter canvas (using matplotlib to draw it).

The only problem that I have with this is that the standard zoom/scrolling that are in matplotlib (using left mouse button to 'move' the plot and right mouse button to 'zoom') are not accessible in the canvas, basically the functionality of the '4 pointed cross' in the matplotlib plot window.

I think this would require creating my own handlers but I would assume that there has to be a way to use the default handlers of matplotlib? I have also looked at 'scrolling' canvas options as mentioned in this question but those only seem to change the size of the plot area instead of zooming in/out on the data, also I do not want to add any further buttons just to manipulate the plot area.

The bare minimum code that I have currently:

#! /usr/bin/env python
from Tkinter import *
import matplotlib.pyplot as plt
import matplotlib.backends.backend_tkagg as tkagg
import tkFileDialog

class App():
    def __init__(self,master):
        # VARIABLES
        self.inputFile = ""
        self.fig = plt.Figure()
        self.canvas = tkagg.FigureCanvasTkAgg(self.fig, master = master)
        self.canvas.get_tk_widget().pack()
        self.canvas.draw()

        # FRAME
        frame = Frame(master)
        master.title("MassyTools 0.1.1 (Alpha)")

        # VARIABLE ENTRIES

        # BUTTONS

        # MENU
        menu = Menu(root)
        root.config(menu = menu)

        filemenu = Menu(menu)
        menu.add_cascade(label="File", menu=filemenu)
        filemenu.add_command(label="Open Input File", command = self.openFile)
        calibmenu = Menu(menu)
        menu.add_cascade(label="Calibrate",menu=calibmenu)
        calibmenu.add_command(label="Open Calibration File", command = self.openCalibrationFile)
        calibmenu.add_command(label="Calibrate", command = self.calibrateData)  

    def openFile(self):
        file_path = tkFileDialog.askopenfilename()
        setattr(self,'inputFile',file_path)
        data = self.readData()
        self.plotData(data)

    def openCalibrationFile(self):
        print "Place holder for selection of the calibration file"

    def calibrateData(self):
        print "Place holder for actual calibration"

    def readData(self):
        x_array = []
        y_array = []
        with open(self.inputFile,'r') as fr:
            for line in fr:
                line = line.rstrip('n')
                values = line.split()
                x_array.append(float(values[0]))
                y_array.append(float(values[1]))
        return zip(x_array,y_array)

    def plotData(self,data):
        x_array = []
        y_array = []
        for i in data:
            x_array.append(i[0])
            y_array.append(i[1])
        self.fig.clear()
        self.axes = self.fig.add_subplot(111)
        self.line, = self.axes.plot(x_array,y_array)
        self.canvas.draw()

# Stuff that is not being used now but can be useful                        
    """def openFile(self,number):
        name = tkFileDialog.askopenfilename()
        ops = {
            1: 'deglycoData',
            2: 'peptideFile',
            3: 'mzML'
        }
        setattr(self,ops[number],name)
    """
# End of 'stuff'

root = Tk()
app = App(root)
root.mainloop()

Aucun commentaire:

Enregistrer un commentaire