Matplotlib:pcolor()没有绘制最后一行和列?

时间:2014-05-08 17:57:38

标签: python matplotlib

似乎PCOLOR正在砍掉我数据集的最后一行和一列。打印下面zi的形状显示它是Data set. (22,22),正如我所料,但显示的是21平方乘以21平方的区域...任何想法为什么最后一行和列没有被绘制?

def pcolor_probs(x,y,z, x_str, y_str, t_str):
    xi = np.arange(min(x),max(x)+1, 1)
    yi = np.arange(min(y),max(y)+1, 1)
    zi = griddata(x,y,z,xi,yi)
    print np.shape(xi),np.shape(yi),np.shape(zi)

    # fix NANs
    zi = np.asarray(zi)
    for i in range(len(zi)):
        for j in range(len(zi[i])):
            print i,j
            if isnan(float(zi[i][j])):
                zi[i][j] = 0

    # plot
    f = figure()
    ax = f.add_subplot(111)
    pc_plot = ax.pcolor(zi, cmap = cm.coolwarm, shading = 'faceted', alpha = 0.75)
    # pc_plot = ax.contourf(zi, 20, cmap = cm.coolwarm, alpha = 0.75)
    ax.set_xticks(np.arange(zi.shape[0])+0.5, minor=False)
    ax.set_yticks(np.arange(zi.shape[1])+0.5, minor=False)
    ax.set_xticklabels(np.arange(len(xi)))
    ax.set_yticklabels(np.arange(len(yi)))
    ax.set_xlim(min(x), max(x))
    ax.set_ylim(min(y), max(y))
    ax.set_xlabel(x_str)
    ax.set_ylabel(y_str)
    ax.set_title(t_str)
    f.colorbar(pc_plot)


    f.set_tight_layout(True)
    font = {'family' : 'serif','weight' : 'regular','size' : 12}
    matplotlib.rc('font', **font)
    show()

让它变得更加简单,

X = np.random.rand(10,10)
pcolor(X)
show()

可生产,

enter image description here

2 个答案:

答案 0 :(得分:4)

原因是pcolor计算顶点上的点。事实上,有22和10个顶点。请改用imshow(...,extent[])

答案 1 :(得分:3)

有点晚了,但只提供一个X和Y参数,其形状只有1(在两个方向上)将显示整个数组。

类似于下面的例子:

import numpy as np
import matplotlib.pyplot as plt

#define the space limits:
horizontal_min  = -2.
horizontal_max  =  2.
horizontal_step =  0.1
vertical_min    = -1.
vertical_max    =  1.
vertical_step   =  0.2

# create the arrays
nx  = (horizontal_max - horizontal_min) / horizontal_step
ny  = ( vertical_max  -  vertical_min ) /  vertical_step
Z   = np.zeros((nx,ny))
Y,X = np.meshgrid(np.arange(vertical_min,
                            vertical_max+vertical_step, # THIS LINE...
                            vertical_step),
                  np.arange(horizontal_min,
                            horizontal_max+horizontal_step, # ...& THIS LINE
                            horizontal_step)
                  )
Y2,X2 = np.meshgrid(np.arange(vertical_min,
                              vertical_max, # THIS LINE...
                              vertical_step),
                    np.arange(horizontal_min,
                              horizontal_max, # ...& THIS LINE
                              horizontal_step)
                    )              

# populate the data array (Z)
i     = 0
if nx > ny:
    while i < ny:
        Z[i,i]      =  i+1
        Z[nx-i-1,i] = -i-1
        i          += 1
else:
    while i < ny:
        Z[i,i]      =  i+1
        Z[i,ny-i-1] = -i-1
        i          += 1


# make the graph
fig,axes     = plt.subplots(2,1)
pc_plot1 = axes[0].pcolor(X, Y, Z)
axes[0].set_title('X.shape == Y.shape != Z.shape')
pc_plot2 = axes[1].pcolor(X2, Y2, Z)
axes[1].set_title('X.shape == Y.shape == Z.shape')
for ax in axes:
    ax.axis('equal')
    ax.set_xlim(horizontal_min, horizontal_max)
    ax.set_ylim(vertical_min, vertical_max)
fig.tight_layout()
fig.show()

enter image description here 注意标有THIS LINE的行。他们的意思是:

>>> print X.shape,Y.shape,Z.shape
(41, 11) (41, 11) (40, 10)

(对于给定的例子)

只需一个小注释,使用Y,X = np.meshgrid...替换必须转置Z(参见official documentation)。