Liam Sfee
Liam Sfee

Reputation: 63

Heatmap sizing problem when implemented in tkinter

I'm trying to implement pcolor heatmap in tkinter, but they appear to be stretched even though the data fed in is a matrix of size 20x20, I want them to be square like they should

Pic: https://drive.google.com/file/d/1VBSUM8p3HeacW7wQJaCmR8ocrF0WYqBP/view

At first I called figsize to an even size but it did not have any effects on the size of heatmap. Then, I called axs.set_aspect = True, this actually made the heatmaps square, but it also made them shrink significantly in size.

I then changed the background of the canvas, in which the heatmaps are packed. I found out that the size of the canvas is not square so I'm suspecting it's the size of the canvas that's the culprit. However, no matter what width/height I gave the canvas, its size still won't change???

self.frame1 = Frame(self) 
self.frame1.pack() 
self.cvs_plt = LabelFrame(self.frame1, bg = "white")
self.cvs_plt.pack()
data1 = np.random.randn(20, 20)

self.fig = self.drawHeatMap2(data1)
self.fig_cv = FigureCanvasTkAgg(self.fig, self.cvs_plt)
self.fig_cv.get_tk_widget().pack()

def drawHeatMap2(self, data1):
    fig, axes = plt.subplots(1, 2, sharey=True)
    names = ["Actual", "Expected"]
    a = []
    for i in range(2):
        pcm = axes[i].pcolor(data1, cmap = "viridis_r")
        axes[i].set_title(names[i])
        axes[i].set_aspect("auto", "datalim", share = True)
        cb = fig.colorbar(pcm, ax = axes[i])
        a.append(cb)
    a[0].ax.set_visible(False)

Upvotes: 0

Views: 131

Answers (1)

Liam Sfee
Liam Sfee

Reputation: 63

    tkAgg = FigureCanvasTkAgg(self.fig, self.cvs_plt)

    self.fig_cvs = tkAgg.get_tk_widget()
    self.fig_cvs.config(width = 1000)
    self.fig_cvs.pack()

Initially, I thought FigureCanvasTkAgg would return a tkinter object. Turns out it doesn't, and get_tk_widget() does exactly this job. After having obtained the tk object which is the pyplot, you can manipulate it in whichever way you like using config()

Upvotes: 1

Related Questions