在世界地图上绘制分类数据

时间:2014-09-23 08:11:35

标签: python maps

我有5种国家/地区的数据

____USA.csv____
    lats, long, users
    37.3264,    -117.1416,  5334,
    37.3264,    -117.1416,  435,

现在我想在世界地图上显示美国的这些数据。 我试过这个。

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np

# read input csv file
filename = 'D:/22_sept_14/USA.csv'
lats=[]
lons=[]
population=[]
with open(filename) as f:
    reader = csv.reader(f)
    for row in reader:
        lats.append(float(row[0]))
        lons.append(float(row[1]))
        population.append(float(row[2]))



map = Basemap(projection='robin', resolution = 'l', area_thresh = 1000.0,
              lat_0=0, lon_0=-130)
map.drawcoastlines()
map.drawcountries()
map.fillcontinents(color = 'gray')
map.drawmapboundary()
map.drawmeridians(np.arange(0, 360, 30))
map.drawparallels(np.arange(-90, 90, 30))

min_marker_size = 2.5
for lon, lat,pop in zip(lons, lats, population):
    x,y = map(lon, lat)
    if pop>1000:
        color='ro'
    elif pop<1000:
        color='yo'
    msize = pop * .01* min_marker_size
    map.plot(x, y, color, markersize=msize,alpha=.3)

plt.show()

但问题是这种方法只能在“圆圈”中绘制数据。我还想要用定量信息/“数字”(字体大小与数据成比例)填充这些圆圈。我怎么能这样做?

出于测试目的,我尝试在map.plot(上面代码中的第二行)

之前包含这一行
plt.text(lon,lat,'59%',fontsize=34,fontweight='bold',ha='center',va='center',color='b')

但即使这样也无济于事,因为只要我在代码中包含上述行,此行map.plot(x, y, color, markersize=msize,alpha=.3)就无法提供输出。相反,我能看到的只是文字“59%”,就是这样。 我可以绘制圆圈或文本(59%)。但不是他们俩在一起。为什么会这样?

1 个答案:

答案 0 :(得分:2)

这对我有用。好吧,但现在还可以。

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np
import csv

mark_font=3
lats=lons=population=[]
filename = 'D:/22_sept_14/mvsf.csv'
with open(filename) as f:
    reader = csv.reader(f)
    for row in reader:
        lats.append(float(row[0]))
        lons.append(float(row[1]))
        population.append(float(row[2]))

m = Basemap(projection='robin', resolution = 'l', area_thresh = 10000.0,
              lat_0=0, lon_0=0)
m.drawcoastlines()
m.drawcountries()
m.fillcontinents(color = 'gray')
m.drawmapboundary(fill_color='aqua')
m.drawmeridians(np.arange(0, 360, 30))
m.drawparallels(np.arange(-90, 90, 30))
# fill continents, set lake color same as ocean color.
# draw parallels and meridians.
# label parallels on right and top
# meridians on bottom and left
parallels = np.arange(0.,81,10.)
# labels = [left,right,top,bottom]
m.drawparallels(parallels,labels=[False,True,True,False])
meridians = np.arange(10.,351.,20.)
m.drawmeridians(meridians,labels=[True,False,False,True])
# plot blue dot on Boulder, colorado and label it as such.
lon, lat = -100.237, 49.125 # Location of Boulder
# convert to map projection coords.
# Note that lon,lat can be scalars, lists or numpy arrays.
xpt,ypt = m(lon,lat)
# convert back to lat/lon
lonpt, latpt = m(xpt,ypt,inverse=True)
m.plot(xpt,ypt,'go',markersize=17*mark_font,alpha=.8)  # plot a blue dot there
# put some text next to the dot, offset a little bit
# (the offset is in map projection coordinates)
plt.text(xpt,ypt,'17%' , fontsize=17,color='w')


lon, lat = -104.237, 40.125 # Location of Boulder
# convert to map projection coords.
# Note that lon,lat can be scalars, lists or numpy arrays.
xpt,ypt = m(lon,lat)
# convert back to lat/lon
lonpt, latpt = m(xpt,ypt,inverse=True)
m.plot(xpt,ypt,'ro',markersize=16*mark_font,alpha=.8)  # plot a blue dot there
# put some text next to the dot, offset a little bit
# (the offset is in map projection coordinates)
plt.text(xpt,ypt,'16%' , fontsize=16,color='w')

plt.show()