2016-12-01 18 views
1

私は以下のコードに問題抱えている:Python Pandas Bokeh Indexerror:リストインデックスが範囲外です - なぜですか?

:私は上記のコードを実行しようとすると、私は次のエラーを取得する

Time    Col1  Col2  Col3  Col4 
29/11/2016 00:00 4  41  41  55 
29/11/2016 01:00 55  15  61  81 
29/11/2016 02:00 51  75  2   4 
29/11/2016 03:00 21  21  51  9 
etc. 

:これは私のデータフレームのように見えるものである

from bokeh.plotting import figure, output_file, show, save 
from bokeh.models import ColumnDataSource 
from bokeh.models import Range1d, LinearAxis 
import pandas as pd 
from pandas import HDFStore 
from bokeh.palettes import Spectral9 

store = pd.HDFStore('<hdf store location>') 
df = pd.DataFrame(store['d1']) 
df = df.rename_axis('Time') 
df.fillna(0) 

#the number of colums is the number of lines that we will make 
numlines = len(df.columns) 

#import colour pallet 
mypalette = Spectral9[0:numlines] 

# remove unwanted columns 
col_list = ['Col1', 'Col2', 'Col3'] 
df = df[col_list] 

# make the figure, 
p = figure(x_axis_type="datetime", title="<title>", width = 800, height = 450) 
p.xaxis.axis_label = 'Date' 
p.yaxis.axis_label = '<y axis label>' 

p.line(df.index, df['Col1'], legend = 'Col1', color = mypalette[0]) 
p.line(df.index, df['Col2'], legend = 'Col2', color = mypalette[1]) 

# add extra y axis 
p.extra_y_ranges = {'Col3': Range1d(start=0, end=1)} 
p.circle(df.index, df['Col3'], legend = 'Col3', color = mypalette[8], 
    y_range_name='Col3') 
p.add_layout(LinearAxis(y_range_name='Col3'), 'right') 


# creates an output file 
output_file('<output file location>') 

#save the plot 
save(p) 

IndexError        Traceback (most recent call last) 
<ipython-input-20-9d2c8911130d> in <module>() 
38 
39 # add extra y axis 
---> 40 p.circle(df.index, df['Col3'], legend = 'Col3', color = mypalette[8], y_range_name='Col3') 
41 p.add_layout(LinearAxis(y_range_name='Col3'), 'right') 
42 

IndexError: list index out of range 

私が間違っていることを解決できないようです。誰も助けることができますか?

答えて

2

コードの先頭に次の行が表示されます。

#the number of colums is the number of lines that we will make 
numlines = len(df.columns) 

#import colour pallet 
mypalette = Spectral9[0:numlines] 

最初の行では、numlinesを持つ列の数と同じに設定します。データフレームには4つの列しかありません。 2行目で、mypaletteをSpectral9の最初のN個の要素に等しく設定します。ここでnは、あなたが持つ線の数です。そのため、パレットはSpectral9の最初の4つの要素に限定されています。

後であなたのコードでは、mypaletteの9番目の要素を取得しようとします(これは、Pythonでインデックスがゼロの[8]になります)。

p.circle(df.index, df['Col3'], legend = 'Col3', color = mypalette[8], 
    y_range_name='Col3') 

mypaletteには4つの要素しかないので、mypalette [8]は範囲外です。特定の色を使用する場合は、color = mypalette[8]の代わりにcolor = Spectral9[8]を使用することを検討してください。

+0

パーフェクト。ありがとうございました。 – pottolom

関連する問題