python pcolor on an irregular retangular grid

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



python pcolor on an irregular retangular grid



Can I use pcolor to plot data on irregular grid with rectangular cells? I don't want to plot those cells where the data are absent.



example image




2 Answers
2



You can treat it like an image:


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



edit:
if any cells don't have data, you simply don't fill them with a new color: They're white by default





Thank you for quick answer. But how to deal with the case that data are absent on some areas
– Y. Zhong
Aug 2 at 8:33





The concept is the same. Instead of looping over all image cells, you choose the cells with data in them, and fill only those. Before the nested for loops which fill arr with color, it is a white background
– kevinkayaks
Aug 2 at 8:35


arr





Thank you! It works well.
– Y. Zhong
Aug 2 at 9:07





I note that setting those cells without data to NAN is more convenient
– Y. Zhong
Aug 10 at 15:47


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



Just take the cells without data as nan (not a number).



enter image description here






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS