0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

quiverを微調整する小技 (Matplotlib + Cartopy)

Last updated at Posted at 2023-09-05

matplotlib + cartopyで地図上にベクトルを描く時,特に複数の図で同じscaleでベクトルを描きたい際に,quiver関数のscale値やwidth値の調節が面倒くさくて気が滅入ってしまうので,その小技を日本語にしておく.

2023/09/27追記
scale値に加えwidth値も同様に_init()により取得可能.

2023/12/08追記
cartopyで地図上にプロットする際はregrid_shapeを先に設定すると吉(ベクトルの描画間隔が変わり、scale値も変わるので)

参考リンク
how to use matplotlib quiver scale - Stack Overflow

q = ax.quiver(x, y, u, v)  # scale=None, width=Noneがデフォルトで設定される

print(q.scale)
# None  # 描画されるquiverのscale値を入手できない

q._init()  # これが大事っぽい

print(q.scale)
# 1.1610226806616049  # scale値を取得!

print(q.width)
# 0.0024  # width値を取得!

axはMatplotlibのaxisインスタンスquiverはベクトル描画のメソッド(詳細は本家こちらを参考にしてほしい).

肝心なのは,普通にq.scaleをするとNone(或いは与えた引数scaleの値)が返るが,_init()をすることによってmatplotlib内で”よろしく”設定してくれたscaleを取得できること.

上記の例では,quit()して作図前にscaleを確かめ(1.1610226806616049),その値を参考にscalewidth引数を設定し,改めて描画する.width値も同様.

q = ax.quiver(x, y, u, v, scale=1.16, width=0.003)

あとは図を見ながらscalewidthの値を微調整すれば良い(scale値を大きくすれば矢印は短くなり,width値を大きくすれば矢印は太くなる).

(length,headlength,headwidth,etc...の詳細設定は本家を参照.width値を基準に相対的に設定されるらしい)

(なお、transform=ccrs.PlateCarree()は省略してるので注意)

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?