我的编程背景为零。我正在尝试为我的班级编写代码,这给我一个错误“没有足够的值要解压(预期2,得到1)”。我的代码可能有什么问题?
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()
错误消息的第一部分说:
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')
然后,最后,
ValueError: not enough values to unpack (expected 2, got 1)
Stackoverflow不允许我发布整个错误消息。希望看到我的帖子的每个人都明白我的意思。
答案 0 :(得分:0)
我认为问题出在第x, y = map(xs, ys)
行。在此页面上查看Basemap()函数的文档和用法示例(搜索“ .basemap”关键字)。它说示例用法是:
# 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))
您尝试从仅一个对象的Basemap()实例获取x,y。这就是为什么它说它期望2个值(x,y),但只得到一个(Basemap())。