matplotlib: binary heat plot

后端 未结 1 1327
甜味超标
甜味超标 2021-01-14 17:48

Let\'s say I have a 10x10 matrix, that is comprised or just 0s and 1s, represented as a list of lists. How could I use matplotlib to represent such a matrix as

1条回答
  •  一生所求
    2021-01-14 18:02

    You need what's known as a ListedColorMap:

    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib as mpl
    
    # random data
    x = np.random.random_integers(0, 1, (10, 10))
    
    fig, ax = plt.subplots()
    
    # define the colors
    cmap = mpl.colors.ListedColormap(['r', 'k'])
    
    # create a normalize object the describes the limits of
    # each color
    bounds = [0., 0.5, 1.]
    norm = mpl.colors.BoundaryNorm(bounds, cmap.N)
    
    # plot it
    ax.imshow(x, interpolation='none', cmap=cmap, norm=norm)
    

    enter image description here

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