matplotlib - 如何在matplotlib中保持纵横比的同时增加保存图形的大小?

标签 matplotlib

我正在绘制数据并将其保存到文件中。

import matplotlib.pyplot as plt
fig=plt.figure(figsize=(10,10))
plt.gca().set_aspect(1)
plt.scatter(range(10), range(0,20,2))
fig.savefig("test.jpg")

有没有一种简单的方法可以使图形尺寸更大,但不必猜测高度/宽度值以保持纵横比作为轴?

如果我有多个子图并且我想保持它们的固定轴比率,那就更难了。我想控制图形大小(比例),但不要在图形中引入空边框。

最佳答案

一种方法是使用 .set_size_inches 设置新的图形大小:

zoom = 2
w, h = fig.get_size_inches()
fig.set_size_inches(w * zoom, h * zoom)

这应该在保持比例的同时改变大小。

另一种方法是增加dpi如果您只需要保存图形的不同大小:
fig, ax = plt.subplots(figsize=(10,10))
ax.set_aspect(1)
ax.scatter(range(10), range(0,20,2))
dpi = fig.get_dpi()   # This will get dpi that is set matplotlibrc
fig.savefig("test.jpg", dpi=dpi*2)  # Multiply dpi by whatever ration you want to increase the figure

关于matplotlib - 如何在matplotlib中保持纵横比的同时增加保存图形的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29962716/

相关文章:

python - 从 DateTime 数据框列中提取时间并使用线图绘制每小时时间

python - 基于节点颜色的圆形网络集群节点

python - 如何强制 matplotlib 将 x 轴上的值显示为整数

python - 在矩形python中绘制图像

Python:填充雷达图中的区域

python - 如何增加预定义 x 范围的 x 轴上显示的厚度数量?

python - CX-卡住 "ImportError: DLL load failed: %1 is not a valid Win32 application"

python - yaxis标签不显示自定义xaxis概率尺度python

python - 向图中添加导航工具栏(matplotlib 和 PyQt4)

python - 无法在 python 可执行文件上加载 mkl_intel_thread.dll