强制pyplot.imshow()生成更高分辨率的图像 [英] Force pyplot.imshow() to produce image with higher resolution

查看:3825
本文介绍了强制pyplot.imshow()生成更高分辨率的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NxN数组,我使用 matplotlib.pyplot.imshow()在Python中绘图。 N将非常大,我希望我的最终图像具有匹配的分辨率。但是,在随后的代码中,图像分辨率似乎没有随着N的增加而改变。我认为 imshow()(至少我是如何使用它的)具有固定的最小像素大小,该大小比显示具有全分辨率的NxN阵列所需的大。

I have an NxN array that I am plotting in Python using matplotlib.pyplot.imshow(). N will be very large and I want my final image to have resolution to match. However, in the code that follows, the image resolution doesn't seem to change with increasing N at all. I think that imshow() (at least how I'm using it) has a fixed minimum pixel size that is larger than that needed to show my NxN array with full resolution.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm

array = np.loadtxt("output.dat",unpack=True)
plt.figsize=(30.0, 30.0)
im = plt.imshow(array,cmap='hot')
plt.colorbar(im)
plt.savefig("mandelbrot.pdf")

正如您在上面的代码中所看到的,我已经尝试弄乱 plt.figsize 来尝试增加分辨率,但无济于事。我也尝试了各种输出格式(.pdf,.ps,.eps,.png)但这些都生成了比我想要的分辨率更低的图像。 .ps,.eps和.pdf图像看起来完全相同。

As you can see in the code above, I've tried messing with plt.figsize to try and increase resolution but to no avail. I've also tried various output formats (.pdf, .ps, .eps, .png) but these all produced images with lower resolution than I wanted. The .ps, .eps, and .pdf images all looked the exact same.

首先,我的问题是否存在 imshow()或者是否需要更改我的代码的其他方面才能生成更高分辨率的图像?

First, does my problem exist with imshow() or is there some other aspect of my code that needs to be changed to produce higher resolution images?

其次,如何生成更高分辨率的图像?

Second, how do I produce higher resolution images?

推荐答案

plt.figsize()只会改变大小以英寸为单位,同时保持默认dpi。您可以通过在保存数字时传递 dpi 关键字参数来设置数字的分辨率:

plt.figsize() will only change the size of the figure in inches while keeping the default dpi. You can set the resolution of the figure by passing the dpi keyword argument when you save the figure:

    fig.savefig('filename.extension', dpi=XXX)

因此,如果您的数字大小为4x6并使用 dpi = 300 保存,则最终会得到1200x1800分辨率的图像。

So if you have a figure size of 4x6 and save it with dpi=300 you'll end up with an image with 1200x1800 resolution.

您还可以使用 <$ c $设置默认数字大小和dpi c> matplotlibrc

You can also set the default figure size and dpi with matplotlibrc.

这篇关于强制pyplot.imshow()生成更高分辨率的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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