在Python中使用 Plotly 绘制箭袋图
Plotly 是一个Python库,用于设计图形,尤其是交互式图形。它可以绘制各种图形和图表,如直方图、条形图、箱线图、散布图等等。它主要用于数据分析和财务分析。 plotly 是一个交互式可视化库。
箭袋图
颤动图将速度矢量显示为箭头,在点 (x, y) 处具有分量 (u, v)。 quiver(x, y, u, v) 在 x 和 y 中的每对对应元素中指定的坐标处将向量绘制为箭头。使用箭袋图的主要优点是它可以表示更大范围的量级,而不会使箭头缩小为点或彼此重叠。
Syntax: create_quiver(x, y, u, v, scale=0.1, arrow_scale=0.3, angle=0.3490658503988659, scaleratio=None)
Parameters:
x: x coordinates of the arrow locations
y: y coordinates of the arrow locations
u: x components of the arrow vectors
v: y components of the arrow vectors
arrow_scale: value multiplied to length of barb to get length of arrowhead. Default = .3
angle: angle of arrowhead. Default = pi/9
例子:
Python3
import plotly.figure_factory as ff
import numpy as np
x = np.linspace(-2, 2, 60)
y = np.linspace(-1, 1, 60)
Y, X = np.meshgrid(x, y)
u = 1 - X**2 + Y
v = -1 + X - Y**2
# Create quiver plot
fig = ff.create_quiver(x, y, u, v, arrow_scale=.1)
fig.show()
Python3
import plotly.figure_factory as ff
import plotly.graph_objects as go
import numpy as np
x = np.linspace(-2, 2, 60)
y = np.linspace(-1, 1, 60)
Y, X = np.meshgrid(x, y)
u = np.cos(X)*Y
v = np.sin(X)*Y
# Create quiver plot
fig = ff.create_quiver(x, y, u, v, arrow_scale=.1)
# Adding scatter as the origin
fig.add_trace(go.Scatter(x = [0], y = [0],
mode = 'markers',
marker_size = 15
))
fig.show()
输出:
用 Quiver Plot 绘制点
在 graph_objects 类的add_trace()方法的帮助下,可以用点显示一个颤动图。散点图的添加方式显示了箭袋的原点。
例子:
Python3
import plotly.figure_factory as ff
import plotly.graph_objects as go
import numpy as np
x = np.linspace(-2, 2, 60)
y = np.linspace(-1, 1, 60)
Y, X = np.meshgrid(x, y)
u = np.cos(X)*Y
v = np.sin(X)*Y
# Create quiver plot
fig = ff.create_quiver(x, y, u, v, arrow_scale=.1)
# Adding scatter as the origin
fig.add_trace(go.Scatter(x = [0], y = [0],
mode = 'markers',
marker_size = 15
))
fig.show()
输出: