2017-02-26 28 views
2

私はベースマップ上のいくつかのデータを描画し、バックグラウンドで状態の境界を使用していますが状態の省略形を描く:それは地図上の状態名を描画する方法(カリフォルニアため例えばCA)<a href="http://matplotlib.org/basemap/users/figures/plotprecip.py" rel="nofollow noreferrer">this piece of code</a>を使用してmatplotlibのベースマップ

from mpl_toolkits.basemap import Basemap, cm 
# requires netcdf4-python (netcdf4-python.googlecode.com) 
from netCDF4 import Dataset as NetCDFFile 
import numpy as np 
import matplotlib.pyplot as plt 

# plot rainfall from NWS using special precipitation 
# colormap used by the NWS, and included in basemap. 

nc = NetCDFFile('../../../examples/nws_precip_conus_20061222.nc') 
# data from http://water.weather.gov/precip/ 
prcpvar = nc.variables['amountofprecip'] 
data = 0.01*prcpvar[:] 
latcorners = nc.variables['lat'][:] 
loncorners = -nc.variables['lon'][:] 
lon_0 = -nc.variables['true_lon'].getValue() 
lat_0 = nc.variables['true_lat'].getValue() 
# create figure and axes instances 
fig = plt.figure(figsize=(8,8)) 
ax = fig.add_axes([0.1,0.1,0.8,0.8]) 
# create polar stereographic Basemap instance. 
m = Basemap(projection='stere',lon_0=lon_0,lat_0=90.,lat_ts=lat_0,\ 
      llcrnrlat=latcorners[0],urcrnrlat=latcorners[2],\ 
      llcrnrlon=loncorners[0],urcrnrlon=loncorners[2],\ 
      rsphere=6371200.,resolution='l',area_thresh=10000) 
# draw coastlines, state and country boundaries, edge of map. 
m.drawcoastlines() 
m.drawstates() 
m.drawcountries() 
plt.show() 

です?

答えて

4
import numpy as np 
import matplotlib.pyplot as plt 
from mpl_toolkits.basemap import Basemap as Basemap 
from matplotlib.patches import Polygon 

short_state_names = { 
     'AK': 'Alaska', 
     'AL': 'Alabama', 
     'AR': 'Arkansas', 
     'AS': 'American Samoa', 
     'AZ': 'Arizona', 
     'CA': 'California', 
     'CO': 'Colorado', 
     'CT': 'Connecticut', 
     'DC': 'District of Columbia', 
     'DE': 'Delaware', 
     'FL': 'Florida', 
     'GA': 'Georgia', 
     'GU': 'Guam', 
     'HI': 'Hawaii', 
     'IA': 'Iowa', 
     'ID': 'Idaho', 
     'IL': 'Illinois', 
     'IN': 'Indiana', 
     'KS': 'Kansas', 
     'KY': 'Kentucky', 
     'LA': 'Louisiana', 
     'MA': 'Massachusetts', 
     'MD': 'Maryland', 
     'ME': 'Maine', 
     'MI': 'Michigan', 
     'MN': 'Minnesota', 
     'MO': 'Missouri', 
     'MP': 'Northern Mariana Islands', 
     'MS': 'Mississippi', 
     'MT': 'Montana', 
     'NA': 'National', 
     'NC': 'North Carolina', 
     'ND': 'North Dakota', 
     'NE': 'Nebraska', 
     'NH': 'New Hampshire', 
     'NJ': 'New Jersey', 
     'NM': 'New Mexico', 
     'NV': 'Nevada', 
     'NY': 'New York', 
     'OH': 'Ohio', 
     'OK': 'Oklahoma', 
     'OR': 'Oregon', 
     'PA': 'Pennsylvania', 
     'PR': 'Puerto Rico', 
     'RI': 'Rhode Island', 
     'SC': 'South Carolina', 
     'SD': 'South Dakota', 
     'TN': 'Tennessee', 
     'TX': 'Texas', 
     'UT': 'Utah', 
     'VA': 'Virginia', 
     'VI': 'Virgin Islands', 
     'VT': 'Vermont', 
     'WA': 'Washington', 
     'WI': 'Wisconsin', 
     'WV': 'West Virginia', 
     'WY': 'Wyoming' 
} 

m = Basemap(llcrnrlon=-119,llcrnrlat=22,urcrnrlon=-64,urcrnrlat=49, 
      projection='lcc',lat_1=33,lat_2=45,lon_0=-95) 
# data from U.S Census Bureau 
# http://www.census.gov/geo/www/cob/st2000.html 
shp_info = m.readshapefile('st99_d00','states',drawbounds=True) 
printed_names = [] 
for shapedict,state in zip(m.states_info, m.states): 
    short_name = short_state_names.keys()[short_state_names.values().index(shapedict['NAME'])] 
    if short_name in printed_names: continue 
    # center of polygon 
    x, y = np.array(state).mean(axis=0) 
    # You have to align x,y manually to avoid overlapping for little states 
    plt.text(x+.1, y, short_name, ha="center") 
    printed_names += [short_name,] 

m.drawparallels(np.arange(25,65,20),labels=[1,0,0,0]) 
m.drawmeridians(np.arange(-120,-40,20),labels=[0,0,0,1]) 
plt.title('The United States of North America') 
plt.show() 

enter image description here

+0

おかげで@Serenityが、私はミシガン州とウィスコンシン州に問題があると思いますが、コードに関連していないようです、あなたはそれらがそこに置かれている理由についての洞察力を持っていますか? – Ash

+0

MIとWIにいくつかの島があり、あなたのコードがそれらの島の州の省略形を描いていたことが判明した場合、それらの島をスキップして州の主要部分に名前を描く必要があります。 – Ash

2

(例えばFL見て)@Serenityの答えが正しいですが、略語は配置されるべき場所をいくつかの小さな状態について手動で変更しなければなりません。

状態のポリゴンのすべての点の平均ではなく、それらの点に凸面を描き、それらの点の平均を使用することができます。これは物事を大きく変えることはありませんが、誰かにとって有用かもしれません。

また、いくつかの州には、ここでは描画しないいくつかの小さな島があります(short_nameがprint_namesにある場合、これらの島をスキップします)。ミシガン州とウィスコンシン州の場合、これらの島は主要州の前に来るので、描かれません。私はチェックし、メインのミシガンは4番、ウィスコンシンは3番なので、4番ミシガンと3番ウィソンズンの州名を描くだけです。

import numpy as np 
import matplotlib.pyplot as plt 
from mpl_toolkits.basemap import Basemap as Basemap 
from matplotlib.patches import Polygon 
from scipy.spatial import ConvexHull, Voronoi 
import pdb 

short_state_names = { 
     'AK': 'Alaska', 
     'AL': 'Alabama', 
     'AR': 'Arkansas', 
     'AS': 'American Samoa', 
     'AZ': 'Arizona', 
     'CA': 'California', 
     'CO': 'Colorado', 
     'CT': 'Connecticut', 
     'DC': 'District of Columbia', 
     'DE': 'Delaware', 
     'FL': 'Florida', 
     'GA': 'Georgia', 
     'GU': 'Guam', 
     'HI': 'Hawaii', 
     'IA': 'Iowa', 
     'ID': 'Idaho', 
     'IL': 'Illinois', 
     'IN': 'Indiana', 
     'KS': 'Kansas', 
     'KY': 'Kentucky', 
     'LA': 'Louisiana', 
     'MA': 'Massachusetts', 
     'MD': 'Maryland', 
     'ME': 'Maine', 
     'MI': 'Michigan', 
     'MN': 'Minnesota', 
     'MO': 'Missouri', 
     'MP': 'Northern Mariana Islands', 
     'MS': 'Mississippi', 
     'MT': 'Montana', 
     'NA': 'National', 
     'NC': 'North Carolina', 
     'ND': 'North Dakota', 
     'NE': 'Nebraska', 
     'NH': 'New Hampshire', 
     'NJ': 'New Jersey', 
     'NM': 'New Mexico', 
     'NV': 'Nevada', 
     'NY': 'New York', 
     'OH': 'Ohio', 
     'OK': 'Oklahoma', 
     'OR': 'Oregon', 
     'PA': 'Pennsylvania', 
     'PR': 'Puerto Rico', 
     'RI': 'Rhode Island', 
     'SC': 'South Carolina', 
     'SD': 'South Dakota', 
     'TN': 'Tennessee', 
     'TX': 'Texas', 
     'UT': 'Utah', 
     'VA': 'Virginia', 
     'VI': 'Virgin Islands', 
     'VT': 'Vermont', 
     'WA': 'Washington', 
     'WI': 'Wisconsin', 
     'WV': 'West Virginia', 
     'WY': 'Wyoming' 
} 

m = Basemap(llcrnrlon=-119,llcrnrlat=22,urcrnrlon=-64,urcrnrlat=49, 
      projection='lcc',lat_1=33,lat_2=45,lon_0=-95) 
# data from U.S Census Bureau 
# http://www.census.gov/geo/www/cob/st2000.html 
shp_info = m.readshapefile('./data/us_states_st99/st99_d00','states',drawbounds=True) 
printed_names = [] 
mi_index = 0 
wi_index = 0 
for shapedict,state in zip(m.states_info, m.states): 
    draw_state_name = True 
    short_name = short_state_names.keys()[short_state_names.values().index(shapedict['NAME'])] 
    if short_name in printed_names and short_name not in ['MI', 'WI']: 
     continue 
    if short_name == 'MI': 
     if mi_index != 3: 
      draw_state_name = False 
     mi_index += 1 
    if short_name == 'WI': 
     if wi_index != 2: 
      draw_state_name = False 
     wi_index += 1 
    # center of polygon 
    x, y = np.array(state).mean(axis=0) 
    hull = ConvexHull(state) 
    hull_points = np.array(state)[hull.vertices] 
    #center of convex hull over the polygon points 
    x, y = hull_points.mean(axis=0) 
    if draw_state_name: 
     # You have to align x,y manually to avoid overlapping for little states 
     plt.text(x+.1, y, short_name, ha="center") 
    printed_names += [short_name,] 

m.drawparallels(np.arange(25,65,20),labels=[1,0,0,0]) 
m.drawmeridians(np.arange(-120,-40,20),labels=[0,0,0,1]) 
plt.title('The United States of North America') 
plt.show() 

あなたが他の州の他の島々を気にした場合、あなたはそれらの島々(例えば名前、または何でもあなたがする必要がある、引き続きポリゴンを描画しなくはない)と似た何かを行う必要があります。

enter image description here

+1

あなたのベースマップはどこからダウンロードしましたか?リンクはもはや動かないようです。 –

+0

https://github.com/matplotlib/basemap/tree/master/examplesのst99_d00 – Ash

関連する問題

 関連する問題