在Python中使用 Plotly 绘制地毯图
Plotly 是一个Python库,用于设计图形,尤其是交互式图形。它可以绘制各种图形和图表,如直方图、条形图、箱线图、展开图等等。它主要用于数据分析以及财务分析。 Plotly 是一个交互式可视化库。
地毯地块
地毯图是几种不同的特定类型的图中的任何一种。地毯图是一种在二维图中显示两个或多个自变量与一个或多个因变量之间的相互作用的图。地毯图与其他图不同,因为地毯图可用于更准确地插入数据点。它可以使用 graph_objects 类的Carpet()方法创建。
Syntax: plotly.graph_objects.Carpet(a=None,b=None,carpet=None, x=None, y=None)
Parameters:
a: An array containing values of the first parameter value
b: An array containing values of the first parameter value
x: A two dimensional array of x coordinates at each carpet point. If ommitted, the plot is a cheater plot and the xaxis is hidden by default.
y: A two dimensional array of y coordinates at each carpet point.
carpet: An identifier for this carpet, so that scattercarpet and contourcarpet traces can specify a carpet plot on which they lie
例子:
Python3
import plotly.graph_objects as go
fig = go.Figure(go.Carpet(
y = [1, 2, 3, 4, 5, 6]
))
fig.show()
Python3
import plotly.graph_objects as go
fig = go.Figure(go.Carpet(
a = [1, 2, 3, 1, 2, 3],
b = [4, 5, 6, 4, 5, 6],
y = [1, 2, 3, 4, 5, 6]
))
fig.show()
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(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.update_layout(
plot_bgcolor='black',
paper_bgcolor='black',
xaxis=dict(
showgrid=False,
showticklabels=False
),
yaxis=dict(
showgrid=False,
showticklabels=False
)
)
fig.show()
输出:
添加 a 和 b 值
应使用 A 和 B 属性来保存参数值。
例子:
蟒蛇3
import plotly.graph_objects as go
fig = go.Figure(go.Carpet(
a = [1, 2, 3, 1, 2, 3],
b = [4, 5, 6, 4, 5, 6],
y = [1, 2, 3, 4, 5, 6]
))
fig.show()
输出:
设置A和B轴
aaxis和baxis列表分别用于设置 A 轴和 B 轴。
示例 1:
蟒蛇3
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()
输出:
示例 2:样式 A 和 B 轴
蟒蛇3
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.update_layout(
plot_bgcolor='black',
paper_bgcolor='black',
xaxis=dict(
showgrid=False,
showticklabels=False
),
yaxis=dict(
showgrid=False,
showticklabels=False
)
)
fig.show()
输出: