2014-02-11 203 views
12

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

谢谢!

注意:下面的代码没有从数组中取值,我只是在玩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) 

输出:

enter image description here

+1

使用批注可以让您非常灵活地指定文本的坐标。 – tacaswell

+0

我想知道是否有办法做到这一点,而不指定坐标。到目前为止,我手动完成了它(可能不是最聪明的想法)假设数字大小可能会改变,我将不得不提出一个计算正确坐标的函数。 – marillion

回答

21

你可以这样做:

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() 

enter image description here


编辑:

以下是带有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() 

enter image description here

+0

谢谢!我将尝试对此进行迭代。我必须使用imshow/matshow绘图作为基础,因为它会显示热图。我打算在热图上有值。不过,我想我可以覆盖你在imshow/matshow图上的内容。让我们试试... – marillion

+0

@marillion,检查编辑。 – wflynny

+0

感谢您的代码!请注意,最终的图像可能会倒过来显示。根据您提供给['imshow']的参数,可以通过交换set_ylim:ax.set_ylim(bottom = max_val - diff/2,top = min_val - diff/2) –

2

为了您的图形,你应该应该尝试与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) 
+0

有没有办法将这张表覆盖在imshow/matshow图上?我必须保留那些将被用作热图的东西。我只需要将'x''o'覆盖在它上面。 – marillion

+0

是的,我可以做一些类似于plt的事情。绘图(范围(10),范围(10))',我看到表格下方的图表,表格就像另一个图表。 –

2

上@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)