`plt.imshow()` 用于显示单一颜色图像,该图像显示为黑色。

huangapple go评论69阅读模式
英文:

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()

但最终得到的结果是:

`plt.imshow()` 用于显示单一颜色图像,该图像显示为黑色。

  • 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:

`plt.imshow()` 用于显示单一颜色图像,该图像显示为黑色。

  • 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()

huangapple
  • 本文由 发表于 2023年5月18日 01:38:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76274802.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定