Matplotlib & Tkinter:防止标题隐藏图形
Matplotlib & Tkinter: prevent titles from hiding the graph
我有两个子图,每个都有标题。我用 tkinter 在下面绘制它们。
这按它应该的方式工作。我的问题是,在减少整个 window 的同时,下图的标题覆盖了上图的 x-label 。是否有可能在子图(-titles)之间有一个最小值 space?谢谢:)
这里有一个例子:
from tkinter import *
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
root = Tk()
x = [-3, -2, -1, 0, 1, 2, 3, 4, 5]
y_1 = [2, 4, 6, 8, 1, 3, 5, 7, 9]
y_2 = [9, 6, 3, 1, 4, 7, 6, 2, 2]
fig = Figure(figsize=(9, 9))
ax = fig.add_subplot(211)
ax.set_title("TITLE ONE")
ax.set_ylabel("Y1")
ax.set_xlabel("X")
line = ax.plot(x, y_1, color="black" )[0]
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.get_tk_widget().pack()
ax2 = fig.add_subplot(212)
ax2.set_title("TITLE TWO")
ax2.set_ylabel("Y2")
ax2.set_xlabel("X")
ax2.plot(x, y_2, color="black")
fig.tight_layout()
root.mainloop()
删除 fig.tight_layout()
行,改用
import matplotlib
matplotlib.rcParams['figure.autolayout'] = True
在你的脚本之上。
我有两个子图,每个都有标题。我用 tkinter 在下面绘制它们。 这按它应该的方式工作。我的问题是,在减少整个 window 的同时,下图的标题覆盖了上图的 x-label 。是否有可能在子图(-titles)之间有一个最小值 space?谢谢:)
这里有一个例子:
from tkinter import *
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
root = Tk()
x = [-3, -2, -1, 0, 1, 2, 3, 4, 5]
y_1 = [2, 4, 6, 8, 1, 3, 5, 7, 9]
y_2 = [9, 6, 3, 1, 4, 7, 6, 2, 2]
fig = Figure(figsize=(9, 9))
ax = fig.add_subplot(211)
ax.set_title("TITLE ONE")
ax.set_ylabel("Y1")
ax.set_xlabel("X")
line = ax.plot(x, y_1, color="black" )[0]
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.get_tk_widget().pack()
ax2 = fig.add_subplot(212)
ax2.set_title("TITLE TWO")
ax2.set_ylabel("Y2")
ax2.set_xlabel("X")
ax2.plot(x, y_2, color="black")
fig.tight_layout()
root.mainloop()
删除 fig.tight_layout()
行,改用
import matplotlib
matplotlib.rcParams['figure.autolayout'] = True
在你的脚本之上。