Matplotlib imshow/matshow display values on plot

后端 未结 3 597
-上瘾入骨i
-上瘾入骨i 2020-12-05 03:31

I am trying to create a 10x10 grid using either imshow or matshow in Matplotlib. The function below takes a numpy array as input, and plots the gri

3条回答
  •  忘掉有多难
    2020-12-05 03:53

    For your graph you should should try with pyplot.table:

    import matplotlib.pyplot as plt
    import numpy as np
    
    board = np.zeros((10, 10))
    board[0,0] = 1
    board[0,1] = -1
    board[0,2] = 1
    def visBoard(board):
        data = np.empty(board.shape,dtype=np.str)
        data[:,:] = ' '
        data[board==1.0] = 'X'
        data[board==-1.0] = 'O'
        plt.axis('off')
        size = np.ones(board.shape[0])/board.shape[0]
        plt.table(cellText=data,loc='center',colWidths=size,cellLoc='center',bbox=[0,0,1,1])
        plt.show()
    
    visBoard(board)
    

提交回复
热议问题