【问题标题】:Tkinter with matplotlib - Heatmap colorbar not clearing with rest of axes带有 matplotlib 的 Tkinter - 热图颜色条不与其余轴一起清除
【发布时间】:2021-04-05 16:21:18
【问题描述】:

我正在使用 tkinter 和 matplotlib(和 seaborn)构建一个 GUI,以显示来自用户选择的 csv 的热图。我希望热图在每次加载时更新,并使用适当的颜色条。每次加载新数据时,我都会清除坐标轴,但颜色条永远不会消失,并且新的热图会挤到一边。我希望旧的颜色条也被清除,以便新的热图可以正确填充空间。

我做了一个 MWE 来炫耀我的问题:

import numpy as np
import tkinter as tk
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import seaborn as sns

class App(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.state('zoomed')
        self.winfo_toplevel().title('App')

        frame = tk.Frame(self)
        frame.pack()

        button_reload = tk.Button(frame, text='Reload data', command=self.reload_data)
        button_reload.pack()

        self.fig = Figure()
        self.ax = self.fig.add_subplot(111)

        self.fig.tight_layout()  # Small margins
        self.ax.axis('off')  # Disable axis lines
        self.canvas_heatmap = FigureCanvasTkAgg(self.fig, master=frame)
        self.canvas_heatmap.get_tk_widget().pack(expand=True, fill='both')

    def reload_data(self):
        # dummy data for example
        data = np.random.rand(3,3)

        # Clear old heatmap from axes
        self.ax.clear()

        # Set up new heatmap
        self.ax = sns.heatmap(data, ax=self.ax, linewidth=0.1)
        self.canvas_heatmap.draw()
        self.canvas_heatmap.get_tk_widget().pack(expand=True, fill='both')  # necessary?
    
def quit_GUI():
    root.quit()
    root.destroy()

if __name__ == '__main__':
    root = App()
    root.protocol('WM_DELETE_WINDOW', quit_GUI)  # Kill process on clicking 'X'
    root.mainloop()

这里有一些照片,当我不希望它们出现时,你可以在其中看到颜色条。

到目前为止还不错:

不好:

更糟:

我可以一直这样下去,直到我的热图变成一条条。

【问题讨论】:

    标签: python matplotlib tkinter seaborn


    【解决方案1】:

    你需要清除人物并重新制作斧头。

    import numpy as np
    import tkinter as tk
    from matplotlib.figure import Figure
    from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
    import seaborn as sns
    
    class App(tk.Tk):
        def __init__(self):
            tk.Tk.__init__(self)
            self.state('zoomed')
            self.winfo_toplevel().title('App')
    
            frame = tk.Frame(self)
            frame.pack()
    
            button_reload = tk.Button(frame, text='Reload data', command=self.reload_data)
            button_reload.pack()
    
            self.fig = Figure()
            self.canvas_heatmap = FigureCanvasTkAgg(self.fig, master=frame)
            self.canvas_heatmap.get_tk_widget().pack(expand=True, fill='both')
    
        def reload_data(self):
            data = np.random.rand(3,3)
            self.fig.clear()
            ax = self.fig.add_subplot(111)
            ax.axis('off')  # Disable axis lines
            line = sns.heatmap(data, ax=ax, linewidth=0.1)
            self.fig.tight_layout()  # Should go after the drawing
            self.canvas_heatmap.draw()
            # ~ self.canvas_heatmap.get_tk_widget().pack(expand=True, fill='both')  # not necessary
            
    if __name__ == '__main__':
        root = App()
        # ~ root.protocol('WM_DELETE_WINDOW', quit_GUI)  # not needed
        root.mainloop()
    

    【讨论】:

      猜你喜欢
      • 2018-03-23
      • 1970-01-01
      • 1970-01-01
      • 2018-03-27
      • 2018-10-31
      • 2018-06-21
      • 1970-01-01
      • 2019-07-14
      • 1970-01-01
      相关资源
      最近更新 更多