python - 在 python 上的地图上绘制数据
问题描述
当我尝试显示人口数据时,没有任何内容显示为输出,但是当我评论代码以显示人口时,一切正常,那么代码中的问题是什么?
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.cm as cm
plt.figure(figsize=(25,15))
# lon_0 is central longitude of robinson projection.
# resolution = 'c' means use crude resolution coastlines.
m=Basemap(projection='mill',llcrnrlat=5,urcrnrlat=40,\
llcrnrlon=65,urcrnrlon=95,resolution='i')
#set a background colour
m.drawmapboundary(fill_color='#85A6D9')
# draw coastlines, country boundaries, fill continents.
m.fillcontinents(color='white',lake_color='#85A6D9')
m.drawcoastlines(color='#6D5F47', linewidth=.4)
m.drawcountries(color='#6D5F47', linewidth=.4)
# draw lat/lng grid lines every 30 degrees.
m.drawmeridians(np.arange(-180, 180, 5), color='#bbbbbb')
m.drawparallels(np.arange(-90, 90, 5), color='#bbbbbb')
# lat/lon coordinates of states
lats = [16.00,28.00,26.24,25.11,21.29513,28.38000,28.38000,22.30943,29.23848,32.08421,32.44000,23.45000,15.31728,10.85052,23.47332,19.60119,24.44000,25.30000,23.30000,26.00000,20.94092,30.40000,27.39128,27.30000,11.12712,23.74513,28.20761,30.15000,22.97862]
lngs = [80.00,95.00,92.53,85.32000,81.82823,77.12000,72.12000,72.13623,76.43189,77.57117,74.54000,85.30000,75.71389,76.27108,77.94800,75.55298,93.58000,91.00000,20.52000,94.20000,84.80347,75.50000,73.43262,88.30000,78.65689,91.74683,79.82666,79.15000,87.74780]
populations = [84.58,1.38,31.20,104.099452,25.545198,16.787941,1.458545,60.439692,25.351462,6.864602,12.541302,32.988134,61.095297,33.406061,72.626809,112.374333,2.855794,2.966889,1.097206,1.978502,41.974218,27.743338,68.548437,0.610577,72.14703,3.673917,199.812341,10.086292,91.276115] #millions
# compute the native map projection coordinates for cities
x,y = m(lngs,lats)
#scale populations to emphasise different relative pop sizes
s_populations = [p * p for p in populations]
#defining color
s_colors= [cm.rainbow for p in populations]
#scatter scaled circles at the city locations
m.scatter(
x,
y,
s=s_populations, #size
c='darkblue', #color
marker='o', #symbol
alpha=0.3, #transparency
zorder = 5, #plotting order
)
## plot population labels.
#for population, xpt, ypt in zip(populations, x, y):
# label_txt = int(round(population, 0)) #round to 0 dp and display as integer
# plt.text(
# xpt,
# ypt,
# label_txt,
# color = 'black',
# size='small',
# horizontalalignment='center',
# verticalalignment='center',
# zorder =3 ,
# )
#add a title and display the map on screen
plt.title('Population according to state')
plt.show()
解决方案
推荐阅读
- docker - LogStash Docker 只是在启动后停止
- python - 在 Django 中使用 Jinja2 加载 css 文件
- typescript - 在 TypeScript 中,如何在具有多个类型参数的方法中推断出泛型类型参数?
- regex - 用于查找不是结束字符的最后一个事件的正则表达式
- javascript - React.js,在所有 getDownloadURL 函数完成后打印一些东西
- spring - Spring数据Cassandra可以与动态模型和键空间一起使用吗
- visual-studio-code - VS Code设置中的变量,如何?
- python - `print(help('modules'))` 中是否缺少任何标准库模块
- bash - Perl Pack 在 Shell 变量上解包
- bash - 根据bash / posh中数组中的列匹配值将CSV拆分为两个文件