3

我可以以某种方式在Plotly左侧是文本的地方进行报告,该文本在滑块选择上发生变化,而右侧是我的图表?不使用dash和过多的方式javascript是首选。

它应该如下所示:

在此处输入图像描述

4

1 回答 1

0

对于仍在寻找答案的人:

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

Plotly 图形由 3 部分组成:数据(要绘制的轨迹)、布局(要在其上绘制数据)、框架(用于循环的数据 + 布局)

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

通用代码流程:

让我们假设:

  1. 数据框 df 有 4 列 X,Y1,Y2,Dates
  2. 您的滑块代表不同的日期
#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)

希望逻辑流程清晰!

于 2020-07-08T18:53:05.047 回答