2017-04-18 66 views
1

我試圖用Bokeh(代碼中的data_frame)繪製以下數據框,在我的示例中,我只有兩列0和1(以及日期是x軸)。但在我的真實數據集中,我有超過10個,所以我試圖找到比我的更好的版本,這個版本沒有很好地概括。 (我想到了一個for循環,但它似乎不是最佳)散景圖時間序列

from bokeh.plotting import figure, show 
from bokeh.charts import TimeSeries 
from bokeh.io import output_notebook 

output_notebook() 

data_frame = pd.DataFrame({0: [0.17, 0.189, 0.185, 0.1657], 1: [0.05, 0.0635, 0.0741, 0.0925], 'Date': [2004, 2005, 2006, 2007]}) 
p = figure(x_axis_label = 'date', 
     y_axis_label='Topics Distribution') 

p.circle(data_frame.Date, data_frame.iloc[:, 0]) 
p.circle(data_frame.Date, data_frame.iloc[:, 1]) 

show(p) 

我已經試過這爲好,但它不工作,我不想只行分:

p = TimeSeries(data_frame, index='Date', legend=True, 
      title = 'T', ylabel='topics distribution') 

感謝您的幫助!

回答

0

讓我們嘗試了不同的方法,看看這使得一些更有意義:

  • 重塑數據是在一個 "tidy"數據格式

  • 使用散景高層散點圖與顏色參數

代碼:

chartdata = data_frame.set_index('Date').stack().reset_index().rename(columns={'level_1':'Category',0:'Value'}) 

print(chartdata) 

輸出 「整潔」 的數據格式:

Date Category Value 
0 2004   0 0.1700 
1 2004   1 0.0500 
2 2005   0 0.1890 
3 2005   1 0.0635 
4 2006   0 0.1850 
5 2006   1 0.0741 
6 2007   0 0.1657 
7 2007   1 0.0925 

生成圖表:

from bokeh.charts import Scatter 
p = Scatter(chartdata, x='Date', y='Value', color='Category',xlabel='date', ylabel='Topics Distribution') 

enter image description here

+0

哇,我從來沒有想過這個問題,那正是我想。非常感謝 :) – glouis