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.
Asked
Active
Viewed 1,810 times
0
-
For that purpose, not exactly Tkinter would be helpful. I would suggest try exploring other Python libraries like `mpld3` or `Bokeh`. – Kartikeya Apr 10 '22 at 16:21
-
There are two inferences I could draw : One is I will have to use a GUI toolkit other than Tkinter i.e. PyQt or WxPython. Secondly, libraries like mpld3 and Bokeh have a possibility of going well with Tkinter, is that so? – Prachi Singh Apr 11 '22 at 02:34
-
Please provide enough code so others can better understand or reproduce the problem. – Community Apr 11 '22 at 08:46
-
The 2nd inference: first use `matplotlib` to plot the graph, then transform it to an interactive graph using the `mpld3` and then integrate it in the Tkinter window if possible. This was my idea. [For example, there are ways to integrate mpld3 plots with html](https://www.freecodecamp.org/news/how-to-embed-interactive-python-visualizations-on-your-website-with-python-and-matplotlib/), likewise, I was suggesting to also try integrating mpld3 plots with Tkinter. – Kartikeya Apr 11 '22 at 09:20
1 Answers
1
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.

Kartikeya
- 818
- 2
- 6
- 11
-
I'm unable to use the zoom button, not sure why. Actually, I'm drawing real time plots, which updates itself continuously. However, I'm able to move/pan the canvas vaguely. Is there a possibility of real time updates interfering with tkinter functionalities such as zoom? I'm a newbie here and any help would be really valuable. – Prachi Singh Apr 12 '22 at 15:52
-
Zoom not possible? What OS and Python version are you using? You can use [the pan/zoom button or zoom-to-rectangle button](http://omz-software.com/pythonista/matplotlib/users/navigation_toolbar.html) for zooming in the graph. – Kartikeya Apr 12 '22 at 16:11
-
There is no way to have real-time updates on plots with Tkinter. All you can do is create the plots every time any changes are made, then update the same on the Tkinter window. Refer to [Updating the coordinates of a graph in matplotlib](https://stackoverflow.com/questions/3877774/updating-a-graphs-coordinates-in-matplotlib). – Kartikeya Apr 12 '22 at 16:11
-
Okay, so there was a problem with my understanding. Your answer actually solves my problem. Thanks for helping. – Prachi Singh Apr 14 '22 at 10:23