2017-09-15 89 views
0

我試圖動態地從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:列表分配索引超出第一個跟蹤[i]行的範圍。希望這不會太長時間。謝謝你的時間。

上述這裏
+0

變化'跟蹤[1] ...''到trace.append(go.Scatter ...' –

+0

這奏效了,謝謝您! –

回答

0

每馬克西米利彼得斯評論是代碼:

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))