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

如何将matplotlib矩阵可视化中的最小值设为白色并调整Blues色阶从深色起始?

Visualizing a 3x3 Matrix with Custom Coloring in Matplotlib

I have a 3x3 matrix I need to visualize, using this code:

import matplotlib.cm
import matplotlib.pyplot as plt
import numpy as np
import copy
cmap = copy.copy(cm.get_cmap("Blues"))
cmap.set_bad('white')
fig = plt.figure(figsize=(15, 10))
img = np.array([[-0.9, -0.5599234, 0.21042876],[-0.42735877, 0.61514954, -0.74305015],[0.61958201, -0.04358633, 0.78672511]])
im = plt.imshow(img, origin='upper', cmap=cmap)

In the current visualization, the top-left element is the minimum value of the matrix, but it's not showing up as white. I have two questions:

  1. How can I modify the code to make the minimum value in the matrix display as white?
  2. Can I adjust the Blues colormap so it starts with a dark color and fades to light?

Answers

1. Making the minimum value white

The set_bad() method only affects values marked as "bad" (like np.nan or np.inf), not the minimum value of your data. To make the minimum value white, you have a couple of straightforward options:

  • Option 1: Mask the minimum value
    Convert the minimum value in your matrix to np.nan, which will then be rendered using the white color you set for "bad" values. Here's how to adjust your code:

    import matplotlib.cm as cm  # Fixed the missing 'as cm' reference here!
    import matplotlib.pyplot as plt
    import numpy as np
    import copy
    
    cmap = copy.copy(cm.get_cmap("Blues"))
    cmap.set_bad('white')
    fig = plt.figure(figsize=(15, 10))
    img = np.array([[-0.9, -0.5599234, 0.21042876],[-0.42735877, 0.61514954, -0.74305015],[0.61958201, -0.04358633, 0.78672511]])
    
    # Mask the minimum value with np.nan
    img_masked = img.copy()
    img_masked[img_masked == np.min(img_masked)] = np.nan
    
    im = plt.imshow(img_masked, origin='upper', cmap=cmap)
    plt.colorbar(im)  # Optional: Add colorbar to clarify the scale
    plt.show()
    

    Note: I fixed a small oversight in your original code—you were using cm.get_cmap without importing matplotlib.cm as cm.

  • Option 2: Create a custom colormap with white at the minimum
    If you don't want to modify your data, you can build a custom colormap where the lowest value maps to white. Use LinearSegmentedColormap for this:

    import matplotlib.cm as cm
    import matplotlib.pyplot as plt
    import numpy as np
    from matplotlib.colors import LinearSegmentedColormap
    
    fig = plt.figure(figsize=(15, 10))
    img = np.array([[-0.9, -0.5599234, 0.21042876],[-0.42735877, 0.61514954, -0.74305015],[0.61958201, -0.04358633, 0.78672511]])
    
    # Extract colors from the original Blues colormap
    blues_colors = cm.get_cmap("Blues")(np.linspace(0, 1, 256))
    # Set the first color (minimum value) to white (RGBA format)
    blues_colors[0] = [1, 1, 1, 1]
    custom_cmap = LinearSegmentedColormap.from_list("CustomBlues", blues_colors)
    
    im = plt.imshow(img, origin='upper', cmap=custom_cmap)
    plt.colorbar(im)
    plt.show()
    

2. Reversing the Blues colormap to start dark

Absolutely! Most matplotlib colormaps (including Blues) have a built-in reversed version, denoted by adding _r to the end of the colormap name. Just replace "Blues" with "Blues_r" in your code:

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

fig = plt.figure(figsize=(15, 10))
img = np.array([[-0.9, -0.5599234, 0.21042876],[-0.42735877, 0.61514954, -0.74305015],[0.61958201, -0.04358633, 0.78672511]])

# Use reversed Blues colormap to start dark
im = plt.imshow(img, origin='upper', cmap="Blues_r")
plt.colorbar(im)
plt.show()

If you want to combine this with the minimum-value-white tweak from question 1, just apply both changes together (e.g., use Blues_r in the custom colormap or mask approach).


内容的提问来源于stack exchange,提问作者machinery

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 16:42:50