使用python在国家地图上绘制数据的最简单方法

时间:2016-03-18 06:23:17

标签: python shapefile bokeh matplotlib-basemap vincent

无法删除问题。请参阅问题:Shade states of a country according to dictionary values with Basemap

我想在每个墨西哥州绘制数据(特定年份的病人数)。 我正在使用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表导入信息的地图(没有其他可用国家的信息)。如果想要绘制另一个国家,是否可以制作表格?

  5. 探索了这4个选项,我发现教程不是很清楚或不容易理解。我发现很难相信在python中绘制一个国家的地图很困难。我认为必须有一种比过去教程中解释的更简单的方法。

    问题是: 用python绘制某个国家(任何)地图的最简单(希望是简单的)方法是什么?

    我安装了以下软件包:matplotlib,pyshp,mpl_toolkits.basemap,bokeh,pandas,numpy。 我还从http://www.gadm.org/

    下载了墨西哥的地图

    提前致谢。

2 个答案:

答案 0 :(得分:5)

虽然这个问题在目前的形式下似乎无法回答,但我至少会注意到在使用底图时你似乎有些不对劲 - 你不想导入Shapefile,而只是使用{{1像这样的readshapefile对象的方法:

Basemap

然后,您将能够通过m = Basemap(projection='tmerc') m.readshapefile("/path/to/your/shapefile", "mexican_states") (作为数组列表)和m.mexican_states的相应信息(例如名称,可能是识别代码)访问每个州的边界坐标。然后,您将需要某种类型的dict或DataFrame,其中包含状态的名称/代码(对应于m.mexican_states_info中的内容)以及要绘制的值。假设你有一个名为m.mexican_states_info的字典,看起来像mexican_states_sick_people,那么一个简单的例子就可以这样做了。

{"Mexico City":123, "Chiapas":35, ...}

如果你有状态的工作形状文件并且确保你拥有的病人的数据集具有某种允许你匹配的状态的标识符(名称或代码),那么这个例子应该或多或少有用。 shapefile中带有状态标识符的数字(这是循环中的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)) 行所依赖的 - 在示例中,我使用shapefile中的名称作为键访问字典中的val)。 / p>

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

答案 1 :(得分:2)

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

相关问题