gpt4 book ai didi

python - plotly :如何结合 make_subplots() 和 ff.create_distplot()?

转载 作者:行者123 更新时间:2023-12-04 11:45:24 25 4
gpt4 key购买 nike

使用 plotly 创建多个子图既简单又优雅。考虑以下示例,该示例并排绘制来自数据帧的两个系列:

剧情:

enter image description here

代码:

# imports
from plotly.subplots import make_subplots
import plotly.figure_factory as ff
import plotly.graph_objs as go
import pandas as pd
import numpy as np

# data
np.random.seed(123)
frame_rows = 40
n_plots = 6
#frame_columns = ['V_'+str(e) for e in list(range(1,n_plots+1))]
frame_columns = ['V_1', 'V_2']
df = pd.DataFrame(np.random.uniform(-10,10,size=(frame_rows, len(frame_columns))),
index=pd.date_range('1/1/2020', periods=frame_rows),
columns=frame_columns)
df=df.cumsum()+100
df.iloc[0]=100

# plotly setup
plot_rows=1
plot_cols=2
fig = make_subplots(rows=plot_rows, cols=plot_cols)

# plotly traces
fig.add_trace(go.Scatter(x=df.index, y=df['V_1']), row=1, col=1)
fig.add_trace(go.Scatter(x=df.index, y=df['V_2']), row=1, col=2)


fig.show()

更换 go.Scatter()对象 similar objects简单:

剧情:

enter image description here

但我似乎找不到将这个设置与 ff.create_distplot() 结合起来的方法。 :

分布图:

enter image description here

带有 distplot 的代码:
# imports
from plotly.subplots import make_subplots
import plotly.figure_factory as ff
import plotly.graph_objs as go
import pandas as pd
import numpy as np

# data
np.random.seed(123)
frame_rows = 40
n_plots = 6
#frame_columns = ['V_'+str(e) for e in list(range(1,n_plots+1))]
frame_columns = ['V_1', 'V_2']
df = pd.DataFrame(np.random.uniform(-10,10,size=(frame_rows, len(frame_columns))),
index=pd.date_range('1/1/2020', periods=frame_rows),
columns=frame_columns)
df=df.cumsum()+100
df.iloc[0]=100

# plotly setup
plot_rows=1
plot_cols=2
fig = make_subplots(rows=plot_rows, cols=plot_cols)

# plotly traces
fig.add_trace(go.Scatter(x=df.index, y=df['V_1']), row=1, col=1)
#fig.add_trace(go.Scatter(x=df.index, y=df['V_2']), row=1, col=2)

# distplot
hist_data = [df['V_1'].values, df['V_2'].values]
group_labels = ['Group 1', 'Group 2']
#fig2 = ff.create_distplot(hist_data, group_labels)

# combine make_subplots, go.Scatter and ff.create_distplot(
fig.add_trace(ff.create_distplot(hist_data, group_labels), row=1, col=2)

fig.show()

这会引发相当大的 ValueError。

原因好像是 go.Scatter()ff.create_distplot()返回两种不同的数据类型; plotly.graph_objs.Scatterplotly.graph_objs._figure.Figure , 分别。而且似乎确实 make_subplots不会与后者一起工作。或者有人知道解决这个问题的方法吗?

感谢您的任何建议!

最佳答案

事实证明,您不能直接执行此操作,因为 make_subplots()不接受 plotly.graph_objs._figure.Figure对象作为 add_trace() 的参数直接地。但是你可以建立一个 ff.create_distplot ' 和 “偷”来自该图中的数据并将它们应用于 go.Histogram 的组合中和 go.Scatter()对象 被接受 make_subplots() .您甚至可以对 rug/margin plot 做同样的事情。

剧情:

enter image description here

代码:

# imports
from plotly.subplots import make_subplots
import plotly.figure_factory as ff
import plotly.graph_objs as go
import pandas as pd
import numpy as np

# data
np.random.seed(123)
frame_rows = 40
n_plots = 6
#frame_columns = ['V_'+str(e) for e in list(range(1,n_plots+1))]
frame_columns = ['V_1', 'V_2']
df = pd.DataFrame(np.random.uniform(-10,10,size=(frame_rows, len(frame_columns))),
index=pd.date_range('1/1/2020', periods=frame_rows),
columns=frame_columns)
df=df.cumsum()+100
df.iloc[0]=100

# plotly setup
plot_rows=2
plot_cols=2
fig = make_subplots(rows=plot_rows, cols=plot_cols)

# plotly traces
fig.add_trace(go.Scatter(x=df.index, y=df['V_1']), row=1, col=1)
fig.add_trace(go.Scatter(x=df.index, y=df['V_2']), row=2, col=1)

# distplot
hist_data = [df['V_1'].values, df['V_2'].values]
group_labels = ['Group 1', 'Group 2']
fig2 = ff.create_distplot(hist_data, group_labels)

fig.add_trace(go.Histogram(fig2['data'][0],
marker_color='blue'
), row=1, col=2)

fig.add_trace(go.Histogram(fig2['data'][1],
marker_color='red'
), row=1, col=2)

fig.add_trace(go.Scatter(fig2['data'][2],
line=dict(color='blue', width=0.5)
), row=1, col=2)

fig.add_trace(go.Scatter(fig2['data'][3],
line=dict(color='red', width=0.5)
), row=1, col=2)

# rug / margin plot to immitate ff.create_distplot
df['rug 1'] = 1.1
df['rug 2'] = 1
fig.add_trace(go.Scatter(x=df['V_1'], y = df['rug 1'],
mode = 'markers',
marker=dict(color = 'blue', symbol='line-ns-open')
), row=2, col=2)

fig.add_trace(go.Scatter(x=df['V_2'], y = df['rug 2'],
mode = 'markers',
marker=dict(color = 'red', symbol='line-ns-open')
), row=2, col=2)

# some manual adjustments on the rugplot
fig.update_yaxes(range=[0.95,1.15], tickfont=dict(color='rgba(0,0,0,0)', size=14), row=2, col=2)
fig.update_layout(showlegend=False)

fig.show()

关于python - plotly :如何结合 make_subplots() 和 ff.create_distplot()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58803324/

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