Daan Kuitenbrouwer
Daan Kuitenbrouwer

Reputation: 45

Empty figures with basemap

I am trying to use model output on flows in a tidal basin. The model uses a curvilinear grid. My first task is to just plot one component of the velocity of the highest water layer. I wrote a little bit of code based on the question under the name: Matplotlib Streamplot for Unevenly (curvilinear) Grid.

Now as far as I can see, I didn't change anything essential except for the numbers in comparison to the earlier metioned question, but the figures remain empty. I put the code and some numbers below.

import numpy as np
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt

Lat = np.array([[ 30.40098833,  30.40103752,  30.40108727,  30.40113704],
 [ 30.40140046,  30.40145021,  30.40149997,  30.40154973],
 [ 30.40186559,  30.40191478,  30.40196453,  30.4020143 ],
 [ 30.40239781,  30.402447,    30.40249676,  30.40254652]])

Lon = np.array([[-86.51729818, -86.51794126, -86.5185871,  -86.51923603],
 [-86.51725858, -86.51790149, -86.51854717, -86.51919595],
 [-86.51721383, -86.51785659, -86.51850228, -86.51915089],
 [-86.51716242, -86.51780518, -86.51845087, -86.51909948]])

Xvel = np.array([[ 0.0325774,  -0.02811189, -0.04972513, -0.07736091],
 [ 0.00592685, -0.00043959, -0.00735147, -0.05015078],
 [-0.03365543, -0.03183309, -0.03701356, -0.07232581],
 [-0.09578606, -0.10139448, -0.11220678, -0.13221299]])


plt.ion()
fig,(ax1) = plt.subplots(1,1)

m = Basemap(llcrnrlon=Lon.min(),llcrnrlat=Lat.min(),
urcrnrlon=Lon.max(), urcrnrlat=Lat.max(),
projection='merc',resolution='i',ax=ax1)

m.contourf(Lat,Lon,Xvel,latlon=True)
m.drawcoastlines()
m.drawrivers()
m.plot(Lat,Lon,'-k',alpha=0.3,latlon=True)
m.plot(Lat.T,Lon.T,'-k',alpha=0.3,latlon=True)

Could someone tell me what it is that causes the plots to remain empty?

I have another question regarding the use of Basemap: My datasheet also contains a lot of NaN's (gridpoints with no information). I was wondering how I can let Basemap know that I just don't have any information on these positions and that I don't want any plotting there. In the current code it causes an 'Points of LinearRing do not form a closed linestring' error.

Upvotes: 1

Views: 553

Answers (1)

hugke729
hugke729

Reputation: 351

Regarding the second part of your question (since Ajean appears to have solved the first half), the standard way to tell Matplotlib (and hence Basemap) to not plot data is to create a masked array. Lets say your Xvel contained NaNs, then to plot it you would do

import numpy.ma as ma
m.contourf(Lon, Lat, ma.masked_invalid(Xvel), latlon=True)

the function ma.masked_invalid, as its name implies, masks all invalid (i.e., NaN) values, so that they're not plotted.

Upvotes: 1

Related Questions