问题
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
回答1:
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.
来源:https://stackoverflow.com/questions/35910802/datetime-x-axis-in-bokeh-charts-area