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

标签 python shapefile bokeh matplotlib-basemap vincent

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

我想绘制墨西哥各州的数据(某年的患病人数)。 我正在使用 jupyter 笔记本。 到目前为止,我已经看到了几个选项和教程,但似乎都没有明确解释如何绘制国家 map 。下面我解释了一些我见过的选项/教程,以及为什么它们不起作用(我这样做只是为了证明教程不是很直接):

  1. Bokeh ( http://docs.bokeh.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. 情节(https://plot.ly/python/choropleth-maps/)。本教程绘制了从 csv 表导入信息的美国 map (没有其他国家的信息可用)。如果要绘制另一个国家,是否可以制作表格?

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

问题是: 使用 Python 绘制某个国家(任何)的 map 的最简单(希望简单)的方法是什么?如何?

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

提前致谢。

最佳答案

虽然这个问题在目前的形式下似乎无法回答,但我至少会注意到您在使用 basemap 时似乎出了点问题 - 您不想导入 Shapefile,而只是使用 读取它Basemap 对象的 readshapefile 方法,如下所示:

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))

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

希望这对您有所帮助,祝您好运!

关于python - 使用 python 在国家 map 上绘制数据的最简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36077520/

相关文章:

Python Bokeh 将附加参数发送到小部件事件处理程序

python - 色相色彩空间中的图像分割

java - 从 Java 中的 InputStream 读取 ESRI shapefile

java - 使用 GeoTools 更改从 Shapefile 读取的几何图形的精度

gis - NetLogo GIS : dealing with large shapefile

python - 从面板仪表板引发错误并显示回溯消息

Python 查找一个列表中介于另一个列表值之间的值的方法

扩展 SocketServer.TCPServer 时出现 Python "instance has no attribute"错误

pythonbrew bashrc awk 行不再有效

python - 使用 slider 更新 Bokeh 图