【问题标题】:Basemap: plot a world map with countries and bubbles with data底图:绘制带有国家和气泡的世界地图和数据
【发布时间】:2018-07-03 14:59:40
【问题描述】:
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.

将熊猫导入为 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)

带有转化率数据的数据框

数据转换_比较数据框:

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

【问题讨论】:

  • 你能提供一个最小的工作示例来重现你的工作吗?您对m.scatter 的输入似乎是错误的,但无法分辨conversion comparison[["Res","Sea"]] 中的真正内容
  • 我现在已经添加了数据框。我还添加了我想要输出格式的图像。
  • 您完全没有绘制数据位置的坐标。例如。西班牙将位于北纬 40° 和西纬 4°。如果没有这些数据,matplotlib 将不知道在哪里绘制点。
  • 但是我的数据中可能有其他国家,所以我如何传递坐标以便我可以绘制所有国家,而不仅仅是西班牙

标签: python pandas matplotlib matplotlib-basemap


【解决方案1】:

仍然无法完全回答您的问题,因为您的示例不太可行,但关键在于您对 m.scatter 的调用,它缺少国家/地区的坐标并且没有传递正确的气泡大小。

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

然后您需要将这些坐标合并到您现有的数据集中。假设您已成功完成此操作,并且坐标位于数据集的 latlon 列中,则可以按如下方式调用 m.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') 

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

【讨论】:

  • 好的,所以如果我将 lat 和 lon 作为坐标传递,那么我如何获取 res 的值并在气泡中搜索。或者我可以在气泡中传递一些文本来打印气泡中的值。
  • 标签与annotate 函数一起使用,在上面的答案中添加了一个示例
  • 如果我的回答解决了您的问题,请考虑接受它以关闭问题。
  • 是的。我正在应用解决方案。现在我没有纬度和经度数据,所以我正在尝试生成它。一旦我有了,我将应用解决方案。
  • 除了我在回答中链接的主题之外,还有一张表here 可能会有所帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-06
  • 1970-01-01
  • 2013-01-13
相关资源
最近更新 更多