2017-12-19 33 views
0

고도가 표시된 말라위지도를 만들려고합니다. 물론 이런 식으로 뭔가하지만, 말라위 : 여기에서 약간의 고도 데이터를 다운로드 한파이썬에서 고도 표기하기

enter image description here

:

meters, from 5-min data/(unknown) (time: 1; latitude: 360; longitude: 720) 
    Dimension coordinates: 
      time       x   -    - 
      latitude      -   x    - 
      longitude      -   -    x 
    Attributes: 
      history: 
Elevations calculated from the TBASE 5-minute 
latitude-longitude resolution... 
      invalid_units: meters, from 5-min data 
: 이것은 큐브를 만든 후 해당 데이터의 인쇄입니다 http://research.jisao.washington.edu/data_sets/elevation/

데이터를 가져 와서 큐브를 만들고, 추가 변수 (시간 및 기록)를 제거하고 말라위의 위도와 경도로 데이터를 제한하는 것으로 시작했습니다. 이것이 내가 무슨 짓이 ... 작동하지 않습니다, 그러나

Elevation 
[[ 978. 1000. 1408. 1324. 1080. 1370. 1857. 1584.] 
[ 1297. 1193. 1452. 1611. 1354. 1480. 1350. 627.] 
[ 1418. 1490. 1625. 1486. 1977. 1802. 1226. 482.] 
[ 1336. 1326. 1405. 728. 1105. 1559. 1139. 789.] 
[ 1368. 1301. 1463. 1389. 671. 942. 947. 970.] 
[ 1279. 1116. 1323. 1587. 839. 1014. 1071. 1003.] 
[ 1096. 969. 1179. 1246. 855. 979. 927. 638.] 
[ 911. 982. 1235. 1324. 681. 813. 814. 707.] 
[ 749. 957. 1220. 1198. 613. 688. 832. 858.] 
[ 707. 1049. 1037. 907. 624. 771. 1142. 1104.] 
[ 836. 1044. 1124. 1120. 682. 711. 1126. 922.] 
[ 1050. 1204. 1199. 1161. 777. 569. 999. 828.] 
[ 1006. 869. 1183. 1230. 1354. 616. 762. 784.] 
[ 838. 607. 883. 1181. 1174. 927. 591. 856.] 
[ 561. 402. 626. 775. 1053. 726. 828. 733.] 
[ 370. 388. 363. 422. 508. 471. 906. 1104.] 
[ 504. 326. 298. 208. 246. 160. 458. 682.] 
[ 658. 512. 334. 309. 156. 162. 123. 340.]] 
latitude 
DimCoord(array([ -8.25, -8.75, -9.25, -9.75, -10.25, -10.75, -11.25, -11.75, 
     -12.25, -12.75, -13.25, -13.75, -14.25, -14.75, -15.25, -15.75, 
     -16.25, -16.75], dtype=float32), standard_name='latitude', units=Unit('degrees'), var_name='lat', attributes={'title': 'Latitude'}) 
longitude 
DimCoord(array([ 32.25, 32.75, 33.25, 33.75, 34.25, 34.75, 35.25, 35.75], dtype=float32), standard_name='longitude', units=Unit('degrees'), var_name='lon', attributes={'title': 'Longitude'}) 

내가 플롯 할 때 :

import matplotlib.pyplot as plt 
import matplotlib.cm as mpl_cm 
import numpy as np 
import iris 
import cartopy 
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter 
import iris.analysis.cartography 


def main(): 

    #bring in altitude data 
    Elev = '/exports/csce/datastore/geos/users/s0899345/Climate_Modelling/Actual_Data/elev.0.5-deg.nc' 

    Elev= iris.load_cube(Elev) 

    #remove variable for time 
    del Elev.attributes['history'] 
    Elev = Elev.collapsed('time', iris.analysis.MEAN) 

    Malawi = iris.Constraint(longitude=lambda v: 32.0 <= v <= 36., latitude=lambda v: -17. <= v <= -8.)  
    Elev = Elev.extract(Malawi) 

    print 'Elevation' 
    print Elev.data 
    print 'latitude' 
    print Elev.coord('latitude') 
    print 'longitude' 
    print Elev.coord('longitude') 

이 잘 작동하고 다음과 같이 출력은

#plot map with physical features 
ax = plt.axes(projection=cartopy.crs.PlateCarree()) 
ax.add_feature(cartopy.feature.COASTLINE) 
ax.add_feature(cartopy.feature.BORDERS) 
ax.add_feature(cartopy.feature.LAKES, alpha=0.5) 
ax.add_feature(cartopy.feature.RIVERS) 
#plot altitude data 
plot=ax.plot(Elev, cmap=mpl_cm.get_cmap('YlGn'), levels=np.arange(0,2000,150), extend='both') 
#add colour bar index and a label 
plt.colorbar(plot, label='meters above sea level') 
#set map boundary 
ax.set_extent([32., 36., -8, -17]) 
#set axis tick marks 
ax.set_xticks([33, 34, 35]) 
ax.set_yticks([-10, -12, -14, -16]) 
lon_formatter = LongitudeFormatter(zero_direction_label=True) 
lat_formatter = LatitudeFormatter() 
ax.xaxis.set_major_formatter(lon_formatter) 
ax.yaxis.set_major_formatter(lat_formatter) 
#save the image of the graph and include full legend 
plt.savefig('Map_data_boundary', bbox_inches='tight') 
plt.show() 

내가 오류가 'Attribute Error: Unknown property type cmap' 온 세상의 다음지도입니다 ...

enter image description here

아이디어가 있으십니까?

답변

2

내가 데이터를 준비합니다,

plot = ax.contourf(...) 

대부분의 아마 당신은 또한 contourf에 인수로 위도와 경도를주고 싶어 사용할 너와 같지만 time 크기를 제거하는 것을 제외하고는 길이가 1 인 차원을 제거하는 iris.util.squeeze을 사용합니다. @ImportanceOfBeingErnest가 말한대로

import iris 

elev = iris.load_cube('elev.0.5-deg.nc') 
elev = iris.util.squeeze(elev) 
malawi = iris.Constraint(longitude=lambda v: 32.0 <= v <= 36., 
         latitude=lambda v: -17. <= v <= -8.)  
elev = elev.extract(malawi) 

, 당신은 윤곽 플롯을합니다. 어떤 플로팅 기능을 사용해야하는지 잘 모르겠 으면 matplotlib gallery을 탐색하여 제작하고자하는 것과 비슷한 것을 찾으십시오. 이미지를 클릭하면 코드가 표시됩니다.그러나

import matplotlib.pyplot as plt 
import matplotlib.cm as mpl_cm 
import numpy as np 
import cartopy 

cmap = mpl_cm.get_cmap('YlGn') 
levels = np.arange(0,2000,150) 
extend = 'max' 

ax = plt.axes(projection=cartopy.crs.PlateCarree()) 
plt.contourf(elev.coord('longitude').points, elev.coord('latitude').points, 
      elev.data, cmap=cmap, levels=levels, extend=extend) 

matplotlib.pyplot version

:

그래서, 당신은 matplotlib.pyplot.contourf 기능을 사용할 수 있습니다 윤곽 플롯을 만들기 위해,하지만 당신은 numpy 배열의 형태로 큐브에서 관련 데이터를 얻을 수 있습니다 irisiris.plot 형태로 maplotlib.pyplot 함수에 대한 바로 가기를 제공합니다. 그러면 오른쪽 투영으로 축 인스턴스가 자동으로 설정되고 큐브의 데이터가 matplotlib.pyplot으로 전달됩니다. 그래서 마지막 두 줄은 단순히이 될 수 있습니다

import iris.quickplot as qplt 
qplt.contourf(elev, cmap=cmap, levels=levels, extend=extend) 
:

import iris.plot as iplt 
iplt.contourf(elev, cmap=cmap, levels=levels, extend=extend) 

iris.plot version

자동으로 적합한 년 Colorbar 및 라벨을 추가하는 것을 제외하고 기본적으로 iris.plot과 동일하다, 또한 iris.quickplot있다

iris.quickplot version

일단 플롯되면 t 그는 예를 축하고 (있는 나는 단순히 코드를 복사) 다른 항목을 추가 :

from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter 

qplt.contourf(elev, cmap=cmap, levels=levels, extend=extend) 
ax = plt.gca() 

ax.add_feature(cartopy.feature.COASTLINE) 
ax.add_feature(cartopy.feature.BORDERS) 
ax.add_feature(cartopy.feature.LAKES, alpha=0.5) 
ax.add_feature(cartopy.feature.RIVERS) 

ax.set_xticks([33, 34, 35]) 
ax.set_yticks([-10, -12, -14, -16]) 
lon_formatter = LongitudeFormatter(zero_direction_label=True) 
lat_formatter = LatitudeFormatter() 
ax.xaxis.set_major_formatter(lon_formatter) 
ax.yaxis.set_major_formatter(lat_formatter) 

iris.quickplot with additions

+0

고맙습니다. @RuthC, 이것은 매우 명확하고 유용한 답변입니다! 새해 복 많이 받으세요! – ErikaAWT

0

당신이 추가 할 수 있습니다 : 당신하기 matplotlib 업데이트 할

import matplotlib.colors as colors 

color = plt.get_cmap('YlGn') # and change cmap=mpl_cm.get_cmap('YlGn') to cmap=color 

를 또한보십시오 : 그것은 당신을 보인다

pip install --upgrade matplotlib 

편집

color = plt.get_cmap('YlGn') # and change cmap=mpl_cm.get_cmap('YlGn') to cmap=color 
+0

감사 바실리, 당신의 도움을 주셔서 감사합니다,하지만 didnt가 작업 :(변화 없음. – ErikaAWT

+0

당신은 뭔가를 찾을 수 있습니다 [여기] (https://matplotlib.org/tutorials/colors/colormaps.html) –

+0

감사합니다. 바실리, 나는 이미 보았지만 행운은 없습니다. 다른 데이터 세트에서 동일한 코딩을 사용했기 때문에 이상하지 않습니다. 완벽하게 작동합니다. 나는 특별히 아무것도 가져올 필요가 없으며 cmap이 이해됩니다. 난 그냥이 데이터를 실행할 수 없습니다 .... – ErikaAWT

1

은 뭔가를 원하는 등고선 플롯. 그래서 그 대신

plot = ax.plot(...) 

의 당신은 아마

plot = ax.contourf(longitude, latitude, Elev, ...) 
+0

고마워 @ ImportanceOfBeingErnest 'plot = ax.contourf (Elev.coord ('위도 '), Elev.coord ('경도 ')로 변경했습니다. , Elev.data, cmap = YlGn, levels = np.arange (0,2000,150), extend = 'both')'오류가 발생했습니다 : 'ValueError : 배열로 배열 요소를 설정하는 중' – ErikaAWT

+0

항상 제공 전체 코드 및 전체 오류 추적. 그렇지 않으면 무슨 일이 일어나고 있는지 알 수 없습니다. – ImportanceOfBeingErnest

+0

죄송합니다. #plot 고도 데이터 plot = ax.plot (Elev, cmap = mpl_cm.get_cmap ('YIGn'), levels = np.arange (0,2000,150), extend = 'both')' 이 #plot 고도 데이터를 읽으려면 YlGn = plt.get_cmap ('YlGn') plot = ax.contourf (Elev.coord ('위도'), Elev.coord ('경도'), Elev.data, cmap = YlGn, levels = np.arange (0,2000,150), extend = 'both') ' – ErikaAWT