繁体   English   中英

Matplotlib imshow/matshow 在绘图上显示值

[英]Matplotlib imshow/matshow display values on plot

我正在尝试使用imshowmatshow创建一个 10x10 网格。 下面的函数将一个 numpy 数组作为输入,并绘制网格。 但是,我希望数组中的值也显示在网格定义的单元格内。 到目前为止,我找不到合适的方法来做到这一点。 我可以使用plt.text在网格上放置东西,但这需要每个单元格的坐标,完全不方便。 有没有更好的方法来做我想要完成的事情?

谢谢!

注意:下面的代码还没有从数组中获取值,我只是在玩plt.text

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import colors

board = np.zeros((10, 10))

def visBoard(board):
   cmap = colors.ListedColormap(['white', 'red'])
   bounds=[0,0.5,1]
   norm = colors.BoundaryNorm(bounds, cmap.N)
   plt.figure(figsize=(4,4))
   plt.matshow(board, cmap=cmap, norm=norm, interpolation='none', vmin=0, vmax=1)
   plt.xticks(np.arange(0.5,10.5), [])
   plt.yticks(np.arange(0.5,10.5), [])
   plt.text(-0.1, 0.2, 'x')
   plt.text(0.9, 0.2, 'o')
   plt.text(1.9, 0.2, 'x')
   plt.grid()

   visBoard(board)

输出:

在此处输入图片说明

你能做这样的事情:

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots()

min_val, max_val = 0, 10
ind_array = np.arange(min_val + 0.5, max_val + 0.5, 1.0)
x, y = np.meshgrid(ind_array, ind_array)

for i, (x_val, y_val) in enumerate(zip(x.flatten(), y.flatten())):
    c = 'x' if i%2 else 'o' 
    ax.text(x_val, y_val, c, va='center', ha='center')
#alternatively, you could do something like
#for x_val, y_val in zip(x.flatten(), y.flatten()):
#    c = 'x' if (x_val + y_val)%2 else 'o'

ax.set_xlim(min_val, max_val)
ax.set_ylim(min_val, max_val)
ax.set_xticks(np.arange(max_val))
ax.set_yticks(np.arange(max_val))
ax.grid()

在此处输入图片说明


编辑:

这是一个带有imshow背景的更新示例。

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots()

min_val, max_val, diff = 0., 10., 1.

#imshow portion
N_points = (max_val - min_val) / diff
imshow_data = np.random.rand(N_points, N_points)
ax.imshow(imshow_data, interpolation='nearest')

#text portion
ind_array = np.arange(min_val, max_val, diff)
x, y = np.meshgrid(ind_array, ind_array)

for x_val, y_val in zip(x.flatten(), y.flatten()):
    c = 'x' if (x_val + y_val)%2 else 'o'
    ax.text(x_val, y_val, c, va='center', ha='center')

#set tick marks for grid
ax.set_xticks(np.arange(min_val-diff/2, max_val-diff/2))
ax.set_yticks(np.arange(min_val-diff/2, max_val-diff/2))
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.set_xlim(min_val-diff/2, max_val-diff/2)
ax.set_ylim(min_val-diff/2, max_val-diff/2)
ax.grid()
plt.show()

在此处输入图片说明

对于您的图表,您应该尝试使用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)

对@wflynny 的代码进行了一些详细说明,使其成为一个函数,该函数接受任何大小的矩阵并绘制其值。

import numpy as np
import matplotlib.pyplot as plt

cols = np.random.randint(low=1,high=30)
rows = np.random.randint(low=1,high=30)
X = np.random.rand(rows,cols)

def plotMat(X):
    fig, ax = plt.subplots()
    #imshow portion
    ax.imshow(X, interpolation='nearest')
    #text portion
    diff = 1.
    min_val = 0.
    rows = X.shape[0]
    cols = X.shape[1]
    col_array = np.arange(min_val, cols, diff)
    row_array = np.arange(min_val, rows, diff)
    x, y = np.meshgrid(col_array, row_array)
    for col_val, row_val in zip(x.flatten(), y.flatten()):
        c = '+' if X[row_val.astype(int),col_val.astype(int)] < 0.5 else '-' 
        ax.text(col_val, row_val, c, va='center', ha='center')
    #set tick marks for grid
    ax.set_xticks(np.arange(min_val-diff/2, cols-diff/2))
    ax.set_yticks(np.arange(min_val-diff/2, rows-diff/2))
    ax.set_xticklabels([])
    ax.set_yticklabels([])
    ax.set_xlim(min_val-diff/2, cols-diff/2)
    ax.set_ylim(min_val-diff/2, rows-diff/2)
    ax.grid()
    plt.show()

plotMat(X)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM