繁体   English   中英

遍历熊猫数据框的各列以进行绘图

[英]Loop through columns of pandas dataframe for plotly

我正在尝试从csv动态收集数据并将其绘制在图形中。 这是csv数据文件的一部分:

Time,Digital Plots - 11,Digital Plots - 10,Digital Plots - 9,Digital Plots - 8,Digital Plots - 7,Digital Plots - 6,Digital Plots - 5,Digital Plots - 4,Digital Plots - 3,Digital Plots - 2,Digital Plots - 1,Digital Plots - 0        
0,1,1,0,0,0,0,0,0,1,1,1,1
5,1,1,0,0,0,0,0,0,1,1,1,1
10,1,1,0,0,0,0,0,0,1,1,1,1
15,1,1,0,0,0,0,0,0,1,1,1,1
20,1,1,0,0,1,0,0,0,1,1,1,1
25,1,1,0,0,1,0,0,0,0,1,1,1
30,1,1,0,0,1,0,0,0,0,1,1,1
35,1,1,0,0,0,1,0,0,0,1,1,1
40,1,1,0,0,0,1,0,0,0,1,1,1
45,1,1,0,0,0,0,0,0,1,1,1,1
50,1,0,0,0,0,0,0,0,1,1,1,1
55,1,0,0,0,0,0,0,0,1,1,1,1
60,1,0,0,0,0,0,0,0,1,1,1,1
65,1,1,0,1,0,0,0,0,1,1,1,1
70,1,1,0,1,0,0,0,0,1,1,1,1
75,1,1,0,0,0,0,0,0,1,1,1,1
80,1,1,0,0,0,0,0,0,1,1,1,1

可以使用以下代码:

########     import functions    ########
import plotly
from plotly import tools
import plotly.offline as py
import plotly.graph_objs as go
import plotly.figure_factory as FF
py.init_notebook_mode()
import numpy as np
import pandas as pd

####    Import Data File    ####
file_in_csv = "C:\\Users\\All Pass data CRC.csv"
df = pd.read_csv(file_in_csv)
sample_data_table = FF.create_table(df.head())
py.iplot(sample_data_table)

####    Set each channel Trace Properties    ####
trace0 = go.Scatter( x = df['Time'], y=df['Digital Plots - 0'], mode = 'lines', name = 'Ch 0')
trace1 = go.Scatter( x = df['Time'], y=df['Digital Plots - 1'], mode = 'lines', name = 'Ch 1')
trace2 = go.Scatter( x = df['Time'], y=df['Digital Plots - 2'], mode = 'lines', name = 'Ch 2')
trace3 = go.Scatter( x = df['Time'], y=df['Digital Plots - 3'], mode = 'lines', name = 'Ch 3')
trace4 = go.Scatter( x = df['Time'], y=df['Digital Plots - 4'], mode = 'lines', name = 'Ch 4')
trace5 = go.Scatter( x = df['Time'], y=df['Digital Plots - 5'], mode = 'lines', name = 'Ch 5')
trace6 = go.Scatter( x = df['Time'], y=df['Digital Plots - 6'], mode = 'lines', name = 'Ch 6')
trace7 = go.Scatter( x = df['Time'], y=df['Digital Plots - 7'], mode = 'lines', name = 'Ch 7')
trace8 = go.Scatter( x = df['Time'], y=df['Digital Plots - 8'], mode = 'lines', name = 'Ch 8')
trace9 = go.Scatter( x = df['Time'], y=df['Digital Plots - 9'], mode = 'lines', name = 'Ch 9')
trace10 = go.Scatter( x = df['Time'], y=df['Digital Plots - 10'], mode = 'lines', name = 'Ch 10')
trace11 = go.Scatter( x = df['Time'], y=df['Digital Plots - 11'], mode = 'lines', name = 'Ch 11')

####    Set up Digital Timing Chart    ####
fig = tools.make_subplots(rows = 12, cols = 1,shared_xaxes = True)
fig.append_trace(trace0, 1, 1)
fig.append_trace(trace1, 2, 1)
fig.append_trace(trace2, 3, 1)
fig.append_trace(trace3, 4, 1)
fig.append_trace(trace4, 5, 1)
fig.append_trace(trace5, 6, 1)
fig.append_trace(trace6, 7, 1)
fig.append_trace(trace7, 8, 1)
fig.append_trace(trace8, 9, 1)
fig.append_trace(trace9, 10, 1)
fig.append_trace(trace10, 11, 1)
fig.append_trace(trace11, 12, 1)
fig['layout'].update(height = 750, width = 950, title = 'Bit Timing!')
py.iplot(fig)

如果以后添加更多通道,我将尝试找出有多少通道,并输入通道名称并设置图。 下面是我对代码的尝试:

import pandas as pd
import plotly
import plotly.offline as py
import plotly.graph_objs as go
from plotly import tools
#import plotly.figure_factory as FF

py.init_notebook_mode()

#import numpy as np
trace = []
file_in_csv = "C:\\All Pass data CRC.csv"
df = pd.read_csv(file_in_csv)

Headers = df.columns.values.tolist()
print (Headers)

for i in range(12):
    trace[i]=go.Scatter(x=df[Headers[0]], y = df[Headers[i+1]], mode = 'lines', name = Headers[i+1])

fig = tools.make_subplots(rows = 12, cols = 1, shared_xaxes = True)

for i in range(12):
     fig.append_trace(trace[i],i+1,1)

我收到一个'IndexError:列表分配索引超出第一条trace [i]行的范围。 希望这不会太久。 谢谢你的时间。

每个马克西米利安·彼得斯上面的评论是这里的代码:

for i in range(Num_Channels):
    trace0.append(go.Scatter(x=df_lab["Time"], y = df_lab[Headers[i+1]], mode = 'lines', name = Headers[i+1]))
    trace1.append(go.Scatter(x=X_SoC[i], y = Y_SoC[i], mode = 'markers', showlegend = False))

暂无
暂无

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

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