在Plotly Python中创建一个TextBox

时间:2017-11-08 11:20:25

标签: python plotly plotly-dash

我可以以某种方式在Plotly中生成左侧为文本的报告,在滑块选择中更改,右侧是我的图表吗?不使用dash和过多javascript的方式是首选。

它应该是这样的:

enter image description here

1 个答案:

答案 0 :(得分:0)

针对仍在寻找答案的人:

一些绘图。graph_objects基础首先:

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

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

通用代码流:

我们假设:

  1. 数据框df具有4列X,Y1,Y2,日期
  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)

希望逻辑流程清楚!