gpt4 book ai didi

python - 在不使用 Plotly Express 的情况下向 Plotly 子图添加垂直矩形

转载 作者:行者123 更新时间:2023-12-03 23:42:27 32 4
gpt4 key购买 nike

我正在尝试在具有子图的绘图图表中创建对应于不同日期范围的阴影区域。
理想情况下,我希望每个阴影矩形都适合每个子图,但我发现这很难。这是一些示例代码:

import pandas as pd
import numpy as np
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots

df = pd.DataFrame({'A': list(range(25)),
'B': list(range(25, 50)),
'C': list(range(50, 75))}, index=pd.date_range('20200101', periods=25))

fig = make_subplots(rows=3, cols=1)

for idx, col in enumerate(df.columns):
fig.add_trace(go.Scatter(x=df.index, y=df[col]), row=idx + 1, col=1)

shape_dict = {'type':'rect', 'xref':'x', 'yref':'paper', 'x0':'2020-01-03', 'x1':'2020-01-12', 'y0':0, 'y1':1, 'fillcolor': 'LightSalmon', 'layer': 'below', 'opacity': 0.25, 'line_width': 0}
如果我这样做 fig.update_layout(shapes=[shape_dict]) ,然后我得到这个:
enter image description here
还不错,但我更喜欢将这些形状中的每一个都分别安装到它们自己的子图中。
当我尝试使用 add_shape 执行此操作时,阴影区域失去其缩放比例:
for idx, col in enumerate(df.columns):
fig.add_trace(go.Scatter(x=df.index, y=df[col]), row=idx + 1, col=1)
fig.add_shape(shape_dict, row=idx + 1, col=1)
这给了我这个:
enter image description here
我更喜欢 不是 必须单独重新计算轴。
我也无法访问 add_vrect -- 我不知道为什么,但它不能作为一种方法使用,而且我也不能使用 plotly.express ,大部分 plot.ly 的文档使用 px图表和他们做我所描述的事情的方法。
编辑
要回复下面的答案, add_vrect不适用于我的 plotly 版本,即 4.12.0。
例如 r-beginners中的示例代码给我这个:
df = pd.DataFrame({'A': list(range(25)),
'B': list(range(25, 50)),
'C': list(range(50, 75))}, index=pd.date_range('20200101', periods=25))

fig = make_subplots(rows=3, cols=1)

for idx, col in enumerate(df.columns):
fig.add_trace(go.Scatter(x=df.index, y=df[col]), row=idx + 1, col=1)

# shape_dict = {'type':'rect', 'xref':'x', 'yref':'paper', 'x0':'2020-01-03', 'x1':'2020-01-12', 'y0':0, 'y1':1, 'fillcolor': 'LightSalmon', 'layer': 'below', 'opacity': 0.25, 'line_width': 0}
# fig.update_layout(shapes=[shape_dict])

fig.add_vrect(
x0="2020-01-03", x1="2020-01-12",
y0=0, y1=1,
fillcolor="LightSalmon", opacity=0.25,
layer="below", line_width=0)

fig.show()
返回错误信息: AttributeError: 'Figure' object has no attribute 'add_vrect'

最佳答案

阴影输出到每个子图 add_vrect() .规模也不受影响。这个答案是否符合你的问题的意图?

import pandas as pd
import numpy as np
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots

df = pd.DataFrame({'A': list(range(25)),
'B': list(range(25, 50)),
'C': list(range(50, 75))}, index=pd.date_range('20200101', periods=25))

fig = make_subplots(rows=3, cols=1)

for idx, col in enumerate(df.columns):
fig.add_trace(go.Scatter(x=df.index, y=df[col]), row=idx + 1, col=1)

# shape_dict = {'type':'rect', 'xref':'x', 'yref':'paper', 'x0':'2020-01-03', 'x1':'2020-01-12', 'y0':0, 'y1':1, 'fillcolor': 'LightSalmon', 'layer': 'below', 'opacity': 0.25, 'line_width': 0}
# fig.update_layout(shapes=[shape_dict])

fig.add_vrect(
x0="2020-01-03", x1="2020-01-12",
y0=0, y1=1,
fillcolor="LightSalmon", opacity=0.25,
layer="below", line_width=0,
)

fig.show()
enter image description here

关于python - 在不使用 Plotly Express 的情况下向 Plotly 子图添加垂直矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64939883/

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