0

I want to plot multiple images on the same graph in python using FigureCanvasTkAgg.When using matplotlib I could use {import matplotlib.pyplot as plt} plt.imshow(image1) ; plt.pause(0.6) ; plt.imshow(image2) Since I use Tkinter,I make use of FigureCanvasTkAgg to do the same

   `f = Figure(figsize=(6,6))

    a = f.add_subplot(111)
    image = np.array(np.random.random((1024,1024))*100,dtype=int)
    a.imshow(image)

    canvas = FigureCanvasTkAgg(f, self)
    canvas.show()
    canvas.get_tk_widget().pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)`

Could anyone please help me in how to achieve pause effect of matplotlib in FigureCanvasTkAgg.

1 Answer 1

0

You could use the threading module to schedule the execution of a function:

from threading import Thread

def wrapper():
    time.sleep(5)
    image = np.array(np.random.random((1024,1024))*100,dtype=int)
    ax.imshow(image)
    canvas.draw()

_t = Thread(target=wrapper)
_t.start()

This will execute the function wrapper in a new thread, so the original program can go on meanwhile.

Here the full example:

#!/usr/bin/env python
import time
import tkinter as tk
from threading import Thread
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import numpy as np

root = tk.Tk()
f = Figure(figsize=(6,6))

ax = f.add_subplot(111)
# First image    
image = np.array(np.random.random((1024,1024))*100, dtype=int)
ax.imshow(image)

canvas = FigureCanvasTkAgg(f, root)
canvas.draw()
canvas.get_tk_widget().pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)

def wrapper():
    time.sleep(5)
    # Second image appears after 5 seconds
    image = np.array(np.random.random((1024,1024))*100,dtype=int)
    ax.imshow(image)
    canvas.draw()

_t = Thread(target=wrapper)
_t.start()

root.mainloop()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.