【Cartopy】Python Cartopyを使ったMapping
こんにちは! 私は気象のデータを用いて研究を行っています。
Pythonを用いて、Jupyternotebookでコーディングを行っています 今回はCartopyの使い方の基本をメモします。
Cartopyについて
今まではBasemapを使って解析をしていたのですが、今回Cartopyに切り替えることにしました。
Cartopyの基本的な使い方は、ホームページに掲載されていますので参照ください!
Using cartopy with matplotlib — cartopy 0.19.0rc2.dev8+gd251b2f documentation
グラフの作成
アジア~日本をマッピング
地図は正距円筒図法で書きます。 まず、これらをインポートします
#*1 import matplotlib.pyplot as plt #*2 import cartopy.crs as ccrs import cartopy.feature as cfeature
Keys
1. グラフを描くツール
2. Cartopyでの描写で用いるツール
def main(): fig = plt.figure(figsize=(15,5)) plt.rcParams["font.size"] = 18 #*3 plt.suptitle("Map") #全体のタイトル設定 #図の用意 ax1 = fig.add_subplot(1,3,1, projection=ccrs.PlateCarree()) #*4 ax1.set_extent([90, 150, 0, 60], crs=ccrs.PlateCarree()) #タイトルの設定 ax1.set_title("Asia") ax2 = fig.add_subplot(1,3,2, projection=ccrs.PlateCarree()) ax2.set_extent([128, 148, 30, 50], crs=ccrs.PlateCarree()) ax2.set_title("Japan") ax3 = fig.add_subplot(1,3,3, projection=ccrs.PlateCarree()) ax3.set_extent([139, 141, 34.5, 36.5], crs=ccrs.PlateCarree()) ax3.set_title("Kanto") #まとめて設定するものはforループで! axes = [ax1, ax2, ax3] for ax in axes: #海岸線の解像度を10 mにする*5 ax.coastlines(resolution='10m') #国境線を入れる*6 ax.add_feature(cfeature.BORDERS, linestyle=':') plt.show() #図の保存 fig.savefig('XXX.png', format='png', dpi=360) if __name__ == '__main__': main()
Keys
3. 一括でフォントサイズ設定
4. 図の範囲[経度(min), 経度(max), 緯度(min), 緯度(max)]
5. resolutionは、"110", "50","10"のどれか
6. linestyleは、":","--","-"など
以下のコードを参考にしました。 https://scitools.org.uk/cartopy/docs/latest/gallery/features.html#sphx-glr-gallery-features-py
県庁所在地のプロット
試しに県庁所在地をマップにプロットします。 県庁所在地のデータは以下のHPを利用いたしました。
【みんなの知識 ちょっと便利帳】都道府県庁所在地 緯度経度データ - 各都市からの方位地図 - 10進数/60進数での座標・世界測地系(WGS84)
まずは使うライブラリをインポートします。
import pandas as pd
次にエクセルファイルからデータをdf(データフレーム)に入れます
df = pd.read_excel("latlng_data.xls", skiprows=4, skipfooter=6, index_col=0) df.head() #dfのはじめのみの表示
Keys
1. skiprows: はじめの行数をスキップ
2. skipfooter: 終わりの行数をスキップ
3. index_col: インデックスにする行を指定する
データを用意したら、緯度(latitude)と経度(longitude)を切りとります
lat = df["緯度"] lon = df["経度"]
グラフを描きます 今回はScatter(散布)で点を打っていきます
def main(): fig = plt.figure(figsize=(5,5)) plt.rcParams["font.size"] = 18 ax = fig.add_subplot(1,1,1, projection=ccrs.PlateCarree()) ax.set_extent([128, 148, 30, 50], crs=ccrs.PlateCarree()) ax.set_title("Japan") ax.coastlines(resolution='10m') #海岸線の解像度を10 mにする ax.add_feature(cfeature.BORDERS, linestyle=':') ax.stock_img() #地図の色を塗る #データのプロット ax.scatter(lon, lat, color="r", marker="o", s = 5) plt.show() #保存 fig.savefig('XXX.png', format='png', dpi=360) if __name__ == '__main__': main()
今日はここまで
それでは 🌏