[英]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,图现在占用更多屏幕区域 % [
除了更改边距的建议外,您还可以将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.