根据相关数据(人群)设置每个点(lon,lat)的标记大小会产生错误:float()参数必须是字符串或数字,而不是' list'。
根据我对zip的理解,该函数应该一次只调用一个数字(索引),然后迭代到下一个。如果我将标记设置为常量(例如markersize = 10),则一切正常。
# 1. get data
lon = [-122.2416355, -122.2977475, -121.204408, -118.3272612, -119.0194639]
lat = [37.7652076, 37.88687, 40.2362738, 33.34221, 35.3738712]
crowd = [8.0, 500.0, 4.0, 44.0, 119.0]
# 2. draw map
map = Basemap(projection='lcc', resolution='h',
lat_0=37.5, lon_0=-119,
width=1E6, height=1.2E6)
map.drawcoastlines()
map.drawcountries()
map.drawstates()
map.fillcontinents()
map.drawmapboundary()
x,y = map(lon, lat)
for x, y, c in zip(lon, lat, crowd):
x,y = map(lon, lat)
map.plot(x, y, 'bo', markersize=crowd)
plt.show()
答案 0 :(得分:0)
工作代码应为:
import matplotlib.pyplot as plt
import numpy
from mpl_toolkits.basemap import Basemap
# 1. get data
lon = [-122.2416355, -122.2977475, -121.204408, -118.3272612, -119.0194639]
lat = [37.7652076, 37.88687, 40.2362738, 33.34221, 35.3738712]
crowd = [8.0, 500.0, 4.0, 44.0, 119.0]
# 2. draw map
map = Basemap(projection='lcc', resolution='h',
lat_0=37.5, lon_0=-119,
width=1E6, height=1.2E6)
map.drawcoastlines()
map.drawcountries()
map.drawstates()
map.fillcontinents()
map.drawmapboundary()
x,y = map(lon, lat) # convert (long-lat) degrees to map coords
for x1, y1, c in zip(x, y, crowd):
# markersize is scale down by /10
# need alpha<1 to get some transparency
# red color is more appropriate
map.plot(x1, y1, 'ro', markersize=c/10., alpha=0.4)
plt.show()
结果图: