带postgres DB数据标记的bokeh折线图

2024-04-20 15:55:08 发布

您现在位置:Python中文网/ 问答频道 /正文

数据行如下所示:

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,图例颜色按“名称”分组

我写了一些代码如下,但它显示了一个空白页。好像出了什么问题。我也不知道如何添加数据标记。从互联网上搜索,似乎应该合并一个折线图和一个圆图,以便完成它。在

有人能帮我纠正错误并告诉我怎么做吗?在

^{pr2}$

Tags: 数据代码name标记名称date颜色代表
2条回答

从你从测向仪上读取颜色的事实来看,你可能需要画几条线。如果是这样的话,您应该使用multi_line,而不是{}。看到了吗

http://docs.bokeh.org/en/latest/docs/user_guide/plotting.html#multiple-lines

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

更新

创建一些数据:

^{pr2}$

看起来像这样:

enter image description here

我把列名改成了实际的颜色。然后您需要重新排列数据以便使用多行:

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

由于我不能评论巴勃罗的回答,我是在他的回答的基础上加上我的。 它对我来说也是空白的,问题来自时间戳,您需要确保在数据伪造中使用了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)

谢谢巴勃罗的回答,这对我帮助很大

相关问题 更多 >