2016-03-18 58 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相關的新問題。 –

相關問題