gpt4 book ai didi

Python:更改 Plotly 中的自定义控件值

转载 作者:行者123 更新时间:2023-12-01 06:51:53 25 4
gpt4 key购买 nike

我想将 slider 值从“步骤 0、...、步骤 3”更改为“日期”列中的日期值“19/11/2019,..., 21/11/2019” .

除此之外,在 slider 左侧稍上方有一个计数器,显示“频率:步骤 X”,而不是“步骤 X”,我希望将其更改为动态显示“日期 X”,其中X 显示“日期”列中的列值。

这是我的数据集

+------------+---+---+---+---+
| Date | A | B | C | D |
+------------+---+---+---+---+
| 19/11/2012 | 1 | 3 | 4 | 5 |
| 20/11/2012 | 3 | 2 | 3 | 2 |
| 21/11/2012 | 1 | 2 | 2 | 2 |
+------------+---+---+---+---+

这是我的代码。

# imports
import plotly.graph_objects as go
import pandas as pd

# sample data
dat=pd.DataFrame(dict(Date=['19/11/2012', '20/11/2012', '21/11/2012'],
A=[1,3,1],
B=[3,2,2],
C=[4,3,2],
D=[5,2,2],))

# Create figure
fig = go.Figure()

# Add traces, one for each slider step
for step in np.arange(len(dat['Date'])):
fig.add_trace(
go.Scatter(
visible=False,
line=dict(color="#00CED1", width=6),
name="𝜈 = " + str(step),
x=['A','B','C','D'],
y=dat.iloc[step,1::]))

# Make 10th trace visible


# Create and add slider
steps = []
for i in range(len(fig.data)):
step = dict(
method="restyle",
args=["visible", [False] * len(fig.data)],
)
step["args"][1][i] = True # Toggle i'th trace to "visible"
steps.append(step)

sliders = [dict(
active=1,
currentvalue={"prefix": "Frequency: "},
pad={"t": 50},
steps=steps
)]

fig.update_layout(
sliders=sliders
)

fig.show()

这是我的代码的输出: This is the output of my code

如您所见,圆圈部分是“Frequency: Step X”计数器,尖头部分是 slider 。

最佳答案

这应该可以做到:

# Edit slider labels
fig['layout']['sliders'][0]['currentvalue']['prefix']='Date: '
for i, date in enumerate(dat['Date'], start = 0):
fig['layout']['sliders'][0]['steps'][i]['label']=date

plotly :

enter image description here

代码:

dat=pd.DataFrame(dict(Date=['19/11/2012', '20/11/2012', '21/11/2012'],
A=[1,3,1],
B=[3,2,2],
C=[4,3,2],
D=[5,2,2],))

# Create figure
fig = go.Figure()

# Add traces, one for each slider step
for step in np.arange(len(dat['Date'])):
fig.add_trace(
go.Scatter(
visible=False,
line=dict(color="#00CED1", width=6),
name="𝜈 = " + str(step),
x=['A','B','C','D'],
y=dat.iloc[step,1::]))

# Create and add slider
steps = []
for i in range(len(fig.data)):
step = dict(
method="restyle",
args=["visible", [False] * len(fig.data)],
)
step["args"][1][i] = True # Toggle i'th trace to "visible"
steps.append(step)

sliders = [dict(
active=1,
currentvalue={"prefix": "Frequency: "},
pad={"t": 50},
steps=steps
)]

fig.update_layout(
sliders=sliders
)

# Edit slider labels
fig['layout']['sliders'][0]['currentvalue']['prefix']='Date: '
for i, date in enumerate(dat['Date'], start = 0):
fig['layout']['sliders'][0]['steps'][i]['label']=date

fig.show()

关于Python:更改 Plotly 中的自定义控件值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58948973/

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