繁体   English   中英

Plotly 具有多条轨迹的子图

[英]Plotly Subplots with Multiple Traces

在同一个 plot 上制作一个带有多条迹线的 plotly 子图。

所以我正在制作我计划放在 HTML 文件中以供 web 查看的子图。 但是,问题是我的每个 plot 都有多个痕迹,并且是在 px.line 中制作的。 好吧,所以我尝试使用 go.Scatter 制作一个带有多个轨迹的 plot,但这也不起作用。

这是当前代码:

import pandas as pd 
import plotly.express as px 
import plotly.graph_objects as go
import plotly
from itertools import cycle
from plotly.subplots import make_subplots 


#below code imports file needed at the moment
F62Ext1 = pd.read_csv("F6-2_Ext1.csv")
F62Ext2 = pd.read_csv("F6-2_Ext2.csv")


#using a savgol filter to smooth out data in python
from scipy.signal import savgol_filter
#definition of savgol filter for dataframe written below
def savgol(x):
    return savgol_filter(x,101,6)
F62Ext1_smoothed = F62Ext1.apply(savgol)
F62Ext2_smoothed = F62Ext2.apply(savgol)

F62E1_CH1 = go.Scatter(x = F62Ext1_smoothed['Time'], y = [F62Ext1_smoothed['CH1'],F62Ext1['ActivePoke'], F62Ext1['CueLight']], name = "F62Ext1, Channel 1")
F62E2_CH1 = go.Scatter(x = F62Ext2_smoothed['Time'], y = [F62Ext2_smoothed['CH1'],F62Ext2['ActivePoke'], F62Ext2['CueLight']], name = "F62Ext2, Channel 1")

fig = make_subplots(rows = 1, cols = 2, shared_yaxes = True)
fig.add_trace(F62E1_CH1, row = 1, col = 1)
fig.add_trace(F62E2_CH1, row = 1, col = 2)

产生以下内容: go.Scatter plot

然而,我想要获得的是: px.line plot with multiple traces

我该如何纠正这个错误?

编辑:F62E1 的字典如下:

{'Time': {0: 0.0, 1: 0.1, 2: 0.2, 3: 0.3, 4: 0.4},
 'CH1': {0: -0.169504148,
  1: -0.372732867,
  2: -0.323666804,
  3: -0.352484157,
  4: -0.327975913},
 'CH2': {0: -0.188884746,
  1: -0.301823346,
  2: -0.13172756,
  3: -0.456077945,
  4: -0.543653381},
 'CH3': {0: 0.03599356,
  1: -0.029965731,
  2: 0.014807156,
  3: -0.06392204,
  4: -0.041387216},
 'CH4': {0: -0.082626583,
  1: -0.071240774,
  2: -0.055854863,
  3: -0.137854075,
  4: -0.134953954},
 'CH5': {0: -0.104248439,
  1: -0.241010992,
  2: -0.249843885,
  3: -0.306350559,
  4: -0.223923059},
 'CH6': {0: -0.020229121,
  1: -0.359940514,
  2: -0.229173544,
  3: -0.266795437,
  4: -0.278069671},
 'CH7': {0: -0.209219168,
  1: -0.255944829,
  2: -0.215244963,
  3: -0.290505347,
  4: -0.28456927},
 'CH8': {0: 0.093962928,
  1: 1.2574526,
  2: 1.75068895,
  3: 1.356159013,
  4: 0.676462295},
 'CH9': {0: -0.150238434,
  1: -0.167165814,
  2: -0.057212299,
  3: -0.20998062,
  4: -0.351662263},
 'CH10': {0: -0.306078824,
  1: -0.399363635,
  2: -0.281349569,
  3: -0.53463429,
  4: -0.383537615},
 'CH11': {0: -0.189443456,
  1: -0.161361134,
  2: -0.223323043,
  3: -0.338406151,
  4: -0.339504188},
 'CH12': {0: -0.174074045,
  1: -0.230008948,
  2: -0.255067313,
  3: -0.380150306,
  4: -0.393756259},
 'CH13': {0: -0.123562058,
  1: -0.271150428,
  2: -0.251144972,
  3: -0.379664224,
  4: -0.393714865},
 'CH14': {0: -0.233391565,
  1: -0.276967731,
  2: -0.236738984,
  3: -0.44725073,
  4: -0.455074619},
 'CH15': {0: -0.234460706,
  1: -0.368129274,
  2: -0.264646375,
  3: -0.438000119,
  4: -0.40056134},
 'CH16': {0: -0.267852578,
  1: -0.224666613,
  2: -0.189448701,
  3: -0.291014541,
  4: -0.387302535},
 'ActivePoke': {0: 0, 1: 0, 2: 0, 3: 0, 4: 0},
 'CueLight': {0: 0, 1: 0, 2: 0, 3: 0, 4: 0},
 'Pump': {0: 0, 1: 0, 2: 0, 3: 0, 4: 0},
 'InactivePoke': {0: 0, 1: 0, 2: 0, 3: 0, 4: 0}}

我认为问题在于 plotly 无法理解此跟踪:

F62E1_CH1 = go.Scatter(x = F62Ext1_smoothed['Time'], y = [F62Ext1_smoothed['CH1'],F62Ext1['ActivePoke'], F62Ext1['CueLight']], name = "F62Ext1, Channel 1")

您可以改为将go.Scatter对象存储在列表中,然后循环遍历它们以单独添加轨迹。 我还使用了参数legendgroupshowlegend ,以便可以一起选择或取消选择子图的所有轨迹——如果不需要,可以将其删除。

例如:

F62E1_CH1_traces = [
    go.Scatter(
        x = F62Ext1_smoothed['Time'], 
        y = y, 
        name = "F62Ext1, Channel 1",
        legendgroup = "F62Ext1, Channel 1",
        showlegend = showlegend
    ) for y, showlegend in zip(
        [F62Ext1_smoothed['CH1'],F62Ext1['ActivePoke'], F62Ext1['CueLight']],
        [True, False, False]
    ) 
]
F62E1_CH2_traces = [
    go.Scatter(
        x = F62Ext2_smoothed['Time'], 
        y = y, 
        name = "F62Ext2, Channel 2",
        legendgroup = "F62Ext2, Channel 2",
        showlegend = showlegend
    ) for y, showlegend in zip(
        [F62Ext2_smoothed['CH1'],F62Ext2['ActivePoke'], F62Ext2['CueLight']],
        [True, False, False]
    )
]

fig = make_subplots(rows = 1, cols = 2, shared_yaxes = True)
for F62E1_CH1_trace in F62E1_CH1_traces:
    fig.add_trace(F62E1_CH1_trace, row = 1, col = 1)
for F62E1_CH2_trace in F62E1_CH2_traces:
    fig.add_trace(F62E1_CH2_trace, row = 1, col = 2)
fig.show()

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM