首页 > 解决方案 > 在 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()

标签: pythonmatplotlib

解决方案


推荐阅读