英文:
plt.imshow() of a single color image showing as black
问题
我正在尝试使用 plt.imshow()
显示一张浅灰色的图像,但图像变成了黑色。
我尝试了以下代码:
import matplotlib.pyplot as plt
import numpy as np
test_image = np.zeros((3871, 2484))
test_image.fill(200)
plt.imshow(test_image, cmap="gray")
plt.show()
但最终得到的结果是:
- Matplotlib 版本:3.7.1
- Numpy 版本:1.24.3
- Python 版本:3.11.3
英文:
I am trying to show a light-gray image using plt.imshow()
, but the image turns out black.
I tried:
import matplotlib.pyplot as plt
import numpy as np
test_image = np.zeros((3871, 2484))
test_image.fill(200)
plt.imshow(test_image, cmap="gray")
plt.show()
But ended up getting:
- Matplotlib version: 3.7.1
- Numpy version: 1.24.3
- Python version: 3.11.3
答案1
得分: 2
You have to include the vmin,vmax
parameters when you plot a single color image with plt.imshow(...)
. Set vmin=0
and vmax=500
to get a gray image. If vmin,vmax
are not specified, then they will be set to the min and max values of the image data. This means that all of your input data is equal to vmin
, which is the darkest possible value (black).
import matplotlib.pyplot as plt
import numpy as np
test_image = np.zeros((3871, 2484))
test_image.fill(200)
plt.imshow(test_image, cmap="gray", vmin=0, vmax=500)
plt.show()
英文:
You have to include the vmin,vmax
parameters when you plot a single color image with plt.imshow(...)
. Set vmin=0
and vmax=500
to get a gray image. If vmin,vmax
are not specified, then they will be set to the min and max values of the image data. This means that all of your input data is equal to vmin
, which is the darkest possible value (black).
import matplotlib.pyplot as plt
import numpy as np
test_image = np.zeros((3871, 2484))
test_image.fill(200)
plt.imshow(test_image, cmap="gray", vmin=0, vmax=500)
plt.show()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论