【发布时间】:2026-02-05 23:15:01
【问题描述】:
似乎我无法理解我应该在 Python MatplotLib 中做什么才能或多或少准确地保存我在屏幕上看到的内容。
这是我准备的测试代码:
import matplotlib.pyplot as plt
import numpy as np
def main():
values = [712, 806, 858, 990, 1158, 1126, 166]
xlabels = np.arange(2013, 2020)
ylabels = ylabels = np.arange(400,1300,400)
index = np.arange(len(xlabels))
fig = plt.figure(1, figsize=(12,16), dpi=100)
plt.bar(index, values, color='grey')
plt.xticks(index, xlabels, fontsize=30)
plt.yticks(ylabels, ylabels, fontsize=30)
plt.ylim((0, 1400))
plt.title('Title', fontsize=40)
plt.savefig('../figs/test.png')
plt.show()
# -----------------------------------------
if __name__ == "__main__":
main()
这是savefig保存的图片:
如果我使用fig.savefig(...) 代替plt.savefig(...),则没有任何变化。
我做错了什么?
【问题讨论】:
-
你试过
fig.savefig()吗? -
现在完成。没什么变化,我会更新问题。
-
尝试使用
figsize=(7,5)或figsize=(8,6)之类的东西,而不是figsize=(12,16) -
如下所述,您的显示器不是 16 英寸高,因此 GUI 使图形更短。 Savefig 没有这样的限制。如上所述,如果您希望它看起来与保存的图相同,请将您的图缩小以适合屏幕。
标签: python matplotlib