繁体   English   中英

问题在 tkinter 图 canvas 上调整 plot 的大小

[英]Problem resizing plot on tkinter figure canvas

Python 3.9 在运行 OS 11.6.1 的 Mac 上。 My application involves placing a plot on a frame inside my root window, and I'm struggling to get the plot to take up a larger portion of the window. 我认为rcParams中的matplotlib.pyplot会解决这个问题,但我必须忽略一些东西。

这是我到目前为止所拥有的:

import numpy as np
from tkinter import Tk,Frame,TOP,BOTH

import matplotlib
from matplotlib import pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

plt.rcParams["figure.figsize"] = [18,10]

root=Tk()
root.wm_title("Root Window")
root.geometry('1500x1000')

x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)
fig, ax = plt.subplots()
ax.plot(x, y)

canvas_frame=Frame(root) # also tried adjusting size of frame but that didn't help
canvas_frame.pack(side=TOP,expand=True)
canvas = FigureCanvasTkAgg(fig, master=canvas_frame)
canvas.draw()
canvas.get_tk_widget().pack(side=TOP,fill=BOTH,expand=True)


root.mainloop()

对于我的实际应用,我需要canvas有一个框架作为其父级,而不仅仅是root ,这就是上面介绍canvas_frame的原因。

尝试这样的事情:

fig.subplots_adjust(left=0.05, bottom=0.07, right=0.95, top=0.95, wspace=0, hspace=0)

这是 output,图现在占用更多屏幕区域 % [ 图现在需要更多的屏幕房地产1

除了更改边距的建议外,您还可以将fill=BOTH添加到 canvas 小部件。

canvas_frame.pack(side=TOP,expand=True,fill=BOTH)

这使得图形 canvas 占用更多空间,但 plot 并没有占用整个 window。 但是,如果我调整 window 的大小,那么 plot 会占用整个 window。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM