我正在使用以下代码构建一个简单的图表:
import matplotlib.pyplot as plt
idl_t, idl_q = [[0, 20], [8, 24]], [[100, 100], [100, 100]]
dsc_t, dsc_q = [[8, 14], [12, 18]], [[100, 100], [5, 5]]
cha_t, cha_q = [[12, 18], [14, 20]], [[5, 5], [100, 100]]
plt.figure(figsize=(7, 3))
plt.plot(idl_t, idl_q, color="blue", lw=2)
plt.plot(dsc_t, dsc_q, color="red", lw=2)
plt.plot(cha_t, cha_q, color="green", lw=2)
plt.xlabel("Time (hour)")
plt.show()
我很确定这是因为更改了 figsize
并且似乎它按比例分配了标签空间。请参阅下面的正常 figsize
图:
如何为具有不同 figsize
的 xlabel
保留足够的空间?它实际上应该是这样的吗,因为它对我来说似乎是一个错误?
最佳答案
plt.figure(figsize=(7, 3))
plt.plot(idl_t, idl_q, color="blue", lw=2)
plt.plot(dsc_t, dsc_q, color="red", lw=2)
plt.plot(cha_t, cha_q, color="green", lw=2)
plt.xlabel("Time (hour)")
plt.tight_layout() # <- HERE
plt.show()
更新
根据@TrentonMcKinney的建议,您还可以将 tight_layout=True
作为 plt.figure
plt.figure(figsize=(7, 3), tight_layout=True)
关于python - 由于 Figsize,x 标签位于我的图形之外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70195707/