2016-11-25 64 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)