[英]Plotly: How to combine make_subplots() and ff.create_distplot()?
[英]plotly make_subplots how to use specs parameter
我正在尝试创建一个包含 2 个子图的图形 (rows=2,cols=1) 其中,我希望第一个子图将是第一行 plot 具有 secondary_y = True 规格。 而第二行 plot 不需要任何 secondary_y 要求。 你能确认我如何使用这个规格参数吗? 我看过https://plotly.com/python-api-reference/generated/plotly.subplots.make_subplots.html文档,但我认为我并不完全理解它。
错误:
ValueError:
The 'specs' argument to make_subplots must be a 2D list of dictionaries with dimensions (2 x 1).
Received value of type <class 'list'>: [[{'secondary_y': True}], [{}, {}]]
你能帮我看看这是怎么回事吗?
fig = make_subplots(rows=2,cols=1,
shared_xaxes = True,vertical_spacing = 0.02,
specs=[[{"secondary_y": True}],
[{},{}]
]
)
fig.add_trace(go.Scatter(x=df['timestamp_gmt'],y=df['mid'],opacity=0.8,
name = "Price"),secondary_y=True,row=1,col=1)
fig.add_trace(go.Scatter(x=df2['date'],y=df2['cnd'],mode='markers',
name="abc"),secondary_y=False,row=1,col=1)
if xlist:
fig.add_annotation(x=xlist[0],y=df2['cnd'].max(),
text="Arrow text", showarrow=True,arrowhead=2,row=1,col=1)
if len(xlist) > 1 :
for x in xlist:
fig.add_vline(x=x, line_width=0.8,opacity = 0.55,row=1,col=1)
else:
fig.add_vline(x=xlist[0],line_width=0.8,opacity=0.55,row=1,col=1)
fig.update_layout(margin=dict(l=30, r=1, t=20, b=20))
fig.update_layout(legend=dict( yanchor="top", y=0.99, xanchor="right", x=0.90 ))
fig.update_yaxes(tickprefix="$")
fig.add_trace(go.Scatter(x=df2_gpby['date'],y=df2_gpby[variable_col],opacity=0.8,
name = "Price"),secondary_y=False,row=2,col=1)
fig.update_xaxes(matches=None, showticklabels=True, visible=True)
fig.update_layout(margin=dict(l=30, r=85, t=30, b=20))
fig.update_layout(legend=dict( yanchor="top", y=0.99, xanchor="right", x=0.99 ))
fig.update_layout(template='plotly_white')
specs=[[{"secondary_y": True}], [{"secondary_y": False}]]
是你所需要的。 2 行,1 列,因此是 2*1 二维列表。import plotly.graph_objects as go
from plotly.subplots import make_subplots
import pandas as pd
import numpy as np
# make code runnable....
df = pd.DataFrame(
{
"timestamp_gmt": pd.date_range("1-jan-2022", periods=50),
"mid": (1 + np.random.normal(loc=0.001, scale=0.01, size=50)).cumprod(),
}
)
df2 = pd.DataFrame(
{
"date": pd.date_range("1-jan-2022", periods=50),
"cnd": 5 * (1 + np.random.normal(loc=0.001, scale=0.01, size=50)).cumprod(),
}
)
variable_col = "price"
df2_gpby = pd.DataFrame(
{
"date": pd.date_range("1-jan-2022", periods=50),
variable_col: 5
* (1 + np.random.normal(loc=0.001, scale=0.01, size=50)).cumprod(),
}
)
xlist = False
# end make code runnable...
fig = make_subplots(
rows=2,
cols=1,
shared_xaxes=True,
vertical_spacing=0.02,
specs=[[{"secondary_y": True}], [{"secondary_y": False}]],
)
fig.add_trace(
go.Scatter(x=df["timestamp_gmt"], y=df["mid"], opacity=0.8, name="Price"),
secondary_y=True,
row=1,
col=1,
)
fig.add_trace(
go.Scatter(x=df2["date"], y=df2["cnd"], mode="markers", name="abc"),
secondary_y=False,
row=1,
col=1,
)
if xlist:
fig.add_annotation(
x=xlist[0],
y=df2["cnd"].max(),
text="Arrow text",
showarrow=True,
arrowhead=2,
row=1,
col=1,
)
if len(xlist) > 1:
for x in xlist:
fig.add_vline(x=x, line_width=0.8, opacity=0.55, row=1, col=1)
else:
fig.add_vline(x=xlist[0], line_width=0.8, opacity=0.55, row=1, col=1)
fig.update_layout(margin=dict(l=30, r=1, t=20, b=20))
fig.update_layout(legend=dict(yanchor="top", y=0.99, xanchor="right", x=0.90))
fig.update_yaxes(tickprefix="$")
fig.add_trace(
go.Scatter(x=df2_gpby["date"], y=df2_gpby[variable_col], opacity=0.8, name="Price"),
secondary_y=False,
row=2,
col=1,
)
fig.update_xaxes(matches=None, showticklabels=True, visible=True)
fig.update_layout(margin=dict(l=30, r=85, t=30, b=20))
fig.update_layout(legend=dict(yanchor="top", y=0.99, xanchor="right", x=0.99))
fig.update_layout(template="plotly_white")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.