不规则矩形网格上的python pcolor

时间:2018-08-02 07:50:06

标签: python matplotlib

我可以使用pcolor在具有矩形单元格的不规则网格上绘制数据吗?我不想绘制那些缺少数据的单元格。

example image

2 个答案:

答案 0 :(得分:0)

您可以将其视为图像:

import numpy as np 
a = 3 # height of each rectangle
b = 14 # width of each rectangle
n = 5 # number of rectangles in each direction
arr = 255*np.ones((n*a,n*b,3),dtype='uint8') #white image array

# generate random colors (RGB format) 
colors = np.random.randint(0,255,size=(n,n,3))

# now fill the image array with rectangles of color
for i in range(n):
    for j in range(n):
        arr[a*i:a*(i+1),b*j:b*(j+1)] = colors[i,j]

# and plot the resulting image 
import matplotlib.pyplot as plt 
plt.imshow(arr)
plt.show() 

enter image description here

编辑: 如果有任何单元格没有数据,则只需为它们填充新的颜色:默认情况下它们是白色的

答案 1 :(得分:0)

import numpy as np
import matplotlib.pyplot as plt

x=np.linspace(-10,10,21)
y=np.linspace(-10,10,21)
X,Y=np.meshgrid(x,y)

#synthetic data
x_c=(x[0:-1]+x[1:])*0.5#center of each cell
y_c=(y[0:-1]+y[1:])*0.5
Xc,Yc=np.meshgrid(x_c,y_c)
Z=Xc**2+Yc**2

#values in those cells without data are set as np.nan
Z[9:11,9]=np.nan
Z[0,0:3]=np.nan
Z[3:4,0]=np.nan

#plot the data
fig=plt.figure()
plt.pcolor(X,Y,Z)
plt.show()

只需将没有数据的单元格作为nan(不是数字)。

enter image description here