gpt4 book ai didi

python - 在 Plotly Python 中创建一个文本框

转载 作者:行者123 更新时间:2023-12-05 07:39:14 24 4
gpt4 key购买 nike

我能否以某种方式在 Plotly 中制作一份报告,其中左侧是文本, slider 选择时会发生变化,右侧是我的图表?首选不使用dash 和过多javascript 的方式。

它应该是这样的:

enter image description here

最佳答案

对于仍在寻找答案的人:

首先介绍一些 plotly.graph_objects 基础知识:

Plotly figure 由三部分组成:Data(要绘制的轨迹)、Layout(要在其上绘制数据)、Frame(Data + Layout 用于循环)

要回答您的问题以在左侧包含文本,您必须将文本写为注释(布局)。要在正确使用子图(数据)上包含两个图表。要使用 slider pick 更新它们,您必须使用 Frame 更新它们。每个框架都有对应于该 slider 位置的 Data+Layout。

通用代码流:

假设:

  1. Dataframe df 有 4 列 X,Y1,Y2,Dates
  2. 您的 slider 代表不同的日期
#Import plotly graph object
import plotly.graph_objects as go

#Getting different dates
dates = df['Dates'].unique().tolist()

#Plotting first instance
data=[]
#Fig 1
data_dict1 = {{
"x": list(df["X"]),
"y": list(df["Y1"]),
"type":"scatter"},
"ncol":2,"nrow":1 #Subplot location
}
#Fig 2
data_dict2 = {
"x": list(df["X"]),
"y": list(df["Y2"]),
"type":"scatter",
"ncol":2,"nrow":2 #Subplot location
}

data = [data_dict1,data_dict2] #combining both plots

#Layout and slider
layout = {
"sliders" : [{
#Enter required slider layout
,"steps": [] #should include this line in slider
}],
"annotations":[ {
#Input required annotations
}]
} # layout ends

#making Frames
frames =[]
for date in dates:
frame = {"data": [], "name": str(date),"layout":{"annotations":[]}} # Saving each data frame
dataset = df[df[Dates]==date] #subsetting data for a date
# Create charts for this subset data
#Fig 1
data_dict1 = {{
"x": list(dataset["X"]),
"y": list(dataset["Y1"]),
"type":"scatter"},
"ncol":2,"nrow":1 #Subplot location
}
#Fig 2
data_dict2 = {
"x": list(dataset["X"]),
"y": list(dataset["Y2"]),
"type":"scatter",
"ncol":2,"nrow":2 #Subplot location
}

#Adding this data to frame
frame["data"] = [data_dict1,data_dict2]

# Creating lines to be displayed using annotations
annote1 = {
#Add data for line 1
}
annote2 = {
#Add data for line 2
}
annote3 = {
#Add data for line 3
}
annote4 = {
#Add data for line 4
}

#Adding all annotations lines to frame
frame["layout"]["annotations"] = [annote1,annote2,annote3,annote4]

#Combinig all frames
frames.append(frame)

#Updating slider step
slider_step = {
"label": date,
"method": "update"}
#Adding step to layout slider
layout["sliders"][0]["steps"].append(slider_step)

#Combine everything
fig = go.Figure(data =data,layout = layout, frames =frames)
#View in browser
plot(fig, auto_open=True)

希望逻辑流程清晰!

关于python - 在 Plotly Python 中创建一个文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47178402/

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