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

RM-*_*RM- 1 python shapefile matplotlib-basemap bokeh vincent

无法删除问题.请参考问题:根据字典值使用Basemap对国家/地区进行阴影状态

我想在每个墨西哥州绘制数据(特定年份的病人数).我正在使用jupyter笔记本.到目前为止,我已经看到了几个选项和教程,但似乎没有一个似乎明确解释如何绘制一个国家的地图.下面我解释一些我见过的选项/教程以及为什么它们没有工作(我只是认为教程不是很直接):

  1. Bokeh(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. 文森特(为什么Python的文森特地图visuzalization不从数据帧映射数据?在说教程出现没有图像(即使我使用的命令)当我运行从答案的代码vincent.core.initialize_notebook()).

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

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

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

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

提前致谢.

Nil*_*dat 5

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

m = Basemap(projection='tmerc')
m.readshapefile("/path/to/your/shapefile", "mexican_states")
Run Code Online (Sandbox Code Playgroud)

然后,您将能够通过m.mexican_states(作为数组列表)和相应的信息(例如名称,可能是识别代码)访问每个州的边界坐标m.mexican_states_info.然后,您将需要某种类型的dict或DataFrame,其中包含状态的名称/代码(对应于内容m.mexican_states_info)和要绘制的值.一个简单的例子会起到这样的作用,假设你有一个叫做的dict 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))
Run Code Online (Sandbox Code Playgroud)

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

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


Cyn*_* GS 4

这可能不是您希望的答案,但是您看过Plotly 的地图吗?他们的示例看起来与您想要做的完全一样,尽管我自己对此并不熟悉,而且我不知道他们有哪些地图可用以及上传您自己的地图有多容易。