作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
使用 plotly 创建多个子图既简单又优雅。考虑以下示例,该示例并排绘制来自数据帧的两个系列:
剧情:
代码:
# 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简单:
ff.create_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()
go.Scatter()
和
ff.create_distplot()
返回两种不同的数据类型;
plotly.graph_objs.Scatter
和
plotly.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 做同样的事情。
剧情:
代码:
# 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/
使用 plotly 创建多个子图既简单又优雅。考虑以下示例,该示例并排绘制来自数据帧的两个系列: 剧情: 代码: # imports from plotly.subplots import make_
我是一名优秀的程序员,十分优秀!