Matplotlib scaling function inside tkinter canvas

I am trying to port some scripts behind the GUI (using Tkinter) and still did this until all open data is displayed on the Tkinter canvas (using matplotlib to draw it).

The only problem I am facing is that the standard scaling / scrolling that are in matplotlib (using the left mouse button to “move” the graph and the right mouse button to “zoom”) are not available on the canvas, basically the functionality "4 pointed cross" in the matplotlib chart window.

I think this will require the creation of my own handlers, but I would suggest that there should be a way to use the default matplotlib handlers? I also looked at the options for “scrolling” the canvas, as mentioned in this question, but it only seems to change the size of the chart area, and not zoom in / out on the data, nor do I want to add additional buttons to manipulate the chart area.

The bare minimum code I have is:

#! /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()
+4
source share
1 answer

So, you can attach an object NavigationToolbar2TkAggto your canvas, which will give you all the usual matplotlib methods and tools.

import matplotlib.backends.backend_tkagg as tkagg

# canvas is your canvas, and root is your parent (Frame, TopLevel, Tk instance etc.)
tkagg.NavigationToolbar2TkAgg(canvas, root)

: matplotlib.

, , (. class NavSelectToolbar(NavigationToolbar2TkAgg)).

+3

Source: https://habr.com/ru/post/1534254/


All Articles