简体   繁体   中英

Datetime x axis in bokeh.charts.Area

Why I plot my data with a datetime series as x axis, it is interpreted as a datetime value, but the wrong one: my 2016 times are interpreted as milliseconds after 1970-1-1. Code:

from __future__ import absolute_import, division, print_function
from __future__ import unicode_literals

from datetime import datetime
import pandas as pd
from bokeh.charts import Area, show, output_file

df = pd.DataFrame()
df['date'] = [datetime(2016, 1, 1), datetime(2016, 1, 2), datetime(2016, 1, 3)]
df['v1'] = [1, 2, 3]
df['v2'] = [4, 4, 3]

p = Area(df, x='date', y=['v1', 'v2'], title="Area Chart",
         legend="top_left", xscale='datetime', stack=True,
         xlabel='time', ylabel='values')

output_file("area.html", title="Area Chart")
show(p)

面积图屏幕截图

Is there a way I can get bokeh.charts.Area recognize my datetime data, or do I have to construct the plot myself using figure() ?

Additional data: bokeh 0.11.1 on Python 2.7

The problem was the from __future__ import unicode statement.* Removing the line fixed the problem.

The core issue is that the the x='date' keyword argument must be bytes. Otherwise bokeh will not find the key in the dataframe. It does not show a warning or error in this case, it just silently replaces it with a numerical index (0, 1, 2, 3), which is interpreted as milliseconds by the date axis.


* Left out of the original question, because the same problem also surfaced after some pip confusion with leftover .egg-info directories.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM