简体   繁体   中英

plotting categorical data on world map

I have data for 5 countries in this format

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

Now I want to show this data on the US on world map. For which I tried this.

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()

But the problem is that this method only plots the data in "circle". What I also want is to fill up these circles with quantitative information/"number" (font size proportional to data). How can I do that ?

For testing purpose I tried including this line before the map.plot (second last line in above code)

plt.text(lon,lat,'59%',fontsize=34,fontweight='bold',ha='center',va='center',color='b')

But even this doesnt help, because as soon as I include the above line in the code, this line map.plot(x, y, color, markersize=msize,alpha=.3) fails to give its output. Instead what I can see is only the text "59%" , thats it. I can either have the circles plotted OR the text(59% that is). But not both of them together. Why is this happening ?

This is what worked for me. Well kind of, but ok for now.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM