2016-03-18 53 views
-1

无法删除问题。请参考问题:Shade states of a country according to dictionary values with Basemap使用python在国家地图上绘制数据的最简单方法

我想绘制每个墨西哥州的数据(某一年的患病人数)。 我正在使用jupyter笔记本。 到目前为止,我已经看到了几个选项和教程,但似乎没有人明确解释如何绘制一个国家的地图。下面我解释一些选项/教程中,我所看到的,为什么他们没有工作(这我只是认为,教程不是很简单的):

  1. 散景(http://bokeh.pydata.org/en/latest/docs/gallery/texas.html)。在教程中,德克萨斯状态被绘制,因为us_counties在bokeh.sampledata中。但是我没有在样本数据中找到其他国家。

  2. mpl_toolkits.basemap(http://www.geophysique.be/2011/01/27/matplotlib-basemap-tutorial-07-shapefiles-unleached/)。尽管我能够导入shapefile,但我无法运行from shapefile import ShapeFile(ImportError:无法导入名称ShapeFile)。此外,我还无法下载dbflib库。

  3. Vincent(Why Python Vincent map visuzalization does not map data from Data Frame?)当我在上述教程的答案中运行代码时,没有出现图像(尽管我使用了命令vincent.core.initialize_notebook())。

  4. Plotly(https://plot.ly/python/choropleth-maps/)。本教程绘制美国从csv表导入信息的地图(没有其他国家的信息可用)。如果想绘制另一个国家,是否有可能制作桌子?

探讨了这4个选项我发现教程不是很清楚或容易遵循。我发现很难相信在蟒蛇中绘制一个国家的地图是困难的。我认为必须有比以往教程中解释的更简单的方法。

现在的问题是: 哪个是最简单(希望简单)的方式来绘制一个国家(任何)与Python的地图,以及如何?

我已经安装了以下软件包:matplotlib,pyshp,mpl_toolkits.basemap,bokeh,pandas,numpy。 我也下载了墨西哥的地图从http://www.gadm.org/

在此先感谢。

+0

这似乎主要是一个基于意见的问题,究竟是什么问题? –

+0

我有几个问题,每个选项(如解释)。我想,我不应该提出一个单独的问题,而应该要求一个简单的教程。当然简单是基于意见的,但任何简单的教程都足够了。 –

+0

虽然这不是真正的stackoverflow - 你应该发布明确的问题,显示你做了什么,为什么失败,理想情况下用最少的工作例子,以便人们有解决问题的现实机会。无论如何,我在下面试过一个教程,让我知道如果这有帮助! –

回答

2

这可能不是您希望的答案,但您看过Plotly for maps?他们的例子看起来完全像你想要做的,尽管我自己并不熟悉它,而且我不知道他们有哪些地图可用,以及上传自己的地图是多么容易。

5

虽然这个问题目前的形式似乎是无法回答的,但我至少会注意到当你使用底图的时候你似乎出了什么问题 - 你不想导入Shapefile,只是使用readshapefile方法读取它一个Basemap对象,像这样的:

m = Basemap(projection='tmerc') 
m.readshapefile("/path/to/your/shapefile", "mexican_states") 

然后,您将能够通过m.mexican_states访问每个国家的边界​​的坐标(作为阵列的列表)和相应的信息(如姓名,也许一个蔡作馨代码)通过m.mexican_states_info。然后,您需要某种类型的字典或DataFrame,其中包含状态的名称/代码(对应于m.mexican_states_info中的内容)以及要绘制的值。一个简单的例子会工作是这样的,假设你有一个名为mexican_states_sick_people字典看起来像{"Mexico City":123, "Chiapas":35, ...}

import numpy as np 
import matplotlib.pyplot as plt 
from matplotlib.collections import PatchCollection 
from mpl_toolkits.basemap import Basemap 
from shapely.geometry import Polygon 
from descartes import PolygonPatch 

fig, ax = plt.subplots() 

# Set up basemap and read in state shapefile (this will draw all state boundaries) 
m = Basemap(projection='tmerc') 
m.readshapefile("/path/to/your/shapefile", "mexican_states") 

# Get maximum number of sick people to calculate shades for states based on relative number  
max_sick = np.max(mexican_states_sick_people.values()) 

# Loop through the states contained in shapefile, attaching a PolygonPatch for each of them with shade corresponding to relative number of sick people 
state_patches = [] 
for coordinates, state in zip(m.mexican_states, m.mexican_states_info): 
    if state["State_name"] in mexican_states_sick_people.keys(): 
     shade = mexican_states_sick_people[state["State_name"]]/max_sick  
     state_patches.append(PolygonPatch(Polygon(coordinates), fc = "darkred", ec='#555555', lw=.2, alpha=shade, zorder=4) 

# Put PatchCollection of states on the map 
ax.add_collection(PatchCollection(state_patches, match_original=True)) 

这个例子应该或多或少的功能,如果你有状态的工作shape文件,并确保你所拥有的病人的数据集有一些标识符(名称或代码),允许你将这些数字与shapefile中的状态标识符进行匹配(这是循环中shade = ...行所依赖的 - 在示例中,我使用shapefile中的名称作为键访问字典中的val)。

希望这会有所帮助,祝你好运!

+0

非常感谢你,你是对。在我将删除此问题后,我将发布特别与mpl_toolkits.basemap相关的新问题。 –

相关问题