matplotlib matshow labels

前端 未结 2 759
被撕碎了的回忆
被撕碎了的回忆 2021-01-30 23:14

I start using matplotlib a month ago, so I\'m still learning.
I\'m trying to do a heatmap with matshow. My code is the following:

data = numpy.array(a).resha         


        
2条回答
  •  春和景丽
    2021-01-30 23:24

    The other way to do this is to specify the ticks, and then set the corresponding labels. Then you don't have to worry about the extra out of bounds tick. This comes up in lots of matplotlib demos. So here, note the extra calls the ax.set_xticks and ax.set_yticks

    import numpy as np
    import matplotlib.pyplot as plt
    
    alpha = ['ABC', 'DEF', 'GHI', 'JKL']
    
    data = np.random.random((4,4))
    
    fig = plt.figure()
    ax = fig.add_subplot(111)
    cax = ax.matshow(data, interpolation='nearest')
    fig.colorbar(cax)
    
    xaxis = np.arange(len(alpha))
    ax.set_xticks(xaxis)
    ax.set_yticks(xaxis)
    ax.set_xticklabels(alpha)
    ax.set_yticklabels(alpha)
    
    plt.show()
    

提交回复
热议问题