How to refresh FigureCanvasTkAgg continuously in tkinter - python

I'm trying to automatically update a chart in tkinter continuously using FigureCanvasTkAgg without the use of buttons.
Here is what I've coded so far
import random
import tkinter as tk
import seaborn as sb
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
root = tk.Tk()
def cplot():
xCord = [random.randint(0,10) for i in range(5)]
yCord = [random.randint(0,10) for i in range(5)]
#defining heatmap dimensions
fig, ax = plt.subplots()
#ploting heat map with x and y coordinates
sb.kdeplot(xCord, yCord, shade = True, cmap = "Reds")
ax.invert_yaxis()
plt.axis("off")
plt.show()
root.after(1, cplot)
return fig
fig = cplot()
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.draw()
canvas.get_tk_widget().pack()
root.after(1, cplot)
root.mainloop()

To have the plot image regenerate on a timer, recreate the canvas widget inside the function and recall the function.
Try this code:
import random
import tkinter as tk
import seaborn as sb
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
root = tk.Tk()
canvas = None
def cplot():
global canvas
xCord = [random.randint(0,10) for i in range(5)]
yCord = [random.randint(0,10) for i in range(5)]
#defining heatmap dimensions
fig, ax = plt.subplots()
#ploting heat map with x and y coordinates
sb.kdeplot(xCord, yCord, shade = True, cmap = "Reds")
ax.invert_yaxis()
plt.axis("off")
if canvas: canvas.get_tk_widget().pack_forget() # remove previous image
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.draw()
canvas.get_tk_widget().pack()
root.after(100, cplot)
root.after(1, cplot)
root.mainloop()

Related

Text moves axes upon panning with constrained layout

If I pan the function in the following example, the axis position is moved in order to keep TEXT in the canvas. Is there a way to keep the axes and to treat TEXT just like the plotted line while keeping constrained_layout=true?
import tkinter as tk
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.backends.backend_tkagg as tkagg
class NewGUI():
def __init__(self):
self.root = tk.Tk()
self.root.geometry('500x400+200+200')
self.plot_frame = tk.Frame()
self.plot_frame.place(x=10, y=10, relheight=0.7, relwidth=0.7, anchor='nw')
self.plot_window = Plotwindow(self, (18,12))
self.root.mainloop()
class Plotwindow():
def __init__(self, root, size):
fig = mpl.figure.Figure(size, constrained_layout=True)
ax = fig.add_subplot(111)
canvas = tkagg.FigureCanvasTkAgg(fig, master=root.plot_frame)
canvas.get_tk_widget().pack()
toolbar = tkagg.NavigationToolbar2Tk(canvas, root.root)
toolbar.update()
x = np.arange(0, 5, 0.1)
y = np.sin(x)
ax.plot(x, y)
ax.text(0, 0, 'TEXT')
if __name__ == '__main__':
new = NewGUI()

How to update matplotlib subplots on a tkinter Canvas?

I want to update subplots that are embedded into a Canvas in a tkinter GUI. Whatsoever I try, my intention fails. See how far I am:
from tkinter import *
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk)
import matplotlib.pyplot as plt
import time
root = Tk()
figId = plt.figure()
canvas = FigureCanvasTkAgg(figId, master=root)
canvas.get_tk_widget().pack()
canvas.draw()
vals1 = [5, 6, 3, 9]
vals2 = vals1
for i in range(0, len(vals1)+1):
toPlot = vals1[0:i]
plt.subplot(211).plot(toPlot)
plt.subplot(212).plot(toPlot)
time.sleep(1)
root.mainloop()
I figured out that doing something like plt.pause(.1) is not the right way. For me, it seems that I have to introduce matplotlib.animation, but I really have no clue how to do that.
I found the answer:
import random
from itertools import count
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from tkinter import *
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk)
x_vals = []
y_vals1 = []
y_vals2 = []
index = count()
root = Tk()
figId = plt.figure()
canvas = FigureCanvasTkAgg(figId, master=root)
canvas.get_tk_widget().pack()
canvas.draw()
def animate(i):
x_vals.append(next(index))
y_vals1.append(random.randint(0, 5))
y_vals2.append(random.randint(0, 5))
plt.cla()
plt.plot(x_vals, y_vals1)
plt.plot(x_vals, y_vals2)
ani = FuncAnimation(plt.gcf(), animate, interval=1000)
root.mainloop()

How can I update a heatmap on a GUI when I change the input values

I am trying to display a GUI with a heatmap and scales/sliders with the scales/sliders changing the values in the heatmap.
I can display the heatmap and sliders and can read from the sliders but I cannot get the heat map to update after I have moved the sliders.
I have tried putting the code (I think) updates the heatmap in a function which is called whenever the scale/slider is moved but I am clearly missing something.
import tkinter
from tkinter import ttk
from tkinter import *
from tkinter.ttk import *
from tkinter import messagebox
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
def myFunc(value):
print (mySlider.get())
array[1][2]=mySlider.get()
#I think I need to put something here to update the heatmap when the
#scale/slider is changed but do not know what
figure, ax = plt.subplots()
ax.imshow(array)
canvas.get_tk_widget().pack()
root = tkinter.Tk()
root.title("Something")
array = ([[1,2,3,4],
[3,9,1,5],
[8,4,1,7],
[2,4,9,1]])
figure, ax = plt.subplots()
ax.imshow(array)
canvas = plt.Figure()
canvas = FigureCanvasTkAgg(figure, root)
canvas.get_tk_widget().pack()
mySlider = tkinter.Scale(root, from_=0, to=15, orient=HORIZONTAL, command=myFunc)
mySlider.pack()
Like this:
import tkinter as tk
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
def myFunc(value):
print (mySlider.get())
array[1][2]=mySlider.get()
im.set_array(array)
canvas.draw()
root = tk.Tk()
root.title("Something")
array = ([[1,2,3,4],
[3,9,1,5],
[8,4,1,7],
[2,4,9,1]])
figure, ax = plt.subplots()
im = ax.imshow(array)
canvas = FigureCanvasTkAgg(figure, root)
canvas.get_tk_widget().pack()
mySlider = tk.Scale(root, from_=0, to=15, orient=tk.HORIZONTAL, command=myFunc)
mySlider.pack()
root.mainloop()
However tkinter is not needed here. matplotlib has a slider built in (I assume you know since you imported it) which is a lot easier to implement:
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
def myFunc(value):
array[1][2]=value
im.set_array(array)
array = ([[1,2,3,4],
[3,9,1,5],
[8,4,1,7],
[2,4,9,1]])
figure, ax = plt.subplots()
im = ax.imshow(array)
ax_slider = plt.axes([0.1, 0.1, 0.8, 0.03]) # [left, bottom, width, height]
slide = Slider(ax_slider, '', 0, 15, valinit=0)
slide.on_changed(myFunc)
plt.show()

How to realise matplotlib line picker by using opencv?

I would like to pick lines that have been drawn on an image by using cv2.line and do something with them. To realise that I had a look on matplotlibs picker and even found a good example here. As I am going to use tkinter for GUI I added it to my MWE.
Code from example which works fine:
import sys
if sys.version_info[0] < 3:
import Tkinter as Tk
else:
import tkinter as Tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.pyplot as plt
import numpy as np
root = Tk.Tk()
root.iconify()
x = np.linspace(0, 10, 100)
fig, ax = plt.subplots()
for i in range(1, 10):
ax.plot(x, i * x + x, picker=5)
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
def on_pick(event):
event.artist.set_visible(not event.artist.get_visible())
fig.canvas.draw()
fig.canvas.callbacks.connect('pick_event', on_pick)
root.mainloop()
Now my code using OpenCV which does not work:
import sys
if sys.version_info[0] < 3:
import Tkinter as Tk
else:
import tkinter as Tk
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import numpy as np
import cv2
root = Tk.Tk()
root.iconify()
img = np.zeros([100,100,3],dtype=np.uint8)
img.fill(255)
cv2.line(img,(10,10),(60,90),(100,149,237),2)
fig = Figure()
ax = fig.add_subplot(111)
ax.set_aspect('equal')
ax.plot()
ax.imshow(img)
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
def onpick1(event):
event.artist.set_visible(not event.artist.get_visible())
fig.canvas.draw()
fig.canvas.callbacks.connect('pick_event', onpick1)
root.mainloop()
In both examples onpick1 should set the lines visibility on or off by a single mouse click but it doesn't. My assumption is that it has something to do with the way it is plotted or how I draw the lines (ax.plt vs cv2.line). I would be very happy about any help. Thanks!

Plot graph with pyplot using input from tkinter spinbox

I am working in a project which needs to plot a graph dynamically as the inputs in a tkinter spinbox is changed.
I have a sample code:
from tkinter import *
from tkinter import font
from tkinter.font import Font
from tkinter import messagebox
print("'Tkinter' module is found as tkinter.")
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure
print("Importing matplotlib from libraries.")
master = Tk()
def ok(x_val=1000,y_val=20):
fig = Figure(figsize=(5,5),dpi=70)
ax = fig.subplots()
ax.set_title("Right Ear")
ax.set_ylabel("db HL")
ax.set_xlabel("Frequency")
ax.set_xlim(100,9000)
ax.set_ylim(130,-10)
ax.set_facecolor("#ffd2d2")
x = [125,250,500,1000,2000,4000,8000]
ticks = [125,250,500,"1K","2K","4K","8K"]
xm = [750,1500,3000,6000]
ax.set_xscale('log', basex=2)
ax.set_xticks(x)
ax.set_xticks(xm, minor=True)
ax.set_xticklabels(ticks)
ax.set_xticklabels([""]*len(xm), minor=True)
ax.yaxis.set_ticks([120,110,100,90,80,70,60,50,40,30,20,10,0,-10])
ax.plot([x_val],[y_val],'r+',markersize=15.0,mew=2)
ax.grid(color="grey")
ax.grid(axis="x", which='minor',color="grey", linestyle="--")
canvas = FigureCanvasTkAgg(fig, master=master)
canvas.show()
canvas.get_tk_widget().grid(column=0,row=2,columnspan=3,rowspan=15)
def action():
print(spin.get())
canvas.draw()
ok(spin.get(),10)
spin = Spinbox(master, from_=125,to=8000,command=action)
spin.grid(column=5,row=2)
ok()
This code does not change the plot, I cannot understand how to change it, to be precise, how to use canvas.draw() here to do the work. The spinbox has value range from 125 to 8000, I could not figure out how to take the value of the spinbox every time it changes (can use command= but how to implement) and feed it to the x axis of ax.plot() and plot dynamically. As the value of spinbox changes the plot also changes to the new position and removes the previous plot from the previous position.
You need to make the variables you need available. A usual approach is to use a class and make those class variables. Those can then be accessed from within the class (self) or outside as attributes.
from Tkinter import *
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
class PlotClass():
def __init__(self):
fig = Figure(figsize=(5,5),dpi=70)
ax = fig.subplots()
ax.set_title("Right Ear")
ax.set_ylabel("db HL")
ax.set_xlabel("Frequency")
ax.set_xlim(100,9000)
ax.set_ylim(130,-10)
ax.set_facecolor("#ffd2d2")
x = [125,250,500,1000,2000,4000,8000]
ticks = [125,250,500,"1K","2K","4K","8K"]
xm = [750,1500,3000,6000]
ax.set_xscale('log', basex=2)
ax.set_xticks(x)
ax.set_xticks(xm, minor=True)
ax.set_xticklabels(ticks)
ax.set_xticklabels([""]*len(xm), minor=True)
ax.yaxis.set_ticks([120,110,100,90,80,70,60,50,40,30,20,10,0,-10])
self.line, = ax.plot([],[],'r+',markersize=15.0,mew=2)
ax.grid(color="grey")
ax.grid(axis="x", which='minor',color="grey", linestyle="--")
self.canvas = canvas = FigureCanvasTkAgg(fig, master=master)
canvas.show()
canvas.get_tk_widget().grid(column=0,row=2,columnspan=3,rowspan=15)
self.spin = Spinbox(master, from_=125,to=8000,command=self.action)
self.spin.grid(column=5,row=2)
def ok(self, x=1000,y=20):
self.line.set_data([x],[y])
self.canvas.draw_idle()
def action(self):
self.ok(float(self.spin.get()),10)
master = Tk()
plotter = PlotClass()
plotter.ok(125,10)
master.mainloop()
Note: In newer versions of matplotlib you should use NavigationToolbar2Tk instead of NavigationToolbar2TkAgg.

Categories