底图:绘制带有数据的国家和气泡的世界地图

时间:2018-01-24 14:17:31

标签: python pandas matplotlib matplotlib-basemap

I am trying to create a world map in background and bubble on top of it to show the data. I am using code below to create it but this gives a map in background with out country names and plane circle which doesnt show the location of country.

将pandas导入为pd     来自mpl_toolkits.basemap导入底图     将matplotlib.pyplot导入为plt

# Set the dimension of the figure
my_dpi=10
plt.figure(figsize=(2600/my_dpi, 1800/my_dpi), dpi=my_dpi)

# read the data (on the web)


# Make the background map

m=Basemap(llcrnrlon=-180, llcrnrlat=-65,urcrnrlon=90,urcrnrlat=80)
m.drawmapboundary(fill_color='#A6CAE0', linewidth=0)
m.fillcontinents(color='grey', alpha=0.3)
m.drawcoastlines(linewidth=0.1, color="white")

# prepare a color for each point depending on the continent.
#data['labels_enc'] = pd.factorize(data['homecontinent'])[0]

# Add a point per position
m.scatter(conversion_comparison['Res'],conversion_comparison['Sea'],
          s=1000, alpha=1.0, c=colors)

具有转换率数据的数据框

data conversion_comparison dataframe:

Country         Sea     Res  ConvRate(%)    Country_codes   
Spain           6179    85      1.38                ES  
United Kingdom  495     99      2.00                GB  
France          473     12      2.55                FR  
United States   442     7.8     1.76                US  
Italy           358     7.4     2.07                IT  
Germany         153     3.3     2.15                DE  
Argentina       135     1.9     1.41                AR  
Ireland         132     3.3     2.49                IE  
Belgium         122     4.3     3.51                BE  
Israel          109     2.2     1.82                IL  

I want bubbles to have country code and converson rate and size of bubble based on conversion rate value

请建议在代码中进行修改以创建地图。 我还附加了最终输出的图像。

[Output i am getting][1]
  [1]: https://i.stack.imgur.com/XagnV.png

[output i want][2]  
[2]: https://i.stack.imgur.com/PVFX6.jpg

1 个答案:

答案 0 :(得分:1)

仍然无法完全回答你的问题,因为你的例子并不可行,但关键在于你m.scatter的号召,因为lat缺乏国家的坐标而且没有#39} ; t为气泡传递正确的尺寸。

首先,您需要的是数据集中所有国家/地区的纬度/经度坐标列表 - 还有另一个StackOverflow question here,它提供了一些获取此值的选项。

然后,您需要将这些坐标合并到现有数据集上。假设您已成功完成此操作并且坐标位于数据集的lonm.scatter列中,则可以按如下方式调用m.scatter(conversion_comparison['lat'],conversion_comparison['lon'], s=conversion_comparison['ConvRate(%)'], alpha=1.0, c=colors)

labels = conversion_comparison.Country.values 
    for label, xpt, ypt in zip(labels, conversion_comparison.lon.values, conversion_comparison.lat.values):
        plt.annotate(label, xy=m(xpt, ypt), xycoords="data", backgroundcolor="w",
                    xytext=(1,1), textcoords='offset points') 

如果您还想为气泡添加标签,可以执行以下操作:

def mainView(request):
    return render(request,'index.html')

def returnVideo(request):
    pyrax.set_setting('identity_type', 'rackspace')
    pyrax.set_credentials(USERNAME, API_KEY)
    cp = pyrax.cloudfiles
    container = cp.get_container('test')
    object = container.get_object('test.mp4')
    file = object.fetch()
    response = HttpResponse(file,content_type='video/mp4')
    response['Content-Disposition'] ='filename=myfile.mp4;inline'
    response['Content-Length'] = object.total_bytes
    return response

(您可能需要在上面稍微调整一下偏移量)