2017-05-03 113 views
3

我是編程新手,我花了很多時間尋找解決方案,但我似乎無法做到。 我有一些測量數據集存儲在字典中。我想創建一個曲線圖,顯示來自測量數據的所有曲線。我試圖創建一個空圖,然後迭代字典,並將兩個存儲的dataFrames的兩列作爲每個鍵的圖形追蹤。Python和Plotly離線:創建(空)圖形並添加for循環的痕跡

我在做什麼錯,或者我該如何做另一種方式?

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

隨着我的代碼,到目前爲止,我得到的錯誤

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

回答

1
  • 在Plotly append_trace工程subplots但不經常Figure秒。
  • 您正在遍歷字典鍵,但是您從不使用其中一個鍵,而是爲每個鍵繪製相同的值。在下面的例子中key用於name的痕跡。

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

謝謝您的回答!如果它不能在情節中工作,我會尋找其他的可能性,然後製作這個情節。當我嘗試你建議的代碼時,我得到以下錯誤:'... ---> 37 y = dictofdf [key] [['df_1'] ['force']], 38 x = dictofdf [ [''df_1'] ['displacement']], 39 mode ='line', TypeError:列表索引必須是整數或切片,而不是str' – MGS

+0

剛剛嘗試過發佈的代碼,它應該工作。在你的片段中有雙括號。確保您正確切分字典/列表。 –

+0

嗯,好的...我在收到錯誤信息後插入了雙括號,看看是否有幫助。在我發佈代碼之前,我忘了刪除它們。 – MGS