2016-11-25 71 views
0

我有一个Pandas dataframe我试图绘制到时间序列线图的数据。Python /熊猫/散景:绘制多条线与数据帧的传说

当绘制一条线时,我已经能够使用p.line函数成功完成此操作,从而确保我制作了x_axis_type 'datetime'

要绘制多行,我一直在使用p.multi_line,它运作良好尝试,但我还需要一个传说,并根据这个帖子,这是不可能的传说添加到多:Bokeh how to add legend to figure created by multi_line method?

狮子座的答案上面的链接中的问题看起来很有希望,但我似乎无法解决数据源自数据框时如何应用这一问题。

有没有人有任何提示?

+0

你可以给你正在试图做的正是一个多一点的信息?在我使用散景和熊猫的经验中,我只是用'''figure' figure()''创建一个图形,然后我循环访问所使用的任何数据,用'''fig.line x = x,y = y,legend =“此行的标签”)'''为我需要的每一行。然后我跳出循环并显示图形,然后显示一个具有多行的单个图形。 – ralston

+0

感谢您的回答。我在其他地方发现了一些代码,它们的作用非常相似请在下面看到我自己的答案。 – pottolom

回答

1

OK,这似乎工作:

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)