Home > Enterprise >  Interactive figures in Tkinter
Interactive figures in Tkinter

Time:04-15

I'm looking for a suggestion for making plots in Tkinter. I'm using Spyder IDE. I'm using matplotlib for real time graphs and they are working fine. But I want the plots to be interactive i.e. I should be able to zoom in, get the (x,y) coordinates when pointed over a specific region on plot etc. which does not seem feasible with matplotlib. My question is, what plotting library can I use in such a case? I was going to go with Plotly but I read that it is not compatible with Tkinter. Is there anything out there which can help make interactive figures in Tkinter? Thank you in advance.

CodePudding user response:

But I want the plots to be interactive i.e. I should be able to zoom in, get the (x,y) coordinates when pointed over a specific region on plot etc. which does not seem feasible with matplotlib.

It is possible with matplotlib, here is a sample:

from tkinter import *
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk)

def plot():
    fig = Figure(figsize = (5, 5), dpi = 100)        
    y = [i**2 for i in range(101)]
    plot1 = fig.add_subplot(111)
    plot1.plot(y)

    
    canvas = FigureCanvasTkAgg(fig,master = window)
    canvas.draw()
    canvas.get_tk_widget().pack()
    toolbar = NavigationToolbar2Tk(canvas,window)
    toolbar.update()
    canvas.get_tk_widget().pack()
    
window = Tk()
window.title('Plotting in Tkinter')
window.state('zoomed')   #zooms the screen to maxm whenever executed

plot_button = Button(master = window,command = plot, height = 2, width = 10, text = "Plot")
plot_button.pack()
window.mainloop()

You can select the zoom button to zoom in a rectangular region, zoom back out, move in the graph by selecting the move tool, and hover over a point in graph to get its x-y coordinates shown on the right-bottom corner. All of that inside a tkinter window.


My question is, what plotting library can I use in such a case?

If using only matplotlib includes all your above-mentioned interactiveness, then I don't think you need to use any other library.

  • Related