使用Python在 Plotly 中简化绘图
Plotly 是一个Python库,用于设计图形,尤其是交互式图形。它可以绘制各种图形和图表,如直方图、条形图、箱线图、散布图等等。它主要用于数据分析和财务分析。 plotly 是一个交互式可视化库。
在 Plotly 中简化绘图
在 plotly 中,流线图基于二维矢量场的表示,该矢量场被解释为速度场,它由与速度场相切的闭合曲线组成。流线化是获取数据的最快和更有效的技术。确定流线时会插入速度值。流线在 xy 域的边界上初始化。
Syntax: create_streamline(x, y, u, v, density=1, angle=0.3490658503988659, arrow_scale=0.09)
Parameters:
x: 1 dimensional, evenly spaced list or array
y: 1 dimensional, evenly spaced list or array
u: 2 dimensional array
v: 2 dimensional array
density: controls the density of streamlines in plot. This is multiplied by 30 to scale similiarly to other available streamline functions such as matplotlib. Default = 1
angle: angle of arrowhead. Default = pi/9
arrow_scale: value to scale length of arrowhead Default = .09
例子:
Python3
import plotly.figure_factory as ff
import numpy as np
x = np.linspace(-1, 1, 10)
y = np.linspace(-1, 1, 10)
Y, X = np.meshgrid(x, y)
u = 1 - X**2 + Y
v = -1 + X - Y**2
# Create streamline figure
fig = ff.create_streamline(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(-1, 2, 50)
y = np.linspace(-1, 1, 50)
Y, X = np.meshgrid(x, y)
u = np.cos(X)*Y
v = np.cos(y)*X
# Source for x and y coordinate
# of scatter plot
X, Y = 0, 0
# Create streamline figure
fig = ff.create_streamline(x, y, u, v, arrow_scale=.1)
fig.add_trace(go.Scatter(x=[X], y=[Y],
mode='markers',
marker_size=15,
))
fig.show()
输出:
绘制源点
可以使用 graph_objects 类的 scatterplot() 方法绘制源点。在此,流线图和散点图中标记的位置进行了调整,以使所有流似乎都来自特定源。
例子:
Python3
import plotly.figure_factory as ff
import plotly.graph_objects as go
import numpy as np
x = np.linspace(-1, 2, 50)
y = np.linspace(-1, 1, 50)
Y, X = np.meshgrid(x, y)
u = np.cos(X)*Y
v = np.cos(y)*X
# Source for x and y coordinate
# of scatter plot
X, Y = 0, 0
# Create streamline figure
fig = ff.create_streamline(x, y, u, v, arrow_scale=.1)
fig.add_trace(go.Scatter(x=[X], y=[Y],
mode='markers',
marker_size=15,
))
fig.show()
输出: