简体   繁体   English

如何使Python图像“适合”这个数字?

[英]How to make a Python image 'fit' the figure?

I have a matrix that is 500x5000 in size, and I am trying to visualize it via matplotlib. 我有一个500x5000的矩阵,我试图通过matplotlib可视化它。 Simply, I have: 简单地说,我有:

import matplotlib as plt
import numpy as np
mat = np.random.rand(500,5000)
plt.imshow(mat, interpolation='none')
plt.show()

The problem is that I get an image that is somewhat 'squeezed', as so: 问题是我得到的图像有点“挤压”,如下所示:

在此输入图像描述

I would just like to be able to make the image 'fit' the figure, so that I can inspect it better. 我希望能够使图像“适合”图形,以便我可以更好地检查它。 Is there a way to do this in matplotlib? 有没有办法在matplotlib中这样做?

Thanks. 谢谢。

If you'd like the image to adjust so that it fills up the available space, specify aspect='auto' to imshow . 如果您希望调整图像以使其填满可用空间,请指定aspect='auto'以进行imshow Note that the pixels will not be square! 请注意,像素不是正方形!

For example: 例如:

import matplotlib.pyplot as plt
import numpy as np
mat = np.random.rand(500,5000)
plt.imshow(mat, interpolation='none', aspect='auto')
plt.show()

在此输入图像描述

Just for comparison, the default behavior forces the pixels to be square, resulting is something more like this: 仅作比较,默认行为强制像素为方形,结果更像是:

在此输入图像描述

Use the set_aspect method on the current axes. 在当前轴上使用set_aspect方法。 In your case, the aspect ratio is about 1:10, so you could set it to 10 to get a square image: 在您的情况下,宽高比约为1:10,因此您可以将其设置为10以获得正方形图像:

import matplotlib.pyplot as plt
import numpy as np

mat = np.random.rand(500,5000)
plt.imshow(mat, interpolation='none')

plt.gca().set_aspect(10)
plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM