从数据框中读取数据并在x轴上使用时间时的空白散景图

时间:2019-05-06 07:37:11

标签: pandas bokeh

我无法使用Bokeh显示剧情。我正在从数据框中读取数据。这是我的Python代码的一小段。

我是Bokeh的新手。我尝试按照《用户指南》中的一些示例进行操作。我无法弄清楚这里出了什么问题。请指教。

import datetime
import pandas
from bokeh.plotting import figure, show, output_file, output_notebook
from bokeh.models import ColumnDataSource
PATH_TO_CSV = "Sample_Data.csv"

output_notebook()
data = pd.read_csv(PATH_TO_CSV, index_col=False)
data['timestamp'] = pd.to_datetime(data['timestamp']).dt.strftime("%H:%M:%S")
source = ColumnDataSource(data)
p = figure(plot_width=400, plot_height=400, x_axis_type="datetime")
p.line('timestamp', 'event_msg', source=source)
show(p)

这里是示例.csv

event_msg,timestamp
Created,2019-03-02 13:19:44.164562-0700
Created,2019-03-02 13:20:32.212323-0700
Created,2019-03-02 13:20:56.582761-0700
Modified,2019-03-02 13:21:48.021752-0700
Deleted,2019-03-02 13:22:16.938382-0700
Modified,2019-03-02 13:22:22.139714-0700
Permission changed,2019-03-02 13:24:20.195975-0700
Deleted,2019-03-02 13:33:53.049900-0700
Modified,2019-03-02 13:33:56.266113-0700
Deleted,2019-03-02 13:33:59.757584-0700

我看到的是完全空白的情节。理想情况下,我对根据事件消息绘制不同的线图感兴趣。

1 个答案:

答案 0 :(得分:1)

您应该像这样转换时间:

data['timestamp'] = pd.to_datetime(data['timestamp'])

所以您的代码应该看起来像(经过Bokeh v1.1.0测试):

import os
import datetime
import pandas as pd
from bokeh.plotting import figure, show, output_file, output_notebook
from bokeh.models import ColumnDataSource
PATH_TO_CSV = "Sample_Data.csv"

output_notebook()

data = pd.read_csv(os.path.join(os.path.dirname(__file__), PATH_TO_CSV), index_col = False)
data['timestamp'] = pd.to_datetime(data['timestamp'])
source = ColumnDataSource(data)
p = figure(plot_width = 400, plot_height = 400, x_axis_type = "datetime", y_range = data['event_msg'].unique())
p.line('timestamp', 'event_msg', source = source)
show(p)

结果:

enter image description here