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

问题描述:

无法删除问题.请参阅问题:一个国家的阴影州根据带有底图的字典值

Could not delete question. Please refer to question: Shade states of a country according to dictionary values with Basemap

我想绘制每个墨西哥州的数据(某一年的病人人数). 我正在使用jupyter笔记本. 到目前为止,我已经看到了几种选择和教程,但是似乎没有一个可以明确地说明如何绘制国家/地区地图.下面,我解释一些我已经看到的选项/教程以及为什么它们不起作用(我只是为了证明教程不是很简单):

I want to plot data (number of sick people for a certain year) on each state of Mexico. I am using jupyter notebook. So far I have seen several options and tutorials, but none seem to seem to explicitly explain how to plot the map of a country. Below I explain some options/tutorial I have seen and why they have not worked (this I do just to argue that tutorials are not very straight forward):

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

  1. Bokeh (http://docs.bokeh.org/en/latest/docs/gallery/texas.html). In the tutorial texas state is plotted given that us_counties is in bokeh.sampledata. However I have not found other countries in the sampledata.

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

mpl_toolkits.basemap (http://www.geophysique.be/2011/01/27/matplotlib-basemap-tutorial-07-shapefiles-unleached/). Although I am able to import shapefile, I cannot run from shapefile import ShapeFile (ImportError: cannot import name ShapeFile). Furthermore I have not been able to download dbflib library.

Vincent(为什么Python Vincent map visuzalization不能映射Data Frame中的数据?)当我从上述教程中的答案中运行代码时,没有图像出现(即使我使用了命令vincent.core.initialize_notebook()).

Vincent (Why Python Vincent map visuzalization does not map data from Data Frame?) When I run the code from the answer in said tutorial no image appears (even though I used command vincent.core.initialize_notebook() ).

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

Plotly (https://plot.ly/python/choropleth-maps/). The tutorial plots the map of USA importing information from a csv table (no information of other countries available). If wanting to plot another country, would it be possible to make the table?

探索了这4个选项,我发现教程不是很清楚或不容易理解.我发现很难相信在python中绘制国家/地区地图很困难.我认为肯定有比以前的教程中解释的方法更简单的方法.

Explored this 4 options I have found tutorials not to be very clear or easy to follow. I find it hard to believe that plotting a map of a country is difficult in python. I think there must be an easier way than the ones explained in the past tutorials.

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

The question is: Which is the easiest (hopefully simple) way to plot the map of a certain country (any) with python and how?

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

I have installed the following packages: matplotlib, pyshp, mpl_toolkits.basemap, bokeh, pandas, numpy. I have also downloaded Mexico's map from http://www.gadm.org/

谢谢.

虽然此问题在当前形式下似乎无法回答,但我至少要注意,使用底图时您似乎有问题-您不会想要导入Shapefile,但只需使用Basemap对象的readshapefile方法读取它,如下所示:

While this question seems to be unanswerable in its current form, I'll at least note that you seem to be something wrong when using basemap - you don't want to import Shapefile, but simply read it using the readshapefile method of a Basemap object like so:

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

然后您将能够通过m.mexican_states(作为数组列表)访问每个州边界的坐标,并通过m.mexican_states_info访问相应的信息(例如名称,可能是一个识别码).然后,您将需要某种dict或DataFrame,其中包含状态的名称/代码(与m.mexican_states_info中的内容相对应)和要绘制的值.一个简单的示例将像这样工作,假设您有一个名为mexican_states_sick_people的字典,其外观类似于{"Mexico City":123, "Chiapas":35, ...}:

You will then be able to access the coordinates of each state's boundaries via m.mexican_states (as a list of arrays) and the corresponding information (such as names, maybe an indentifying code) by m.mexican_states_info. You will then need some sort of dict or DataFrame containing names/codes for states (corresponding to what is in m.mexican_states_info) and the values you want to plot. A simple example would work something like this, assuming that you have a dict called mexican_states_sick_people that looks something like {"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 = ...行所依赖的-在本例中,我使用shapefile中的名称作为键来访问字典中的val).

This example should be more or less functional, if you have a working shapefile of states and make sure that the dataset of sick people you have has some sort of identifier (name or code) for each state that allows you to match up the numbers with the identifier of states in the shapefile (this is what the shade = ... line in the loop relies upon - in the example I'm accessing the vals in the dictionary using a name from the shapefile as key).

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

Hope this helps, good luck!