2017-03-09 114 views
1

數據行的樣子:背景虛化線圖從Postgres的DB

date name val 
2017-02-05 a 600.0 
2017-02-05 b 345.0 
2017-02-05 c 679.0 
2017-02-05 d 0.0 
2017-02-05 e 66.0 
2017-02-05 f 0.0 
2017-02-05 g 156.0 
2017-03-05 a 634.0 
2017-03-05 b 0.0 
2017-03-05 c 2679.0 
2017-03-05 d 0.0 
2017-03-05 e 9266.0 
2017-03-05 f 0.0 
2017-03-05 g 56.0 

我想產生與數據標記折線圖。 x會是日期,y會是val,圖例顏色會被分組爲「name」似乎有什麼問題。我也不確定如何添加數據標記。從互聯網搜索,似乎應該合併一個折線圖和一個圓形圖表,以完成它。

有人可以幫助解決這個錯誤,並告訴我如何做到這一點?

from bokeh.charts import Line, show, output_file, TimeSeries,Scatter 
from bokeh.plotting import figure 
from bokeh.models import HoverTool 
import sqlalchemy as sa 
import pandas as pd 


# database credentials 
usr = 'test' 
pswd = 'test' 
db = 'test' 


# create the connection to the database 
engine = sa.create_engine(
    'postgresql://{0}:{2}@localhost:3552/{1}' \ 
    .format(usr,db,pswd) 
) 


query = "select date::text,name,round(size/1024/1024/1024) as val from test order by 1,2" 

# extract the data 
df = pd.read_sql_query(query, engine) 

output_file("example_chart.html") 

# create a line chart where each column of measures receives a unique color and dash style 
plot = figure(title="Example of a line chart", x_axis_label='Date', y_axis_label='size in GB') 

plot.line(x=df['date'], y=df['val'], color=df['name']) 

show (plot) 

回答

0

從您正在閱讀df顏色的事實表明您可能想要繪製幾行。如果是這種情況,您應該使用multi_line而不是line。見http://bokeh.pydata.org/en/latest/docs/user_guide/plotting.html#multiple-lines

plot.multi_line(xs=df['date'], ys=df['val'], color=df['name']) 

更新

創建一些數據:

import pandas as pd 
import numpy as np 
data = np.array([pd.DatetimeIndex(['2017-02-05']*7+['2017-03-05']*7), 
["red","green","blue","orange","black","yellow","purple"]*2, 
[600.0,345.0,679.0,0.0,66.0,0.0,156.0,634.0,0.0,2679.0,0.0,9266.0,0.0,56.0]]) 
df = pd.DataFrame(data=data.T,columns = ["date","name","val"]) 

,看起來像這樣:

enter image description here

我已經改變了列名實際顏色。然後,你需要重新排列,以使用multi_line您的數據:

import bokeh 
import bokeh.plotting 

p = bokeh.plotting.figure(plot_width=300,plot_height=300, x_axis_type="datetime") 
dates = [df[df.name.isin([x])].date for x in df.name.unique()] 
vals = [df[df.name.isin([x])].val for x in df.name.unique()] 
p.multi_line(xs=dates,ys=vals, color = df.name.unique()) 

bokeh.io.output_file("example_chart.html") 
bokeh.io.show(p) 

輸出看起來像:

enter image description here

+0

嗨巴勃羅,仍然是空白圖表。 – fairybetty

+0

這將幫助我,並可能你自己之前:http://stackoverflow.com/help/mcve –

+0

嗨,帕布洛,我用您建議的「multi_line」函數替換原始線「plot.line」。 python文件可以在沒有錯誤發生的情況下執行。但是,打開的html頁面是空白的。它有圖表的標題和一個灰色框,裏面應該有折線圖,但它是空的。 – fairybetty

0

正如我不能發表評論Pablo的回答,我根據他的回答添加我的。 這對我來說也是空白的,問題來自時間戳,您需要確保您在數據處理中使用DatetimeIndex。

這裏是我的課堂我的代碼提取物(我不擅長編碼,這樣的話,歡迎),它和PostgreSQL中獲取數據,並繪製它:

def getColumn(self, column): 
    self.logger.info('Getting ' + column +' from DB...') 
    self.cur.execute("SELECT " + column + " FROM history") 

    return [i[0] for i in self.cur.fetchall()] 

def multiAccountsPlot(self): 

    #I sort the blank issue by making sure the dates format was the same as Pablo's sample 
    data = np.array([pd.DatetimeIndex([i.isoformat() for i in self.getColumn("timestamp")]), 
     self.getColumn("account"), 
     self.getColumn("amount")]) 
    df = pd.DataFrame(data=data.T,columns = ["date","account","amount"]) 

    p = bokeh.plotting.figure(width=800, height=350, x_axis_type="datetime") 
    dates = [df[df.account.isin([x])].date for x in df.account.unique()] 
    vals = [df[df.account.isin([x])].amount for x in df.account.unique()] 

    mypalette=Spectral11[0:len(df.account.unique())] 
    p.multi_line(xs=dates,ys=vals, line_color=mypalette) 

    bokeh.io.output_file("example_chart.html") 
    bokeh.io.show(p) 

感謝巴勃羅的回答,它幫助我很多