我在Pandas dataframe
中有数据,我试图将其绘制到时间序列折线图中。
当绘制一行时,我已经能够使用p.line
函数非常成功地完成此操作,确保我创建x_axis_type 'datetime'
。
为了绘制多行,我尝试使用p.multi_line
,效果很好,但我也需要一个图例,根据这篇文章,不可能在多行中添加图例:Bokeh how to add legend to figure created by multi_line method?
Leo对上述链接中的问题的回答看起来很有希望,但是当数据来自数据框时,我似乎无法解决如何应用此问题。
有没有人有任何提示?
答案 0 :(得分:3)
好的,这似乎有效:
from bokeh.plotting import figure, output_file, save
from bokeh.models import ColumnDataSource
import pandas as pd
from pandas import HDFStore
from bokeh.palettes import Spectral11
# imports data to dataframe from our storage hdf5 file
# our index column has no name, so this is assigned a name so it can be
# referenced to for plotting
store = pd.HDFStore('<file location>')
df = pd.DataFrame(store['d1'])
df = df.rename_axis('Time')
#the number of columns is the number of lines that we will make
numlines = len(df.columns)
#import color pallet
mypalette = Spectral11[0:numlines]
# remove unwanted columns
col_list = ['Column A', 'Column B']
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="<title>", width = 800, height = 450)
p.xaxis.axis_label = 'Date'
p.yaxis.axis_label = '<units>'
# loop through our columns and colours
for (columnnames, colore) in zip(col, mypalette):
p.line(df.index, df[columnnames], legend = columnnames, color = colore )
# creates an output file
output_file('<output location>')
#save the plot
save(p)