在 matplotlib 中调整图形质量 [英] Adjusting figure quality in matplotlib

查看:43
本文介绍了在 matplotlib 中调整图形质量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一些代码来在matplotlib中生成多面板图形.为了使图形在文档中使用时看起来更清晰,我尝试增加 dpi.但是,我发现这也改变了图形,字体,线条等的大小,如以下更简单的示例所示:

I've written some code to produce a multi-panelled figure in matplotlib. To make the figure look sharper for use in a document, I tried increasing the dpi. I found, though, that this also changed the sizes of the figure, font, lines etc., as illustrated in the following simpler example:

#First normal figure
fig, axarr = plt.subplots(2,2, figsize=(6,6))
for i,ax in enumerate(axarr.flatten()):
    ax.plot(range(10))
    ax.set_title('title'+str(i), fontsize=10)
    ax.set_xlabel('x'+str(i), fontsize=10)
    ax.set_ylabel('y'+str(i), fontsize=10)
plt.tight_layout()

#Again, with higher dpi
fig, axarr = plt.subplots(2,2, figsize=(6,6), dpi=200)
for i,ax in enumerate(axarr.flatten()):
    ax.plot(range(10))
    ax.set_title('title'+str(i), fontsize=10)
    ax.set_xlabel('x'+str(i), fontsize=10)
    ax.set_ylabel('y'+str(i), fontsize=10)

plt.tight_layout()
plt.show()

在我的计算机上(使用matplotlib 1.4.3),第二个图形较大,文本较大,因此y轴标签重叠.如果不使用plt.tight_layout(),子图轴的重叠也会更糟.显然,这在试图制作更高质量的数字时没有帮助.这里发生了什么,我如何改变图形质量并保持图形的外观不变?当我调整子图参数,添加颜色条和图例等时,是否有一种解决方案也可以使用?预先感谢您提供的任何帮助.

On my computer (using matplotlib 1.4.3), the second figure is larger in size and the text is larger, so that the y-axis labels overlap. Without the use of plt.tight_layout(), the overlap of the subplot axes is also worse. Obviously, this is not helpful when just trying to make a higher quality figure. What is going on here, and how can I change the figure quality and keep the appearance of the figure constant? Is there a solution that will also work when I've adjusted subplot parameters, added colour bars and legends etc. i.e. in general? Thanks in advance for any help you can give.

推荐答案

您的问题是先显示它,然后将其保存在GUI窗口中.您必须从定义 dpi 的命令 savefig 保存图像.

Your problem is that you show it and then you save it on the GUI window. You have to save image from command savefig defining there the dpi.

像这样:

import matplotlib.pyplot as plt

#First normal figure
fig, axarr = plt.subplots(2,2, figsize=(6,6))
for i,ax in enumerate(axarr.flatten()):
    ax.plot(range(10))
    ax.set_title('title'+str(i), fontsize=10)
    ax.set_xlabel('x'+str(i), fontsize=10)
    ax.set_ylabel('y'+str(i), fontsize=10)
plt.tight_layout()
plt.savefig('myfigure_100.png', dpi=100)
plt.savefig('myfigure_200.png', dpi=200)

如果要显示,可以在scrpit末尾添加plt.show()

If you want to show it, you can at the end of the scrpit add plt.show()

编辑

图形大小的dpi和字体大小的关系可以如下例所示:

The relation of the dpi of figure size and the font size can be shown on the following example:

import matplotlib.pyplot as plt

#First normal figure
fig1, axarr1 = plt.subplots(2,2, figsize=(6,6), dpi=100)
for i,ax in enumerate(axarr1.flatten()):
    ax.plot(range(10))
    ax.set_title('title'+str(i), fontsize=10)
    ax.set_xlabel('x'+str(i), fontsize=10)
    ax.set_ylabel('y'+str(i), fontsize=10)
plt.tight_layout()

fig2, axarr2 = plt.subplots(2,2, figsize=(12,12), dpi=50)
for i,ax in enumerate(axarr2.flatten()):
    ax.plot(range(10))
    ax.set_title('title'+str(i), fontsize=20)
    ax.set_xlabel('x'+str(i), fontsize=20)
    ax.set_ylabel('y'+str(i), fontsize=20)
plt.tight_layout()

plt.show()

这篇关于在 matplotlib 中调整图形质量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆