Python散景附加Y轴问题
Python Bokeh additional Y axis problems
我编写了下面的代码,以根据来自 Pandas 数据帧的数据生成折线图。数据帧的索引是一个时间序列。
下面的代码运行良好,但我决定再添加一个数据系列(来自数据框的第三列,在代码中称为 'Col3')。我希望这第三个系列位于单独的 Y 轴上。然而,当我添加代码来实现这一点时,如下图所示,用 *** 突出显示,Bokeh 似乎无法生成图。有谁知道我做错了什么吗?
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 Spectral11
store = pd.HDFStore(<dataframe location>)
df = pd.DataFrame(store['d1'])
df = df.rename_axis('Time')
df.fillna(0)
numlines = len(df.columns)
#import colour pallete
mypalette = Spectral11[0:numlines]
# remove unwanted columns
col_list = ['Col1', 'Col2', 'Col3']
df = df[col_list]
# make a list of our columns
col = []
[col.append(i) for i in df.columns]
# make the figure,
p = figure(x_axis_type="datetime", title="<Plot Title>", width = 800, height = 450)
p.xaxis.axis_label = 'Date'
p.yaxis.axis_label = '<Name of Primary Y Axis>'
*** # add extra y axis
p.extra_y_ranges = {'Col3': Range1d(start=0, end=0.25)}
p.add_layout(LinearAxis(y_range_name='Performance ratio %'), 'right') ***
# loop through our columns and colors
for (columnnames, colore) in zip(col, mypalette):
p.line(df.index, df[columnnames], legend = columnnames, color = colore )
# creates an output file
output_file('<outputlocation>')
#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.
几个问题:
当你这样做时:
p.extra_y_ranges = {'Col3': Range1d(start=0, end=0.25)}
p.add_layout(LinearAxis(y_range_name='Performance ratio %'), 'right')
您正在第一行创建另一个名为 Col3
的 y_range
。您需要在第二行使用此名称,而不是 Performance ratio %
。
(或者更好的是,在第一行也将其命名为 Performance ratio %
)。
此外,在您的循环中,您将所有 3 个系列放在同一个 y 轴上,您需要为第三个指定 y_range_name='Col3'
。
以下作品:
numlines = len(df.columns)
#import colour pallete
mypalette = Spectral11[0:numlines]
# remove unwanted columns
col_list = ['Col1', 'Col2', 'Col3']
df = df[col_list]
# make the figure,
p = figure(x_axis_type="datetime", title="<Plot Title>",
width = 800, height = 450, y_range=(0,100))
p.xaxis.axis_label = 'Date'
p.yaxis.axis_label = '<Name of Primary Y Axis>'
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 = {'Performance ratio %': Range1d(start=0, end=50)}
p.line(df.index, df['Col3'], legend = 'Col3', color = mypalette[2],
y_range_name='Performance ratio %' )
p.add_layout(LinearAxis(y_range_name='Performance ratio %'), 'right')
# creates an output file
output_file('bokeh.html')
#save the plot
show(p)
我编写了下面的代码,以根据来自 Pandas 数据帧的数据生成折线图。数据帧的索引是一个时间序列。
下面的代码运行良好,但我决定再添加一个数据系列(来自数据框的第三列,在代码中称为 'Col3')。我希望这第三个系列位于单独的 Y 轴上。然而,当我添加代码来实现这一点时,如下图所示,用 *** 突出显示,Bokeh 似乎无法生成图。有谁知道我做错了什么吗?
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 Spectral11
store = pd.HDFStore(<dataframe location>)
df = pd.DataFrame(store['d1'])
df = df.rename_axis('Time')
df.fillna(0)
numlines = len(df.columns)
#import colour pallete
mypalette = Spectral11[0:numlines]
# remove unwanted columns
col_list = ['Col1', 'Col2', 'Col3']
df = df[col_list]
# make a list of our columns
col = []
[col.append(i) for i in df.columns]
# make the figure,
p = figure(x_axis_type="datetime", title="<Plot Title>", width = 800, height = 450)
p.xaxis.axis_label = 'Date'
p.yaxis.axis_label = '<Name of Primary Y Axis>'
*** # add extra y axis
p.extra_y_ranges = {'Col3': Range1d(start=0, end=0.25)}
p.add_layout(LinearAxis(y_range_name='Performance ratio %'), 'right') ***
# loop through our columns and colors
for (columnnames, colore) in zip(col, mypalette):
p.line(df.index, df[columnnames], legend = columnnames, color = colore )
# creates an output file
output_file('<outputlocation>')
#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.
几个问题:
当你这样做时:
p.extra_y_ranges = {'Col3': Range1d(start=0, end=0.25)}
p.add_layout(LinearAxis(y_range_name='Performance ratio %'), 'right')
您正在第一行创建另一个名为 Col3
的 y_range
。您需要在第二行使用此名称,而不是 Performance ratio %
。
(或者更好的是,在第一行也将其命名为 Performance ratio %
)。
此外,在您的循环中,您将所有 3 个系列放在同一个 y 轴上,您需要为第三个指定 y_range_name='Col3'
。
以下作品:
numlines = len(df.columns)
#import colour pallete
mypalette = Spectral11[0:numlines]
# remove unwanted columns
col_list = ['Col1', 'Col2', 'Col3']
df = df[col_list]
# make the figure,
p = figure(x_axis_type="datetime", title="<Plot Title>",
width = 800, height = 450, y_range=(0,100))
p.xaxis.axis_label = 'Date'
p.yaxis.axis_label = '<Name of Primary Y Axis>'
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 = {'Performance ratio %': Range1d(start=0, end=50)}
p.line(df.index, df['Col3'], legend = 'Col3', color = mypalette[2],
y_range_name='Performance ratio %' )
p.add_layout(LinearAxis(y_range_name='Performance ratio %'), 'right')
# creates an output file
output_file('bokeh.html')
#save the plot
show(p)