1

I am trying to dynamically collect data from a csv and plot it in a graph. Here is a portion of the csv data file:

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

It works with this code:

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

In the case I add more channels later, I am trying to find out how many channels there are and pull in the channel name and set up the plot. Below is my attempt at the code:

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)

I receive an 'IndexError: List assignment index out of range for the first trace[i] line. Hopefully this isn't too long winded. Thanks for your time.

2
  • change trace[i]... to trace.append(go.Scatter... Commented Sep 15, 2017 at 21:35
  • That did the trick, thank you! Commented Sep 18, 2017 at 14:47

1 Answer 1

1

Per Maximillian Peters comment above here is the code:

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))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.