散景带悬浮的多行 [英] Bokeh Multi-line with hover

查看:50
本文介绍了散景带悬浮的多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的熊猫数据框

I have pandas dataframe which looks like this

           'A' 'B' 'C'
2018/1/1    10  20  20
2018/1/2    34  13  23
2018/1/3    23  45  43
2018/1/4    14  98  76
2018/1/5    58  65  57 

如何将其转换为columnDataSource? 如何使用悬停工具在bokeh中创建多线图. 以X轴为日期

How do I convert this to columnDataSource ? How do I create a multi-line graph in bokeh with hover tool. X-axis as the date

推荐答案

导入:

import pandas as pd
from bokeh.models import ColumnDataSource, HoverTool
from bokeh.plotting import figure, show, output_notebook
output_notebook()

以下是您提供的数据:

days = ['2018/1/1', '2018/1/2', '2018/1/3', '2018/1/4', '2018/1/5']
data_a = [10, 34, 23, 14, 58]
data_b = [20, 13, 45, 98, 65]
data_c = [20, 23, 43, 76, 57]

创建DataFrame:

Create DataFrame:

df_plot = pd.DataFrame({'A': data_a, 'B': data_b, 'C': data_c}, index=days)

             A   B   C
2018/1/1    10  20  20
2018/1/2    34  13  23
2018/1/3    23  45  43
2018/1/4    14  98  76
2018/1/5    58  65  57

但是,索引不是正确的日期时间格式,因此请使用正确的格式创建日期列:

However, the index is not a proper datetime format, so create a dates column with the proper format:

df_plot['dates'] = pd.to_datetime(df_plot.index, format='%Y/%m/%d')

             A   B   C       dates
2018/1/1    10  20  20  2018-01-01
2018/1/2    34  13  23  2018-01-02
2018/1/3    23  45  43  2018-01-03
2018/1/4    14  98  76  2018-01-04
2018/1/5    58  65  57  2018-01-05

现在进行绘图:

source = ColumnDataSource(df_plot)
p = figure(x_axis_type="datetime")
p.line('dates', 'A', source=source, color='red')
p.line('dates', 'B', source=source, color='blue')
p.line('dates', 'C', source=source, color='green')
p.add_tools(HoverTool(tooltips=[("A", "@A"), ("B", "@B"), ("C", "@C")]))
show(p)

这只是一个png,实际输出将具有胡佛工具.

This is just a png, the actual output will have hoover tools.

这篇关于散景带悬浮的多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆