2

I have zero background in programming. I am trying to code something for my class and it is giving me an error "not enough values to unpack (expected 2, got 1)". What might be wrong with my codes?

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import shapefile as shp

#Read x, y, z file
data = pd.read_excel('RegionV.xlsx', header = None)

# Get X, Y, Z values from file
data = np.array(data)
data = data.astype(np.float)
x = data[:,0]
y =  data[:,1]
z = data[:,2]

#Mask negative values of z (bathemetry)
zz = np.ma.masked_where(z <= 0, z)


#Create a map using basemap
fig = plt.figure(figsize=(10,10))
map = Basemap(projection='mill',llcrnrlat=11,urcrnrlat=15,\
            llcrnrlon=122,urcrnrlon=125,resolution='h')

lon = np.linspace(min(x), max(x))
lat = np.linspace(min(y), max(y))

xs, ys = np.meshgrid(lon, lat)
x, y = map(xs, ys)


map.drawparallels(np.arange(12.,14.,0.5), labels=[0,0,0,1])
map.drawmeridians(np.arange(123.,126.,0.5), labels=[1,0,0,0])

#Plot 
cmap = plt.cm.jet
cmap.set_bad(color='white')
m.pcolormesh(x, y, zz, cmap=cmap, vmax=1300, vmin=0) 
m.colorbar(location='right', label='meters')


map.drawmapboundary()
map.drawcoastlines()

the first part of the error message says:

ValueError                                Traceback (most recent call last)
    <ipython-input-50-3d7531011dc2> in <module>
         44 cmap = plt.cm.jet
         45 cmap.set_bad(color='white')
    ---> 46 m.pcolormesh(xs, ys, zz, cmap=cmap, vmax=1300, vmin=0)
         47 m.colorbar(location='right', label='meters')

then, at the end,

ValueError: not enough values to unpack (expected 2, got 1)

Stackoverflow is not allowing me to post the entire error message. Hope everyone who see my post understands what I mean.

  • 1
    you should paste the *exact error message* in your question, don't just paraphrase it. The error message will tlell you (and everyone else) the exact line number where the probelm is. hint: there may be something where you've got two things on the left of an equal sign but the thing on the right is only one thing that can't be "unpacked". Google "python packing/unpacking". Once you identify the line number, try adding a print line for the thing on the right and see what you get. – uhoh Sep 12 '19 at 10:09
  • 1
    Also, for best results, once you post a question, ideally it's best to stay online because people may ask questions quite quickly, and if you don't respond your question could get put on hold as "unclear what you're asking" in which case no answers can be posted. – uhoh Sep 12 '19 at 10:13

1 Answers1

1

I think that the problem is with line x, y = map(xs, ys). Look at this page for the documentation of the Basemap() function and the example usage (search for ".basemap" keyword). It says that the example usage is:

# create Basemap instance for Robinson projection.
m = Basemap(projection='robin',lon_0=0.5*(lons[0]+lons[-1]))
# compute map projection coordinates for lat/lon grid.
x, y = m(*np.meshgrid(lons,lats))

You try to get x,y from Basemap() instance, which is only one object. That's why it says that it expected 2 values (x, y), but got only one (Basemap()).

eerio
  • 43
  • 2
  • 6