In Tkinter Python, I want to create two frames and place one on top of the other, and then insert a matplotlib plot inside each of these two frames. That, I managed to do. The problem is that, when created, these two frames always seem to have different heights (the bottom one is always smaller than the top one).
My goal is to get the two frames to have the same height.
Here’s a simplified version of the code that produces the said undesirable results :
import tkinter as tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.pyplot as plt
import numpy as np
# Root
root = tk.Tk()
root.state('zoomed')
# Upper frame
canv = tk.Frame(root)
canv.pack()
plt.rcParams["axes.prop_cycle"] = plt.cycler(color=["#4C2A85", "#BE96FF", "#957DAD", "#5E366E", "#A98CCC"])
plt.style.use('ggplot')
D = [i for i in range(10)]
W = [i for i in range(10)]
if len(W) > 0 :
W = np.array(W)
fig, ax = plt.subplots()
l = ax.fill_between(D, W.astype(np.float64))
ax.set_title("Upper plot")
canvas = FigureCanvasTkAgg(fig, canv)
canvas.draw()
canvas.get_tk_widget().pack()
# Lower frame
canv2 = tk.Frame(root)
canv2.pack()
D = [i for i in range(10)]
P = [i for i in range(10)]
if len(P) > 0 :
P = np.array(P)
fig2, ax2 = plt.subplots()
l2 = ax2.fill_between(D, P.astype(np.float64))
ax2.set_title("Lower plot")
canvas2 = FigureCanvasTkAgg(fig2, canv2)
canvas2.draw()
canvas2.get_tk_widget().pack()
root.mainloop
Thanks to anyone willing to help.
I tried to set the height value inside the pack method to be the same for each frame, but I didn’t see any changes.
Zakaria is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.