MGS
MGS

Reputation: 43

Python and Plotly offline: create (empty) graph and add traces with for-loop

I am new to programming and I have spent a lot of time looking for a solution, but I can't seem to get it right. I have some measurement data sets stored in a dictionary. I want to create a graph with plotly where all curves from measured data are shown. I am trying to create an empty graph and then iterating through the dictionary and add two columns of the two stored dataFrames as traces to the graph for each key.

What am I doing wrong or how can I do it in another way?

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 I have so far, I get 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

Upvotes: 4

Views: 11045

Answers (1)

Maximilian Peters
Maximilian Peters

Reputation: 31659

  • append_trace in Plotly works for subplots but not for regular Figures.
  • You are iterating over your dictionary keys, but you never use one of the keys, you are plotting the same values for each key. In the example below key is used to name the traces.

enter image description here

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)

Upvotes: 2

Related Questions