0

I want to open the image with matplotlib , then embed it into matplotlib plot and then display in Tkinter widget. I know how to display matplotlib graph in Tkinter, but don't know how to do it with the image. Here my code:

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from numpy import array, arange, sin, pi

root = Tk()
root_panel = Frame(root)
root_panel.pack(side="bottom", fill="both", expand="yes")

btn_panel = Frame(root_panel, height=35)
btn_panel.pack(side='top', fill="both", expand="yes")

img_arr = mpimg.imread('img/pic.jpg')
imgplot = plt.imshow(img_arr)

#here is the example of how I embed matplotlib graph in Tkinter,
#basically, I want to do the same with the image (imgplot)
f = Figure()
a = f.add_subplot(111)
t = arange(0.0, 3.0, 0.01)
s = sin(2*pi*t)
a.plot(t, s)

canvas = FigureCanvasTkAgg(f, master=root)
canvas.show()
canvas.get_tk_widget().pack(side="top", fill="both", expand=1)
canvas._tkcanvas.pack(side="top", fill="both", expand=1)

root.mainloop()

2 Answers2

3

To display the picture in the matplotlib figure embeded inside the Tkinter window, all there is to do is replace a.plot(t, s) by a.imshow(img_arr) in the code given in the question.

j_4321
  • 15,431
  • 3
  • 34
  • 61
1
canvas.show() 

gives an error : AttributeError: 'FigureCanvasTkAgg' object has no attribute 'show'

Instead of canvas.show() write canvas.draw() .

cherry
  • 336
  • 1
  • 2
  • 9