matplotlib:cartopyのTIPS
目次
[top]cartopyの基本
cartopyのモジュール
cartopy.crsをインポート
import cartopy.crs as ccrs import matplotlib.pyplot as plt以降はccrsとして参照できる。同時にmatplotlibもインポートしておく
cartopyを呼び出す
fig = plt.figure() # プロット領域の作成(matplotlib) ax = fig.add_subplot(1,1,1, projection=ccrs.PlateCarree()) # サブプロット作成時にcartopy呼び出し以降は戻り値axを使い、ax.メソッドのように作図が可能
cartopyで経度線・緯度線を描く
import matplotlib.ticker as mticker gl = ax.gridlines(crs=ccrs.PlateCarree()) # 経度線・緯度線を描く gl.xlocator = mticker.FixedLocator(経度線を引く値のリスト) gl.ylocator = mticker.FixedLocator(緯度線を引く値のリスト)正距円筒図法、ランベルト正積円筒図法のみ対応
- 例:60度毎に経度線を引く
gl.xlocator = mticker.FixedLocator(np.arange(0,360.1,60))
360では300°Eまでしか経度線が引かれないため、360.1としている - 例:30度毎に緯度線を引く
gl.ylocator = mticker.FixedLocator(np.arange(-90,90.1,30))
90では60°Nまでしか経度線が引かれないため、90.1としている - 例:経度線・緯度線を細い赤破線で描く
gl = ax.gridlines(crs=ccrs.PlateCarree(), linewidth=0.5, linestyle='--', color='r')
線の幅はlinewidth、線種はlinestyle、色はcolorで設定
船種は線種一覧、色の名前は色の名前一覧参照 - 例:経度線・緯度線を不透明度0.8の黒点線で描く
gl = ax.gridlines(crs=ccrs.PlateCarree(), linewidth=1, linestyle=':', color='k', alpha=0.8)
不透明度は、透明0.0〜不透明1.0までの範囲で指定する
cartopyで海岸線を描く
- ax.coastlinesを使った方法
ax.coastlines()
- cartopy.featureを使った方法
import cartopy.feature as cfeature ax.add_feature(cfeature.COASTLINE)
- 例:海岸線を細くする
ax.add_feature(cfeature.COASTLINE, linewidth=0.8)
- 例:海岸線を細い破線にする
ax.add_feature(cfeature.COASTLINE, linewidth=0.8, linestyle='--')
- 例:海岸線を細くする
cartopyで陸と海、湖を塗り分けて描く
cartopy.featureを使う
import cartopy.feature as cfeature ax.add_feature(cfeature.LAND) # 陸に色を付ける ax.add_feature(cfeature.OCEAN) # 海に色を付ける ax.add_feature(cfeature.LAKES) # 湖を描く
- 例:陸を緑色、海と湖を水色にする
ax.add_feature(cfeature.LAND, color='g') # 陸を緑色で塗り潰す ax.add_feature(cfeature.OCEAN, color='aqua') # 海を水色で塗り潰す ax.add_feature(cfeature.LAKES, color='aqua') # 湖を水色で塗り潰す
色の名前は色の名前一覧参照 - 例:経度線・緯度線を海上だけに描く(zorderで描く順序を指定)
gl = ax.gridlines(crs=ccrs.PlateCarree(), zorder=2) # 経度線・緯度線を描く gl.xlocator = mticker.FixedLocator(経度線を引く値のリスト) gl.ylocator = mticker.FixedLocator(緯度線を引く値のリスト) ax.add_feature(cfeature.LAND, zorder=3) # 陸に色を付ける ax.add_feature(cfeature.OCEAN, zorder=1) # 海に色を付ける
経度線・緯度線については、cartopyで経度線・緯度線を描く参照
cartopyで国境線を描く
cartopy.featureを使い、陸上に国境線を描く
import cartopy.feature as cfeature ax.add_feature(cfeature.BORDERS) # 国境線を描く
- 例:国境線を細い破線にする
ax.add_feature(cfeature.BORDERS, linestyle='--', linewidth=0.8)
色を変更するオプションもあるが、意図しない場所が塗り潰されてしまうので使わない方が良い
cartopyで河川を描く
cartopy.featureを使う
import cartopy.feature as cfeature ax.add_feature(cfeature.RIVERS) # 川を描く
- 例:河川の幅を太くする
ax.add_feature(cfeature.RIVERS, linewidth=1.2)
cartopyの地図
サブプロットを生成するfig.add_subplotで図法を指定する
正距円筒図法
ax = fig.add_subplot(1,1,1, projection=ccrs.PlateCarree())gradsのlatlonやBasemapのprojection='cyl'に相当する図法で、緯度線・経度線が直角かつ等間隔に交差
- 東経180度を中心にする場合
ax = fig.add_subplot(1,1,1, projection=ccrs.PlateCarree(central_longitude=180))
メルカトル図法
ax = fig.add_subplot(1,1,1, projection=ccrs.Mercator(central_longitude=中心の経度, min_latitude=緯度下限, max_latitude=緯度上限))
- 例:南緯60度〜北緯60度まで、全ての経度帯で描く場合
ax = fig.add_subplot(1,1,1, projection=ccrs.Mercator(min_latitude=-60.0, max_latitude=60.0))
- 例:南緯60度〜北緯60度まで、東経180度を中心として全ての緯度帯で描く場合
ax = fig.add_subplot(1,1,1, projection=ccrs.Mercator(central_longitude=180.0, min_latitude=-60.0, max_latitude=60.0))
ポーラーステレオ図法(極投影図法)
- 北極を中心に描く場合
ax = fig.add_subplot(1,1,1, projection=ccrs.NorthPolarStereo()) ax.set_extent([西端の経度, 東端の経度, 南端の緯度, 北端の緯度], ccrs.PlateCarree())
gradsのnpsに相当する図法 - 南極を中心に描く場合
ax = fig.add_subplot(1,1,1, projection=ccrs.SouthPolarStereo()) ax.set_extent([西端の経度, 東端の経度, 南端の緯度, 北端の緯度], ccrs.PlateCarree())
gradsのspsに相当する図法
- 例:南緯90度〜20度まで、全ての経度帯で描く場合
ax = fig.add_subplot(1,1,1, projection=ccrs.SouthPolarStereo()) ax.set_extent([-180, 180, -90, -20], ccrs.PlateCarree())
ランベルト図法(ランベルト正角円錐図法)
ax = fig.add_subplot(1,1,1, projection=ccrs.LambertConformal(central_longitude=中心の経度, central_latitude=中心の緯度)) ax.set_extent([西端の経度, 東端の経度, 南端の緯度, 北端の緯度])
- 例:東経135度、北緯35度を中心に日本周辺だけを描く場合
ax = fig.add_subplot(1,1,1, projection=ccrs.LambertConformal(central_longitude=135.0, central_latitude=35.0)) ax.set_extent([100, 170, 10, 70]) # 領域の限定
正射投影図法(平射図法)
ax = fig.add_subplot(1,1,1, projection=ccrs.Orthographic(central_longitude=中心の経度, central_latitude=中心の緯度))
- 例:東経180度、北緯45度を中心に描く場合
ax = fig.add_subplot(1,1,1, projection=ccrs.Orthographic(central_longitude=180.0, central_latitude=45.0))
ロビンソン図法
ax = fig.add_subplot(1,1,1, projection=ccrs.Robinson(central_longitude=中心の経度))
- 例:東経180度を中心に描く場合
ax = fig.add_subplot(1,1,1, projection=ccrs.Robinson(central_longitude=180.0))
モルワイデ図法
ax = fig.add_subplot(1,1,1, projection=ccrs.Mollweide(central_longitude=中心の経度))
- 例:東経180度を中心に描く場合
ax = fig.add_subplot(1,1,1, projection=ccrs.Mollweide(central_longitude=180.0))
ランベルト正積円筒図法
ax = fig.add_subplot(1,1,1, projection=ccrs.LambertCylindrical(central_longitude=中心の経度))
- 例:東経180度を中心に描く場合
ax = fig.add_subplot(1,1,1, projection=ccrs.LambertCylindrical(central_longitude=180.0))
ミラー図法
ax = fig.add_subplot(1,1,1, projection=ccrs.Miller(central_longitude=中心の経度))
- 例:東経180度を中心に描く場合
ax = fig.add_subplot(1,1,1, projection=ccrs.Miller(central_longitude=180.0))
cartopyのデータプロット
経度・緯度データの作成
nlonsが経度方向のデータ数、nlatsが緯度方向のデータ数で、全球の格子点データの場合。緯度方向は北極が先、南極が後の場合。
1次元目が緯度、2次元目が経度データになる
delta = 360. / (nlons - 1) lats = (90. - delta * np.indices((nlats, nlons))[0, :, :])Numpyのnp.indicesを使い、経度(lons)、緯度(lats)データを作成し、図法に対応した(x, y)データに変換する(Basemapの経度・緯度データの作成と同じ)
1次元目が緯度、2次元目が経度データになる
等高線を描く
ax.contour(経度データ, 緯度データ, 等高線のデータ)オプションや調整方法は、matplotlibの等高線参照
- 例:作成した経度・緯度データと2次元の等高線データ(d)で作図
ax.contour(x, y, d)
- 例:等高線の幅を細くする
ax.contour(x, y, d, linewidths=0.8)
デフォルト値:1
linewidthではなくlinewidthsなので注意 - 例:等高線を描く値を指定する
ax.contour(x, y, slp_jul, clevs, levels=[0, 1, 2, 3, 4, 5])
等高線は0から5まで1毎に描く(levelsで指定) - 例:等高線を2から20まで2毎等間隔に描く
clevs = np.arange(2, 20, 2) # 値のリスト作成 cs = ax.contour(x, y, d, levels=clevs)
またはclevs = np.arange(2, 20, 2) # 値のリスト作成 ax.contour(x, y, d, clevs)
- 例:データの最小値から最大値の範囲内で2毎等間隔に描く
clevs = np.arange(np.floor(d.min()), np.ceil(d.max()), 2) # 値のリスト作成 cs = ax.contour(x, y, d, levels=clevs) # 等高線を描く
dがNumpyのndarrayの場合。最小値よりも小さい最大の整数をnp.floor(d.min())、最大値よりも大きい最小の整数をnp.ceil(d.max())で取得、Numpyの数学関数、Numpyの統計処理参照
等高線ラベルを付ける
cs = ax.contour(経度データ, 緯度データ, 等高線のデータ) clevels = cs.levels # ラベルを付ける値 cs.clabel(clevels) # 等高線ラベル
- 例:文字サイズを指定して等高線ラベルを付ける
cs = ax.contour(x, y, d, levels=[0, 1, 2, 3, 4, 5]) clevels = cs.levels # ラベルを付ける値 cs.clabel(clevels, fontsize=12) # 等高線ラベル
ax.contourの戻り値を使い、等高線のラベルを付ける - 例:ラベルのフォーマット、文字サイズを指定する
cs = ax.contour(x, y, d, levels=[0, 1, 2, 3, 4, 5]) clevels = cs.levels # ラベルを付ける値 cs.clabel(clevels, fontsize=12, fmt="%d") # 等高線ラベル
文字の大きさはfontsize、ラベルの数値のフォーマットはfmtで指定する(ここでは整数値にするためfmt="%d")。 - 例:等高線のラベルを1つ飛ばしに付ける
cs = ax.contour(x, y, d, levels=clevs) clevels = cs.levels # ラベルを付ける値 cs.clabel(clevels[::2]) # 等高線ラベル
cs.clabelにラベルを付ける値をclevels[::2]で与えることで、等高線のラベルを1つ飛ばしに付ける - 例:データの最小値から最大値の範囲内で1.5毎等間隔に描きラベルを付ける
levels = np.arange(np.floor(d.min()), np.ceil(d.max()), 1.5) # 値のリスト作成 cs = ax.contour(x, y, d, levels=clevs) clevels = cs.levels # ラベルを付ける値 cs.clabel(clevels, fontsize=12, fmt="%.1f") # 等高線ラベル
ラベルは1.5毎に付け、小数点以下第一位まで表示(fmt="%.1f")
陰影を描く
ax.contourf(経度データ, 緯度データ, 陰影のデータ)オプションや調整方法は、matplotlibの陰影参照
- 例:作成した経度・緯度データと2次元の等高線データ(d)で作図
ax.contourf(x, y, d)
- 例:陰影を描く値を指定する
ax.contour(x, y, slp_jul, clevs, levels=[0, 1, 2, 3, 4, 5, 6])
等高線は0から5まで1毎に描く(levelsで指定、描く値より1大きく設定する) - 例:陰影の色テーブルを指定する(青〜白〜赤と変化)
ax.contour(x, y, slp_jul, clevs, cmap='bwr')
色テーブルは色テーブルの一覧参照
陰影にカラーバーを付ける
cs = ax.contourf(経度データ, 緯度データ, 陰影のデータ) cbar = ax.colorbar(cs) cbar.set_label('カラーバーのラベルに表示する文字列')カラーバーの調整方法については、カラーバー参照
(plt.colorbarと同じオプションを使用可能)
矢羽をプロットする
ax.barbs(経度データ, 緯度データ, 東西風データ, 南北風データ)矢羽の調整方法については、matplotlibの矢羽参照
- 例:2次元データlons, lats, u, vから矢羽をプロット
ax.barbs(x, y, u, v)
- 例:矢羽を6おきにプロット
ax.barbs(lons[::6,::6],lats[::6,::6],uwnd[::6,::6],vwnd[::6,::6])
- 例:矢羽の色を青色にする
ax.barbs(x, y, u, v, color='b')
色の名前は色の名前一覧参照 - 例:矢羽の長さを4にする
ax.barbs(x, y, u, v, length=4)
文字をプロットする
ax.textを使う
テキストの調整方法については、matplotlibのテキスト参照
ax.text(x座標, y座標, "表示するテキスト")
- 例:フォントサイズを9に指定する場合
ax.text(x, y, "表示するテキスト", fontsize=9)
- 例:テキストの色を赤にする場合
ax.text(x, y, "表示するテキスト", color='r')
マーカーをプロットする
ax.plot(x座標, y座標, オプション)
- 例:マーカーを白色の丸にする場合
ax.plot(x, y, marker="o", color="w")
マーカーの名前は指定可能なマーカーの一覧参照
色の名前は色の名前一覧参照 - 例:マーカーをマゼンタの三角にする場合
ax.plot(x, y, marker="m", color="^")
- 例:マーカーを水色の丸にする場合
ax.plot(x, y, color="aqua", marker="o")
- 例:マーカーサイズを大きくする場合
ax.plot(x, y, color="aqua", marker="o", markersize=12)
デフォルト値:6
図の体裁
タイトルを付ける
極投影図法で図枠を円形にする
図枠をmatplotlibのPathで設定する
cartopyのサンプル参照
import matplotlib.path as mpath center, radius = [0.5, 0.5], 0.5 # 円の中心座標と半径 theta = np.linspace(0, 2*np.pi, 100) # 0〜2πの位相 verts = np.vstack([np.sin(theta), np.cos(theta)]).T # 円形の座標作成 circle = mpath.Path(verts * radius + center) # 中心座標(0.5,0.5)、半径0.5の円形 ax.set_boundary(circle, transform=ax.transAxes) # 円形の領域でマスクサブプロットaxが座標(0,0)、(0,1)、(1,1)、(1,0)を頂点とする四角形なので、それに内接する円形の領域でマスクする
cartopyのサンプル参照
全球が表示されなくなった場合
全球を表示させるために、次の記述を加える
ax.set_global()
0度と360度を滑らかにつなげる
- 経度、緯度が1次元データの場合
cartopy.utilのadd_cyclic_pointを使うfrom cartopy.util import add_cyclic_point d_cyclic, lons_cyclic = add_cyclic_point(d, coord=lons) # 0度の値を360度にコピー ax.contourf(lons_cyclic, lats, d_cyclic)
*2次元データd(緯度方向, 経度方向)、1次元の経度・緯度データlons、lats - 経度、緯度が2次元データの場合
Basemapの極投影図法で0度と360度を滑らかにつなげる参照