You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何np.zeros()未指定通道数生成紫色图像?三类场景答疑

Understanding plt.imshow() Behavior with Zero-Initialized Arrays

Let's walk through each of your scenarios to clear up why you're seeing purple vs black, and answer your specific questions in plain terms:


Scenario 1: 2D np.int8 Array

import numpy as np
import matplotlib.pyplot as plt

# Create 10x10 matrix with all zeros
img = np.zeros((10, 10), dtype=np.int8)
plt.imshow(img)
print(img)

Output:
Purple 10x10 image

Answers to Question 1:

  • Why purple instead of black?
    plt.imshow() treats 2D arrays as single-channel grayscale data, but it uses a default color map (usually viridis) unless you tell it otherwise. For np.int8 data, the value range is -128 to 127—0 isn't the lowest value here, so it maps to a middle color in the viridis map (which happens to be purple). If you used dtype=np.uint8 (range 0-255), 0 would be the minimum, but you'd still need to specify cmap='gray' to get black instead of a colored map.
  • Default channel count without a third dimension?
    A 2D array is interpreted as a 1-channel (grayscale) image. There's no hidden extra channel—just one intensity value per pixel.
  • How many channels does the purple image actually have?
    It's still a 1-channel array. The purple color comes from the color map applied to the single intensity channel, not multiple color channels.

Scenario 2: 3D Array with 1 Channel

import numpy as np
import matplotlib.pyplot as plt

# Create 2x2x1 matrix with all zeros
img = np.zeros((2,2,1), dtype=np.int8)
plt.imshow(img)
print(img)

Output:
Purple 2x2 image

Answers to Question 2:

  • Channel count when no third dimension is specified?
    Same as Scenario 1: 1 channel (grayscale). The 2D shape (H,W) is equivalent to single-channel grayscale data.
  • Why still purple with a 1-channel array?
    Adding the third dimension of size 1 doesn't change how plt.imshow() reads the data—it still sees it as single-channel intensity values, using the default viridis color map. Again, since np.int8's 0 isn't the minimum value in its range, it doesn't map to black. To get a black image here, you have two options:
    • Add cmap='gray' to use a grayscale color map, and switch to dtype=np.uint8 so 0 is the lowest possible value, or
    • Scale your data to the 0-1 or 0-255 range that plt.imshow() expects for proper grayscale rendering.

Scenario 3: 3D RGB Array

import numpy as np
import matplotlib.pyplot as plt

# Create 2x2x3 matrix with all zeros
img = np.zeros((2,2,3), dtype=np.int8)
plt.imshow(img)
print(img)

Output:
Black 2x2 image

Answer to Question 3:

Your understanding is 100% correct! A 3-channel array with shape (H,W,3) is interpreted as an RGB image, where each channel corresponds to red, green, and blue intensity. A value of 0 in all three channels means no light is emitted for any color—this results in pure black.

Quick note: While RGB values are typically stored as uint8 (range 0-255), plt.imshow() handles int8 data by clamping values to valid ranges. Since 0 falls within both -128 to 127 and 0-255, it works perfectly here to show black.


内容的提问来源于stack exchange,提问作者F.C. Akhi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 16:40:50