【发布时间】:2020-06-10 10:06:13
【问题描述】:
我正在尝试使用 Tkinter 在 GUI 中将 Matplotlib 图表居中。问题是我似乎无法将图形放在画布的中心(如下图所示,图形粘在画布的顶部,稍微靠右)。我也在使用图形的“figsize”参数,但实际上并没有改变大小。我也尝试过调整子图,但这似乎也没有效果。为什么 figsize 似乎不起作用,我怎样才能使图形在画布中居中?
图形界面
页面代码
# DeReKo Stats
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
top = tk.Frame(self)
bottom = tk.Frame(self)
top.pack(side=tk.TOP, expand = False)
bottom.pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)
label = tk.Label(self, text="DeReKo Stats", font=LARGE_FONT)
btn = ttk.Button(self, text="Return",
command=lambda: controller.show_frame(StartPage))
btn2 = ttk.Button(self, text="Jsyncc Stats",
command=lambda: controller.show_frame(PageOne))
btn3 = ttk.Button(self, text="Weirdness",
command=lambda: controller.show_frame(PageThree))
txtbox = ttk.Entry(self, width=5)
label.pack(in_=top, side=tk.TOP,pady=10, padx=10)
btn.pack(in_=top, side=tk.LEFT)
btn2.pack(in_=top, side=tk.LEFT)
btn3.pack(in_=top, side=tk.LEFT)
txtbox.pack(in_=top, side=tk.LEFT, padx=5)
with open(DEREKO_FILE_NAME, buffering=20000000, encoding="utf-8") as f:
freq = FreqDist(json.loads(f.read()))
fig = plt.figure(figsize=(30,30))
ax1 = fig.add_subplot(111)
df1 = DataFrame(freq.most_common(10), columns=['Token', 'Frequency'])
df1 = df1[['Token', 'Frequency']].groupby(['Token'], sort=False).sum()
df1.plot(kind='bar', legend=True, ax=ax1, rot=0)
plt.subplots_adjust(hspace=5, right=.95)
canvas = FigureCanvasTkAgg(fig, self)
canvas.draw()
canvas.get_tk_widget().pack(in_=bottom, expand=True, fill=tk.BOTH, pady=(30,10), padx=(15,15))
MainWindow.grid_conf(self, tk.Frame)
【问题讨论】:
标签: python matplotlib tkinter