0

Now I have a 24*20 array as below:

In [748]: zb_mean
Out[748]: 
array([[          nan,   20.68575654,   14.11937546,           nan,
              nan,           nan,           nan,           nan,
              nan,           nan,           nan,           nan,
              nan,           nan,           nan,           nan,
              nan,           nan,           nan,           nan],
   [          nan,           nan,           nan,           nan,
              nan,           nan,           nan,           nan,
              nan,           nan,           nan,           nan,
... ... ...
... ... ...

In [749]: zb_mean.shape
Out[749]: (24, 20)

then I creat a grid according to the lat/lon as below:

xi = np.arange(-66,-72,-0.25)
yi = np.arange(40.5,45.5,0.25)

In [755]: len(xi),len(yi)
Out[755]: (24, 20)

xxb,yyb = np.meshgrid(xi, yi)

now the grid and the array have same dimension,I want to plot this grid(xxb,yyb) with the correspond value of array(zb_mean) in the center of each small grid, how should I do?thank you very much!

1 Answer 1

2

If I understand your question correct you want to plot the cells you have valid values. The trick is to use the method masked_invalid that will hide your nan values using the numpy function masked_invalid. I created a small example on how you can do this:

import numpy as np
test=np.asarray([[10,15,20,50],[30,40,nan,70],[nan,10,nan,25],[100,50,nan,60]])
test=np.ma.masked_invalid(test)

xi = np.arange(-66,-70,-1)
yi = np.arange(40.5,44.5,1)
xxb,yyb = np.meshgrid(xi, yi)

c=pcolor(xxb,yyb,test)
colorbar(c)

Hope this helps. Cheers, Trond

Sign up to request clarification or add additional context in comments.

2 Comments

tahnk you very much!!
For the record, pcolormesh would be a better choice here (it is much quicker for contiguous data).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.