Python:Cartopyの基本的な使用法


序文

一般的に使用されるマップのベースマップの描画は、通常、ベースマップまたはカートピーモジュールによって完了します。ベースマップライブラリはpython2に基づいて開発されたモジュールであるため、現在開発および保守されていません。したがって、cartopyモジュールのいくつかの基本的な操作を簡単に紹介します。

1.基本的な紹介

まず、関連するモジュールをインポートします。

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

最初にパラメータ射影を導入します。このコマンドはccrsと連携して射影タイプを設定できます。ここでは、正方形の射影コマンドを例として取り上げます。central_longitudeパラメーターは、投影の中心位置です。中央の設定はベースマップの設定ルールと同じです。詳しくは前の記事をご覧ください。

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)

パラメータスケールはマップ解像度であり、現在10m、50m、110mをサポートし、パラメータ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())

ボックスは描画領域で、crsは投影タイプです。その他のコマンドは基本的に変更されていません。ボックスを[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

おすすめ

転載: blog.csdn.net/weixin_49284189/article/details/109376547