python - 尽管使用 pyplot.show(),但如何使用 matplotlib 保持图形大小不变?

标签 python matplotlib

请看下面的 python 示例:

import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rcParams['figure.figsize'] = [8.27, 11.69]
fig = plt.figure()
plt.show() # This line causes problems with figsize
fig.savefig('test.pdf')

如果我使用“plt.show()”,test.pdf 的图形大小会发生很大变化(来自

  • 210 x 297 [毫米] 到
  • 213 x 250 [毫米]

我该怎么做才能保持不变?

最佳答案

SciPy 上有一个很好的关于在 matplotlib 图形中调整图像大小的演示。网站。

show() 对图形大小的影响取决于使用的 matplotlib 后端。例如,当我使用 TkAgg 后端(我系统上的默认后端)时,它向图形的宽度和高度添加了大约 12 个像素。但是当我切换到 WXAgg 后端时,图形大小实际上减小了。

此外,手动调整 show() 显示的窗口大小也会改变图形大小。此外,如果显示图形需要的窗口对于屏幕而言太大,则窗口大小将减小,图形大小也会相应减小。

无论如何,您最好的选择可能是在呈现 pdf 之前重置图形大小。即:

fig.set_size_inches(8.27, 11.69)
fig.savefig('test.pdf')

关于python - 尽管使用 pyplot.show(),但如何使用 matplotlib 保持图形大小不变?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5242628/

相关文章:

python - Pyplot 图例/键,但用于比例,就像在 map 上一样

python - 在Tkinter中嵌入Matplotlib,显示问题

python - Propellerheads 的 NN-XT 文件格式 : Problems with REFE chunk

python - SOM-- 神经网络

python - cx_卡住 : LookupError: unknown encoding: cp437

python - 如何计算pandas中行值大于特定值的金额?

python - 支持中型发行版的 Python 脚本/依赖项?

python - 具有等高线水平的连续颜色条

python - Google App Engine 中的服务器发送事件 (SSE)

python - 如何在 Matplotlib 中使用 Latex 正确写入 '<' 和 '>' 符号