python Cartopy 의 기초 사용 상세 설명

5023 단어 pythonCartopy
머리말
자주 사용 하 는 지도 밑그림 그리 기 는 보통 Basemap 또는 cartopy 모듈 로 이 루어 집 니 다.Basemap 라 이브 러 리 는 python 2 를 기반 으로 개 발 된 모듈 이기 때문에 현재 유지 보 수 를 개발 하지 않 습 니 다.그래서 cartopy 모듈 의 기초 작업 을 간단하게 소개 합 니 다.기초
우선 관련 모듈 을 가 져 옵 니 다.

import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
먼저 매개 변수 procjection 을 소개 합 니 다.이 명령 은 ccrs 에 맞 춰 투영 형식 을 설정 할 수 있 습 니 다.여 기 는 사각형 투영 명령 을 예 로 들 수 있 습 니 다.그 중 centrallongitude 매개 변 수 는 투영 중심 위치 입 니 다.그 중심 설정 은 Basemap 설정 규칙 과 마찬가지 로 자세 한 내용 은 이전 글 을 볼 수 있 습 니 다.

ax=plt.axes(projection=ccrs.PlateCarree(central_longitude=0))
그리 기 유형 을 설정 한 후 지도의 각 특 징 량 을 그립 니 다.그 코드 는 다음 과 같다.

#ax.add_feature(cfeature.LAKES.with_scale(scale))
ax.add_feature(cfeature.OCEAN.with_scale(scale))
#ax.add_feature(cfeature.RIVERS.with_scale(scale))
#ax.add_feature(cfeature.LAND.with_scale(scale),lw=0.5)
ax.add_feature(cfeature.COASTLINE.with_scale(scale),lw=2)
매개 변수 scale 은 지도 해상도 이 고 현재 10m,50m,110 m 를 지원 하 며 매개 변수 lw 는 선 굵기 입 니 다.해안선 과 바 다 를 그립 니 다.효과 도 는 다음 과 같 습 니 다.
在这里插入图片描述
제작 이 끝 난 후 지도 로 사용 합 니 다.경위도 가 자 연 스 럽 게 없어 서 는 안 된다.이 모듈 에서 좌표 축 라벨 을 도입 하 는 동시에 이 라벨 의 눈금 을 바 꾸 는 표 시 를 도입 하고 구체 적 인 형식 은 다음 과 같다.

ax.set_xticks(np.arange(0,361,40), crs=ccrs.PlateCarree())
ax.set_yticks(np.arange(-90,90+30,30), crs=ccrs.PlateCarree())
#zero_direction_label       0    E W
lon_formatter = LongitudeFormatter(zero_direction_label=False)
lat_formatter = LatitudeFormatter()
ax.xaxis.set_major_formatter(lon_formatter)
ax.yaxis.set_major_formatter(lat_formatter)
효과 그림 은 다음 과 같 습 니 다.
在这里插入图片描述
물론 좌표 축 굵기 변 화 를 원한 다 면 명령 을 도입 할 수 있다.

ax.outline_patch.set_visible(False)
ax.spines['bottom'].set_visible(True)
ax.spines['left'].set_visible(True)
ax.spines['right'].set_visible(True)
ax.spines['top'].set_visible(True)
ax.spines['bottom'].set_linewidth(2.5);###          
ax.spines['left'].set_linewidth(2.5);####          
ax.spines['right'].set_linewidth(2.5);###          
ax.spines['top'].set_linewidth(2.5);####          
이 모듈 에서 좌표 축 을 제어 하 는 명령 은 이미 일반적인 것 과 다르다.따라서 이 통 제 를 닫 고 일반적인 좌표 축 설정 을 엽 니 다.
2.지역 지도 제작
우리 가 어떤 작은 지역 에서 연 구 를 할 때 지역 지 도 를 그 려 야 한다.이때 우 리 는 명령 을 도입 할 수 있다.

ax.set_extent(box,crs=ccrs.PlateCarree())
그 중에서 box 는 그리 기 영역 이 고 crs 는 투영 형식 입 니 다.다른 명령 은 기본적으로 변 하지 않 는 다.box 를[40,180,0,90]으로 설정 하면 다음 과 같은 효 과 를 얻 을 수 있 습 니 다.
在这里插入图片描述
총결산
독자 여러분 의 편 의 를 위해 저 는 지 도 를 그 리 는 함 수 를 썼 습 니 다.여러분 이 사용 할 때 직접 호출 할 수 있 습 니 다.이 예 는 사각형 투영 입 니 다.다른 투영 을 그 리 려 면.함수 부분 인자 만 수정 하면 됩 니 다.코드 는 다음 과 같 습 니 다:

def map_make(scale,box,xstep,ystep):
  ax=plt.axes(projection=ccrs.PlateCarree(central_longitude=180))
  a = (box[1]-box[0])//xstep
  x_start = box[1] - a*xstep
  a = (box[3]-box[2])//ystep
  y_start = box[3] - a*ystep
  ax.set_extent(box,crs=ccrs.PlateCarree())
  #ax.add_feature(cfeature.LAKES.with_scale(scale))
  #ax.add_feature(cfeature.OCEAN.with_scale(scale))
  #ax.add_feature(cfeature.RIVERS.with_scale(scale))
  #ax.add_feature(cfeature.LAND.with_scale(scale),lw=0.5)
  ax.add_feature(cfeature.COASTLINE.with_scale(scale),lw=2)
  
  ax.set_xticks(np.arange(x_start,box[1]+xstep,xstep), crs=ccrs.PlateCarree())
  ax.set_yticks(np.arange(y_start,box[3]+ystep,ystep), crs=ccrs.PlateCarree())
  #zero_direction_label       0    E W
  lon_formatter = LongitudeFormatter(zero_direction_label=False)
  lat_formatter = LatitudeFormatter()
  ax.xaxis.set_major_formatter(lon_formatter)
  ax.yaxis.set_major_formatter(lat_formatter)
  #     
  ax.grid()
  
  ax.outline_patch.set_visible(False)
  ax.spines['bottom'].set_visible(True)
  ax.spines['left'].set_visible(True)
  ax.spines['right'].set_visible(True)
  ax.spines['top'].set_visible(True)
  ax.spines['bottom'].set_linewidth(2.5);###          
  ax.spines['left'].set_linewidth(2.5);####          
  ax.spines['right'].set_linewidth(2.5);###          
  ax.spines['top'].set_linewidth(2.5);####          
  
  return ax
여기에 python Cartopy 의 기초 사용 에 대한 상세 한 설명 이 있 는 이 글 은 여기까지 소개 되 었 습 니 다.더 많은 관련 python Cartopy 내용 은 우리 의 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 조회 하 시기 바 랍 니 다.앞으로 많은 응원 바 랍 니 다!

좋은 웹페이지 즐겨찾기