【问题标题】:How to get buttons on a matplotlib subplot in Tkinter?如何在 Tkinter 中的 matplotlib 子图上获取按钮?
【发布时间】:2019-09-30 06:50:32
【问题描述】:

我是 Tkinter 的新手,想在 matplotlib 子图中获取按钮并使用 Tkinter 显示它。我确实在互联网上搜索过,但找不到完全符合我需求的东西。如果有人可以提供任何见解,那真是太棒了!

截至目前,我尝试了类似的方法(参见下面的代码)并在图形画布中获得了 matplotlib 图形。我想知道如何在每个子图中启用两个按钮。

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import ImageGrid
import numpy as np
from tkinter import *
import matplotlib
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure 
from PIL import Image

win = Tk()

folder = r'D:\V\Bilder_resized\Test'
im = np.arange(100)
im.shape = 10, 10

fig = plt.figure(figsize = (10,10))

grid = ImageGrid(fig, 221,  # similar to subplot(111)
             nrows_ncols=(2, 2),  # creates 2x2 grid of axes
             axes_pad=0.25,  # pad between axes in inch.
             )
for gridax in grid:
    gridax.set_title('True Positive', fontsize = 13)
    gridax.set_xticks([])
    gridax.set_yticks([])
grid_1 = ImageGrid(fig, 222,  # similar to subplot(111)
             nrows_ncols=(1, 2),  # creates 2x2 grid of axes
             axes_pad=0.25,  # pad between axes in inch.
             )
for gridax in grid_1:
    gridax.set_title('False Positive', fontsize = 13)
    gridax.set_xticks([])
    gridax.set_yticks([])
grid_2 = ImageGrid(fig, 223,  # similar to subplot(111)
             nrows_ncols=(1, 2),  # creates 2x2 grid of axes
             axes_pad=0.25,  # pad between axes in inch.
             )
for gridax in grid_2:
    gridax.set_title('False Negative', fontsize = 13)
    gridax.set_xticks([])
    gridax.set_yticks([])
grid_3 = ImageGrid(fig, 224,  # similar to subplot(111)
             nrows_ncols=(2, 2),  # creates 2x2 grid of axes
             axes_pad=0.25,  # pad between axes in inch.
             )
for gridax in grid_3:
    gridax.set_title('True Negative', fontsize = 13)
    gridax.set_xticks([])
    gridax.set_yticks([])

for i in range(4):
    image = os.path.join(folder, true_positive[i])
    img= Image.open(image)
    d = ImageDraw.Draw(img)
    d.text((30,30),"{}".format(np.round(true_positive_confidence[i],2)), 
fill = (255,255,0))
grid[i].imshow(img) 
# The AxesGrid object work as a list of axes.
x = len(false_positive)
c = len(false_negative)

if x < 2:
    a =x 
else:
   a =2

for i in range(a):
    image = os.path.join(folder, false_positive[i])
    img= Image.open(image)
    d = ImageDraw.Draw(img)
    d.text((30,30),"{}".format(np.round(false_positive_confidence[i],2)), 
fill = (255,255,0))
    grid_1[i].imshow(img)
if c < 2:
    b = c
else:
    b =2    
for i in range(b):
    image = os.path.join(folder, false_negative[i])
    img= Image.open(image)
    d = ImageDraw.Draw(img)
    d.text((30,30),"{}".format(np.round(false_negative_confidence[i],2)), 
fill = (255,255,0))
    grid_2[i].imshow(img)
for i in range(4):
    image = os.path.join(folder, true_negative[i])
    img= Image.open(image)
   d = ImageDraw.Draw(img)
d.text((30,30),"{}".format(np.round(true_negative_confidence[i],2)), fill = (255,255,0))
grid_3[i].imshow(img)

canvas = FigureCanvasTkAgg(fig, master=win)

graph_widget = canvas.get_tk_widget()
graph_widget.grid(row=0, column=0, columnspan=3, sticky = 'nsew')
win.mainloop()
plt.show()

代码的输出: output of the above code

我想在每个子图下方嵌入“上一个”和“下一个”按钮,以便在文件夹内导航以显示上一组或下一组图像。 我还添加了一个关于我需要它的示例图像。 Example image

如果有人可以提供有关如何执行此操作的任何想法,那将非常有帮助。抱歉,如果我的帖子中有不清楚的地方!

【问题讨论】:

    标签: python matplotlib tkinter widget tkinter-canvas


    【解决方案1】:

    更好的组织方式是创建一个Frame 来保存每个Figure 对象和按钮,然后对框架进行网格化。下面是一个使用从tk.Frame 继承的类的示例:

    import numpy as np
    import tkinter as tk
    from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
    from matplotlib.figure import Figure
    
    win = tk.Tk()
    
    class FigureFrame(tk.Frame):
        def __init__(self,master=None,**kwargs):
            super().__init__(master,**kwargs)
            self.fig = Figure(figsize=(5, 4), dpi=100)
            self.canvas = FigureCanvasTkAgg(self.fig, master=self)
            graph_widget = self.canvas.get_tk_widget()
            graph_widget.grid(row=0, column=0, columnspan=3, sticky = 'nsew')
            self.plot_next()
            tk.Button(self,text="Prev",command=self.plot_next).grid(row=1,column=0,sticky="w")
            tk.Button(self,text="Next",command=self.plot_next).grid(row=1,column=2,sticky="e")
    
        def plot_next(self):
            self.fig.clear()
            r = np.random.randint(1,10,5)
            self.fig.add_subplot(111).plot(r)
            self.canvas.draw_idle()
    
    for i in range(4):
        f = FigureFrame(win)
        f.grid(row=i//2,column=i%2)
    
    win.mainloop()
    

    【讨论】:

    • 哇.. 这是我想要的一个很好的例子。非常感谢:)
    • 嗨。我确实创建了不同的框架并使用网格将它们连接起来。但是有没有给每一帧而不是整个窗口的标题。 ??
    • plot_next 函数中,subplot = self.fig.add_subplot(111);subplot.plot(r);subplot.set_title("your graph title")。也请继续提出一个新问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-07
    • 1970-01-01
    相关资源
    最近更新 更多