Matplotlib : What is the function of cmap in imshow?

后端 未结 1 1999
半阙折子戏
半阙折子戏 2020-12-28 20:32

I\'m trying to learn opencv using python and came across this code below:

import cv2
import numpy as np
from matplotlib import pyplot as plt

BLUE = [255,0,         


        
相关标签:
1条回答
  • 2020-12-28 21:17

    When img1 has shape (M,N,3) or (M,N,4), the values in img1 are interpreted as RGB or RGBA values. In this case the cmap is ignored. Per the help(plt.imshow) docstring:

    cmap : ~matplotlib.colors.Colormap, optional, default: None

    If None, default to rc image.cmap value. cmap is ignored when X has RGB(A) information

    However, if img were an array of shape (M,N), then the cmap controls the colormap used to display the values.


    import numpy as np
    import matplotlib.pyplot as plt
    import mpl_toolkits.axes_grid1 as axes_grid1
    np.random.seed(1)
    
    data = np.random.randn(10, 10)
    
    fig = plt.figure()
    grid = axes_grid1.AxesGrid(
        fig, 111, nrows_ncols=(1, 2), axes_pad = 0.5, cbar_location = "right",
        cbar_mode="each", cbar_size="15%", cbar_pad="5%",)
    
    im0 = grid[0].imshow(data, cmap='gray', interpolation='nearest')
    grid.cbar_axes[0].colorbar(im0)
    
    im1 = grid[1].imshow(data, cmap='jet', interpolation='nearest')
    grid.cbar_axes[1].colorbar(im1)
    plt.savefig('/tmp/test.png', bbox_inches='tight', pad_inches=0.0, dpi=200,)
    

    enter image description here

    0 讨论(0)
提交回复
热议问题