gpt4 book ai didi

python - 如何在 python 中使用 plotly 制作混合统计子图?

转载 作者:行者123 更新时间:2023-11-28 16:55:30 26 4
gpt4 key购买 nike

我在 csv 文件中有一些数据集(总共 3 个),需要以不同的方式表示它。它们必然是带有 kde(核密度估计)的折线图、箱线图和直方图。

我知道如何单独绘制它们,但为了更方便,我需要将它们合并为一个输出。查阅引用资料后,我确实写了一些代码,但没有运行。

import plotly.graph_objects as go
from plotly.subplots import make_subplots
import plotly.figure_factory as ff
import numpy as np

y1 = np.random.randn(200) - 1
y2 = np.random.randn(200)
y3 = np.random.randn(200) + 1
x = np.linspace(0, 1, 200)

fig = make_subplots(
rows=3, cols=2,
column_widths=[0.6, 0.4],
row_heights=[0.3, 0.6],
specs=[[{"type": "scatter"}, {"type": "box"}],
[{"type": "scatter"}, {"type": "dist", "rowspan": 2}]
[{"type": "scatter"}, None ]])

fig.add_trace(
go.Scatter(x = x,
y = y1,
hoverinfo = 'x+y',
mode='lines',
line=dict(color='rgb(0, 0, 0)',
width=1),
showlegend=False,
)
row=1, col=1
)

fig.add_trace(
go.Scatter(x = x,
y = y2,
hoverinfo = 'x+y',
mode='lines',
line=dict(color='rgb(246, 52, 16)',
width=1),
showlegend=False,
)
row=2, col=1
)

fig.add_trace(
go.Scatter(x = x,
y = y3,
hoverinfo = 'x+y',
mode='lines',
line=dict(color='rgb(16, 154, 246)',
width=1),
showlegend=False,
)
row=3, col=1
)

fig.add_trace(
go.Box(x=y1)
go.Box(x=y2)
go.Box(x=y3)
row=1, col=2
)

hist_data = [y1, y2, y3]

fig.add_trace(
ff.create_distplot(hist_data,
bin_size=.02, show_rug=False)

row=2, col=2
)

fig.show()

上面的代码有什么问题,或者我怎样才能用唯一的输出绘制这些图表?

附言折线图需要分开以便更好地可视化。

最佳答案

我发布了同样的 question在 plotly 论坛上,用户 empet , 优雅地回答。

正如我所怀疑的,make_subplots() 无法处理图形对象,要走的路是“一次将图形数据添加为单个轨迹”。

剧情: mixed-statistical-subplots代码:

import plotly.graph_objects as go
from plotly.subplots import make_subplots
import plotly.figure_factory as ff
import numpy as np

y1 = np.random.randn(200) - 1
y2 = np.random.randn(200)
y3 = np.random.randn(200) + 1
x = np.linspace(0, 1, 200)

colors = ['#3f3f3f', '#00bfff', '#ff7f00']

fig = make_subplots(
rows=3, cols=2,
column_widths=[0.55, 0.45],
row_heights=[1., 1., 1.],
specs=[[{"type": "scatter"}, {"type": "xy"}],
[{"type": "scatter"}, {"type": "xy", "rowspan": 2}],
[{"type": "scatter"}, None ]])

fig.add_trace(
go.Scatter(x = x,
y = y1,
hoverinfo = 'x+y',
mode='lines',
line=dict(color='#3f3f3f',
width=1),
showlegend=False,
),
row=1, col=1
)

fig.add_trace(
go.Scatter(x = x,
y = y2,
hoverinfo = 'x+y',
mode='lines',
line=dict(color='#00bfff',
width=1),
showlegend=False,
),
row=2, col=1
)

fig.add_trace(
go.Scatter(x = x,
y = y3,
hoverinfo = 'x+y',
mode='lines',
line=dict(color='#ff7f00',
width=1),
showlegend=False,
),
row=3, col=1
)

boxfig= go.Figure(data=[go.Box(x=y1, showlegend=False, notched=True, marker_color="#3f3f3f", name='3'),
go.Box(x=y2, showlegend=False, notched=True, marker_color="#00bfff", name='2'),
go.Box(x=y3, showlegend=False, notched=True, marker_color="#ff7f00", name='1')])

for k in range(len(boxfig.data)):
fig.add_trace(boxfig.data[k], row=1, col=2)

group_labels = ['Group 1', 'Group 2', 'Group 3']
hist_data = [y1, y2, y3]

distplfig = ff.create_distplot(hist_data, group_labels, colors=colors,
bin_size=.2, show_rug=False)

for k in range(len(distplfig.data)):
fig.add_trace(distplfig.data[k],
row=2, col=2
)
fig.update_layout(barmode='overlay')
fig.show()

关于python - 如何在 python 中使用 plotly 制作混合统计子图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58770063/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com