Python Pandas Bokeh Indexerror: list index out of range - why?
Python Pandas Bokeh Indexerror: list index out of range - why?
我在使用以下代码时遇到问题:
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)
这是我的数据框的样子:
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.
当我尝试 运行 上面的代码时,出现以下错误:
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
我似乎无法弄清楚我做错了什么。有人可以帮忙吗?
以下几行出现在代码的顶部。
#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 列。在第二行中,将 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 = Spectral9[8]
而不是 color = mypalette[8]
.
我在使用以下代码时遇到问题:
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)
这是我的数据框的样子:
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.
当我尝试 运行 上面的代码时,出现以下错误:
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
我似乎无法弄清楚我做错了什么。有人可以帮忙吗?
以下几行出现在代码的顶部。
#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 列。在第二行中,将 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 = Spectral9[8]
而不是 color = mypalette[8]
.