问题
NOTE FROM BOKEH MAINTAINER: The bokeh.charts
API including TimeSeries
was deprecated and removed a long time ago. This question is not relevant as-is to any recent or future versions of Bokeh. To plot time series, use the stable and supported bokeh.plotting
API. Some examples can be found here.
I am trying to plot a Timeseries
plot with categories.
xaxis_values: startTIme
yaxis_values: count
groupby: day
Every day has 24 hours data sets and like this the entire dataset has more than 100 days I am trying to have few type of plots.
Group by
day
and sum all thecounts
of every hours fromstartTime
which will give 7 time series plots in one graph.Separate by day i.e. every
mon, tue, wed
and so on whatever the number of daysn
and plot a 24 hrs time series.Group by hours irrespective of days. i.e.
00:00:00, 01:00:00
and so on
What is the best way to get the better visualization with bokeh or seaborn .
Input:
2004-01-05,22:00:00,23:00:00,Mon,18944,790
2004-01-05,23:00:00,00:00:00,Mon,17534,750
2004-01-06,00:00:00,01:00:00,Tue,17262,747
2004-01-06,01:00:00,02:00:00,Tue,19072,777
2004-01-06,02:00:00,03:00:00,Tue,18275,785
2004-01-06,03:00:00,04:00:00,Tue,13589,757
2004-01-06,04:00:00,05:00:00,Tue,16053,735
2004-01-06,05:00:00,06:00:00,Tue,11440,636
2004-01-06,06:00:00,07:00:00,Tue,5972,513
2004-01-06,07:00:00,08:00:00,Tue,3424,382
2004-01-06,08:00:00,09:00:00,Tue,2696,303
2004-01-06,09:00:00,10:00:00,Tue,2350,262
2004-01-06,10:00:00,11:00:00,Tue,2309,254
Code: Reference: Here
import numpy as np
import pandas as pd
from bokeh.charts import TimeSeries, show, output_file, vplot
output_file("timeseries.html")
data_one = pd.read_csv('one_hour.csv')
data_one.columns = ['date', 'startTime', 'endTime', 'day', 'count', 'unique']
data = dict(data_one=data_one['count'])
tsline = TimeSeries(data,
x='startTime', y='count',
color=['day'], title="Timeseries", ylabel='count', legend=True)
show(vplot(tsline))
Error:
Traceback (most recent call last):
File "date_graph.py", line 10, in <module>
data = dict(data_one=data_one['count'])
File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 1997, in __getitem__
return self._getitem_column(key)
File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 2004, in _getitem_column
return self._get_item_cache(key)
File "/usr/local/lib/python2.7/dist-packages/pandas/core/generic.py", line 1350, in _get_item_cache
values = self._data.get(item)
File "/usr/local/lib/python2.7/dist-packages/pandas/core/internals.py", line 3290, in get
loc = self.items.get_loc(item)
File "/usr/local/lib/python2.7/dist-packages/pandas/indexes/base.py", line 1947, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas/index.pyx", line 137, in pandas.index.IndexEngine.get_loc (pandas/index.c:4154)
File "pandas/index.pyx", line 159, in pandas.index.IndexEngine.get_loc (pandas/index.c:4018)
File "pandas/hashtable.pyx", line 675, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12368)
File "pandas/hashtable.pyx", line 683, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12322)
KeyError: 'count
Edit: After changing
data = dict(data_one=data_one['count'].tolist())
Error:
Traceback (most recent call last):
File "date_graph.py", line 12, in <module>
tsline = TimeSeries(data, x='startTime', y='count', color=['startTime'], title="Timeseries", ylabel='count', legend=True)
File "/usr/local/lib/python2.7/dist-packages/bokeh/charts/builders/timeseries_builder.py", line 102, in TimeSeries
return create_and_build(builder_type, data, **kws)
File "/usr/local/lib/python2.7/dist-packages/bokeh/charts/builder.py", line 67, in create_and_build
chart.add_builder(builder)
File "/usr/local/lib/python2.7/dist-packages/bokeh/charts/chart.py", line 149, in add_builder
builder.create(self)
File "/usr/local/lib/python2.7/dist-packages/bokeh/charts/builder.py", line 518, in create
chart.add_renderers(self, renderers)
File "/usr/local/lib/python2.7/dist-packages/bokeh/charts/chart.py", line 144, in add_renderers
self.renderers += renderers
File "/usr/local/lib/python2.7/dist-packages/bokeh/core/property_containers.py", line 18, in wrapper
result = func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/bokeh/core/property_containers.py", line 77, in __iadd__
return super(PropertyValueList, self).__iadd__(y)
File "/usr/local/lib/python2.7/dist-packages/bokeh/charts/builders/line_builder.py", line 230, in yield_renderers
x=group.get_values(self.x.selection),
File "/usr/local/lib/python2.7/dist-packages/bokeh/charts/data_source.py", line 173, in get_values
return self.data[selection]
File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 1997, in __getitem__
return self._getitem_column(key)
File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 2004, in _getitem_column
return self._get_item_cache(key)
File "/usr/local/lib/python2.7/dist-packages/pandas/core/generic.py", line 1350, in _get_item_cache
values = self._data.get(item)
File "/usr/local/lib/python2.7/dist-packages/pandas/core/internals.py", line 3290, in get
loc = self.items.get_loc(item)
File "/usr/local/lib/python2.7/dist-packages/pandas/indexes/base.py", line 1947, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas/index.pyx", line 137, in pandas.index.IndexEngine.get_loc (pandas/index.c:4154)
File "pandas/index.pyx", line 159, in pandas.index.IndexEngine.get_loc (pandas/index.c:4018)
File "pandas/hashtable.pyx", line 675, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12368)
File "pandas/hashtable.pyx", line 683, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12322)
KeyError: 'startTime'
来源:https://stackoverflow.com/questions/39025705/timeseries-plots-with-bokeh