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 3 years have passed since last update.

Plotlyのquiver plotで遊んでみた

Posted at

はじめに

plotlyを使ってみたいなと思って検索したら、quiver plotなるものを発見。
面白そうなのとあまり日本語訳で書かれた資料がなさそうだったので、遊び半分でトライしました。

環境

Colab環境下で実施

内容

渦っぽいグラフを描いてみました。

import plotly.figure_factory as ff
import numpy as np
import math

x,y = np.meshgrid(np.arange(-1, 1, 0.25), np.arange(-1, 1, 0.25))

r = np.sqrt(x**2+y**2)
u = -y*np.exp(r)
v = x*np.exp(r)

fig = ff.create_quiver(x, y, u, v)
fig.update_layout(width=500, height=500,xaxis_range=[-1,1],yaxis_range=[-1,1]) 

fig.show()

image.png

各要素の意味を表にまとめました。

パラメータ 意味 追記
x 矢印のx座標位置 listかndarrayで定義
y 矢印のy座標位置
u 矢印のベクトルx座標要素
v 矢印のベクトルy座標要素
scale 矢印のサイズ 0~1までの範囲。デフォルトは0.1。
arrow_scale 矢じりあごの長さ デフォルトは0.3
angle 矢じりの角度 デフォルトはpi/9
scaleratio x軸に対するy軸の比 デフォルトはNone

ちなみに矢じりあごは赤丸のところ。
無題.png

①ベクトル要素の変更

import plotly.figure_factory as ff
import numpy as np
import math

x,y = np.meshgrid(np.arange(-1, 1, 0.25), np.arange(-1, 1, 0.25))

r = np.sqrt(x**2+y**2)
u = -y*np.exp(r)*np.cos(x)
v = x*np.exp(r)*np.sin(y)

fig = ff.create_quiver(x, y, u, v,)
fig.update_layout(width=500, height=500,xaxis_range=[-1,1],yaxis_range=[-1,1]) 

fig.show()

image.png

②矢印の変更

②-1 サイズ

import plotly.figure_factory as ff
import numpy as np
import math

x,y = np.meshgrid(np.arange(-1, 1, 0.25), np.arange(-1, 1, 0.25))

r = np.sqrt(x**2+y**2)
u = -y*np.exp(r)
v = x*np.exp(r)

# 矢印サイズの変更
fig = ff.create_quiver(x, y, u, v, scale=0.5)
fig.update_layout(width=500, height=500,xaxis_range=[-1,1],yaxis_range=[-1,1]) 

fig.show()

大きくしすぎてちょっと見にくい。
image.png

②-2 矢じりの変更

import plotly.figure_factory as ff
import numpy as np
import math

x,y = np.meshgrid(np.arange(-1, 1, 0.25), np.arange(-1, 1, 0.25))

r = np.sqrt(x**2+y**2)
u = -y*np.exp(r)
v = x*np.exp(r)

# 矢じりの変更
fig = ff.create_quiver(x, y, u, v, arrow_scale=0.7, angle=math.pi/4)
fig.update_layout(width=500, height=500,xaxis_range=[-1,1],yaxis_range=[-1,1]) 

fig.show()

矢じりを長くして角度も広げたら、ちょっとコミカルに。
image.png

③ スケールの変更

import plotly.figure_factory as ff
import numpy as np
import math

x,y = np.meshgrid(np.arange(-1, 1, 0.25), np.arange(-1, 1, 0.25))

r = np.sqrt(x**2+y**2)
u = -y*np.exp(r)
v = x*np.exp(r)

# スケールの変更
fig = ff.create_quiver(x, y, u, v, scaleratio=0.5)
fig.update_layout(width=500, height=500,xaxis_range=[-1,1],yaxis_range=[-1,1]) 

fig.show()

y軸方向のサイズが圧縮されて、歪になった。
image.png

まとめ

英語の資料が多くてちょっと苦労したけど、quiver plotを試すことができました。
ただ3Dは対応していないのが残念(matplotlibを使うかPlotlyのcone plotを使うしか、今のところないらしい)。

参考

渦のグラフ
https://physics-school.com/grad-div-rot/

Plotlyについて
https://ai-research-collection.com/plotly-quiver-plots/
https://plotly.github.io/plotly.py-docs/generated/plotly.figure_factory.create_quiver.html

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?