0

I have a csv file with 3 columns (longitude, latitude and precipitation) and 90 rows and I need to plot it as a map in Python. I have not a time index, only value associated with coordinates. I have followed this procedure:

  1. Convert .cvs to .nc (netCDF file)
  2. Plot .nc thought Basemap + Matplotlib

In the second step, I got the follow error:

ValueError: need more than 1 value to unpack

Which seems to be in line c_scheme = mp.pcolor(x,y,avp[:], cmap = 'jet') from the code, especially avp[:]. When I run avp.shape I get it have a shape of (90L,) and avp.ndim show a value of 1.

My questions are: What values should be displayed in "avp" exactly? What should I extract from this dimension?

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

data = Dataset(r'SN1_PTPM.nc')

fig = plt.figure(figsize=(12,9))

lats = data.variables['Latitude'][:] #latitute column
lons = data.variables['Longitude'][:] #longitute column
avp = data.variables['Precipitation'][:] #precipitation column

mp = Basemap(projection = 'merc',
            llcrnrlon = 5.5,
            llcrnrlat = 8.5,
            urcrnrlon = -75,
            urcrnrlat = -72,
            resolution = 'i')

lon,lat = np.meshgrid(lons,lats)
x,y = mp(lon,lat)

c_scheme = mp.pcolor(x,y,avp[:], cmap = 'jet')
cbar = mp.colorbar(c_scheme, location = 'right', pad = '10%')

plt.show()

I thank you in advance for any information you can give me.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
vegoma
  • 3
  • 1

1 Answers1

0

I imagine the issue is your coordinates are tuples (lat,lon) associated with one value each. In that case you can't use meshgrid because that will create a grid of coordinates but you don't have values at all of those points (you create a 2 dimensional grid but only have a 1 dimensional list of values)

Jtradfor
  • 96
  • 3
  • That's right. I solved the problem by converting avp (a one-dimensional file) to the same dimensions as latitude and longitude (which seems to be requirement for interpolation methods like pcolormesh, pcolor and contourf). More details of this process can be found in: https://stackoverflow.com/questions/26872337/how-can-i-get-my-contour-plot-superimposed-on-a-basemap. – vegoma Dec 17 '20 at 01:17
  • Also, I found out that my procedure (and code) was kind of redundant. There is not necessary to convert .cvs to netcdf in order to create a interpolated map throught basemap in python. Just having coordinate (x, y) associate with a value (z) you can make an interpolated map in basemap. – vegoma Dec 17 '20 at 01:27