Python and Plotly offline: create a (empty) plot and add traces with a for-loop
I am new to programming and I spent a lot of time looking for a solution, but I don't seem to get it. I have several sets of measurement data stored in a dictionary. I want to create a graph with graphically showing all the curves from the measured data. I am trying to create an empty graph and then iterate through a dictionary and add two columns from two stored dataFrames as traces to the graph for each key.
What am I doing wrong or how can I do it differently?
import plotly as py
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import plotly.graph_objs as go
figure1 = go.Figure()
for key in dictofdf:
trace1 = go.Scatter(
y=df_1['force'],
x=df_1['displacement'],
mode='line',
marker=go.Marker(color='rgb(255, 127, 14)'),
name='load'
)
trace2 = go.Scatter(
y=df_2['force'],
x=df_2['displacement'],
mode='line',
marker=go.Marker(color='rgb(55, 137, 3)'),
name='unload'
)
figure1.append_trace(trace1,1,1)
figure1.append_trace(trace2,1,1)
py.offline.iplot(figure1, filename='force-displacement-data', image='jpeg')
With the code that I have so far, I am getting the error
--------------------------------------------------------------------------- TypeError Traceback (most recent call
last) <ipython-input-42-4ea5d9b8a638> in <module>()
50 )
51
---> 52 figure1.append_trace(trace1,1,1)
53 figure1.append_trace(trace2,1,1)
54
C:\Users\xxxxx\Anaconda3\lib\site-packages\plotly\graph_objs\graph_objs.py
in append_trace(self, trace, row, col)
914 "Note: the starting cell is (1, 1)")
915 try:
--> 916 ref = grid_ref[row-1][col-1]
917 except IndexError:
918 raise Exception("The (row, col) pair sent is out of range. "
TypeError: 'NoneType' object is not subscriptable
+3
source to share
1 answer
-
append_trace
in Plotly works for subplots , but not for regularFigure
s. - You iterate over the keys of the dictionary, but you never use one of the keys, you draw the same values for each key. The example below is
key
used forname
footprints.
import plotly
plotly.offline.init_notebook_mode()
figure1 = plotly.tools.make_subplots(rows=1, cols=2)
dictofdf = {'measurement1': {'df_1': {'displacement': [1, 2, 3],
'force': [1, 3, 6]},
'df_2': {'displacement': [1, 3, 6],
'force': [5, 7, 9]}
},
'measurement2': {'df_1': {'displacement': [1, 4, 5],
'force': [8, 10 , 12]},
'df_2': {'displacement': [1, 4, 6],
'force': [7, 8, 9]}
}
}
for key in dictofdf:
trace1 = plotly.graph_objs.Scatter(
y=dictofdf[key]['df_1']['force'],
x=dictofdf[key]['df_1']['displacement'],
mode='line',
marker=plotly.graph_objs.Marker(color='rgb(255, 127, 14)'),
name='{}: load'.format(key)
)
trace2 = plotly.graph_objs.Scatter(
y=dictofdf[key]['df_2']['force'],
x=dictofdf[key]['df_2']['displacement'],
mode='line',
marker=plotly.graph_objs.Marker(color='rgb(55, 137, 3)'),
name='{}: unload'.format(key)
)
figure1.append_trace(trace1, 1, 1)
figure1.append_trace(trace2, 1, 2)
plotly.offline.iplot(figure1)
+1
source to share