[英]Bokeh plot time series
我試圖用Bokeh(代碼中的data_frame)繪制下面的數據幀,在我的例子中我只有兩列0和1(和Dates是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')
謝謝你的幫助!
讓我們嘗試一種不同的方法,看看這是否更有意義:
將數據重新整形為“整潔”的數據格式
使用帶有顏色參數的Bokeh高級散點圖
碼:
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')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.