在Python中使用 Plotly 绘制地毯轮廓图
Plotly 是一个Python库,用于设计图形,尤其是交互式图形。它可以绘制各种图形和图表,如直方图、条形图、箱线图、展开图等等。它主要用于数据分析以及财务分析。 plotly 是一个交互式可视化库。
地毯轮廓图
地毯等高线图是一种通过在二维格式上绘制恒定 z 切片(称为等高线)来表示 3 维表面的图形技术。 X 和 Y 属性用于设置 x 和 y 坐标。如果 x 的值缺失,则会形成作弊图。应该使用 A 和 B 属性来保存参数的值。
Syntax: plotly.graph_objects.Contourcarpet( a=None,b=None,xaxis=None, yaxis=None, z=None)
参数:Name Description a Sets the x coordinates. b Sets the y coordinates. xaxis Sets a reference between this trace’s x coordinates and a 2D cartesian x axis. If “x” (the default value), the x coordinates refer to layout.xaxis. If “x2”, the x coordinates refer to layout.xaxis2 yaxis Sets a reference between this trace’s y coordinates and a 2D cartesian y axis. If “y” (the default value), the y coordinates refer to layout.yaxis. If “y2”, the y coordinates refer to layout.yaxis2 z Sets the z data.
例子:
Python3
import plotly.graph_objects as go
fig = go.Figure(go.Carpet(
a=[1, 2, 3, 4, 5, 6],
b=[6, 5, 4, 3, 2, 1],
y=[1, 2, 3, 4, 5, 6],
aaxis=dict(
tickprefix='F = ',
ticksuffix='N',
smoothing=0.2,
minorgridcount=10,
),
baxis=dict(
tickprefix='P = ',
ticksuffix='pa',
smoothing=0.4,
minorgridcount=9,
)
))
fig.show()
Python3
import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Contourcarpet(
a = [1, 2, 3, 4, 5, 6],
b = [6, 5, 4, 3, 2, 1],
z = [1, 1.96, 5, 6.1028, 4, 5.0625],
autocontour = False,
contours = dict(
start = 1,
end = 14,
size = 1
),
line = dict(
width = 2,
smoothing = 0
),
colorbar = dict(
len = 0.4,
y = 0.25
)
))
fig.add_trace(go.Carpet(
a=[1, 2, 3, 4, 5, 6],
b=[6, 5, 4, 3, 2, 1],
y=[1, 2, 3, 4, 5, 6],
aaxis=dict(
tickprefix='F = ',
ticksuffix='N',
smoothing=0.2,
minorgridcount=10,
),
baxis=dict(
tickprefix='P = ',
ticksuffix='pa',
smoothing=0.4,
minorgridcount=9,
)
))
fig.show()
输出:
添加轮廓
在 plotly 中,可以使用graph_object类的go.Contourcarpet()方法添加轮廓。应以图形主体应位于适当段中的方式添加轮廓。
例子:
蟒蛇3
import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Contourcarpet(
a = [1, 2, 3, 4, 5, 6],
b = [6, 5, 4, 3, 2, 1],
z = [1, 1.96, 5, 6.1028, 4, 5.0625],
autocontour = False,
contours = dict(
start = 1,
end = 14,
size = 1
),
line = dict(
width = 2,
smoothing = 0
),
colorbar = dict(
len = 0.4,
y = 0.25
)
))
fig.add_trace(go.Carpet(
a=[1, 2, 3, 4, 5, 6],
b=[6, 5, 4, 3, 2, 1],
y=[1, 2, 3, 4, 5, 6],
aaxis=dict(
tickprefix='F = ',
ticksuffix='N',
smoothing=0.2,
minorgridcount=10,
),
baxis=dict(
tickprefix='P = ',
ticksuffix='pa',
smoothing=0.4,
minorgridcount=9,
)
))
fig.show()
输出: