Bokeh graph doesn't plot properly

时光毁灭记忆、已成空白 提交于 2019-12-02 20:13:29

问题


The following code doesn't generate a graph:

import pandas
import numpy as np
from bokeh.plotting import figure, show, output_file
from bokeh.io import output_notebook
from datetime import datetime

output_notebook()

TOOLS="hover,crosshair,pan,wheel_zoom,zoom_in,zoom_out,box_zoom,undo,redo,reset,\
    tap,save,box_select,poly_select,lasso_select,"

df = pandas.read_csv('./logs.csv')
df['datetime'] = pd.to_datetime(df['datetime'])
xvals = df['datetime'].dt.strftime('%Y-%m-%d')
yvals = df['datetime'].dt.strftime('%H:%M:%S')

p = figure(title="Test Title", width=500, height=500, \
           x_axis_type="datetime", y_axis_type="datetime", \
           x_range=(df.iloc[-1]['datetime'].strftime('%Y/%m/%d'),\
                    df.iloc[0]['datetime'].strftime('%Y/%m/%d')),\
           y_range=('00:00:00','23:59:59'),\
           tools=TOOLS)
p.scatter(xvals, yvals, alpha=0.5)
show(p)

This graph produced is a blank graph. What is the problem?

EDIT:

I updated the code with

xvals = df['datetime'].dt.date
yvals = df['datetime'].dt.time

p = figure(title="Activity history", width=800, height=500, \
           x_axis_type='datetime', y_axis_type='datetime',\
           x_axis_label="Date", y_axis_label="Time",\
           tools=TOOLS)

p.scatter(xvals, yvals, alpha=0.3)
show(p)

And this produces a graph.


回答1:


OK, as far as I can tell, this is what you want (using some project sample data, since you did not provide anything to run your code with):

from bokeh.plotting import figure, show
from bokeh.sampledata.commits import data

p = figure(x_axis_type="datetime", y_axis_type="datetime")
p.circle(x=data.index, y=data.index.time)
show(p)

The datetime axis type, as the name suggests, treats the timestamps as datetimes. I.e., these are interpreted as hours of the day in the first day of the first year of Epoch. That's why the axis starts and ends with 1/01 and 1/02. You might want to use customize the tick formatter to display just the hours.

For reference, data looks like this:



来源:https://stackoverflow.com/questions/54747845/bokeh-graph-doesnt-plot-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!