I am trying to adapt an example from the Bokeh gallery (http://bokeh.pydata.org/en/latest/docs/gallery/unemployment.html )
with my own data. I only changed the data sources, yet when I run this
code in my Jupyter notebook I get the following error with show(p). Additionally, this then causes all of my other Bokeh plots in the same notebook to show the same error, even though they worked fine before running this below segment of code. The data types are all the same as the original example so I can’t figure out what is going on! Below is the error message (quite long) and my original code. Anyone have any ideas?
TypeError Traceback (most recent call last)
<ipython-input-32-948a877f9774> in <module>()
55 output_notebook()
56
---> 57 show(p) # show the plot
/usr/local/lib/python3.4/dist-packages/bokeh/io.py in show(obj, browser, new)
299
300 '''
--> 301 return _show_with_state(obj, _state, browser, new)
302
303 def _show_with_state(obj, state, browser, new):
/usr/local/lib/python3.4/dist-packages/bokeh/io.py in _show_with_state(obj, state, browser, new)
307
308 if state.notebook:
--> 309 comms_handle = _show_notebook_with_state(obj, state)
310
311 elif state.server_enabled:
/usr/local/lib/python3.4/dist-packages/bokeh/io.py in _show_notebook_with_state(obj, state)
328 else:
329 comms_target = make_id()
--> 330 publish_display_data({'text/html': notebook_div(obj, comms_target)})
331 handle = _CommsHandle(get_comms(comms_target), state.document, state.document.to_json())
332 state.last_comms_handle = handle
/usr/local/lib/python3.4/dist-packages/bokeh/embed.py in notebook_div(model, notebook_comms_target)
247
248 with _ModelInDocument(model):
--> 249 (docs_json, render_items) = _standalone_docs_json_and_render_items([model])
250
251 item = render_items[0]
/usr/local/lib/python3.4/dist-packages/bokeh/embed.py in _standalone_docs_json_and_render_items(models)
564 docs_json = {}
565 for k, v in docs_by_id.items():
--> 566 docs_json[k] = v.to_json()
567
568 return (docs_json, render_items)
/usr/local/lib/python3.4/dist-packages/bokeh/document.py in to_json(self)
730 # this is a total hack to go via a string, needed because
731 # our BokehJSONEncoder goes straight to a string.
--> 732 doc_json = self.to_json_string()
733 return loads(doc_json)
734
/usr/local/lib/python3.4/dist-packages/bokeh/document.py in to_json_string(self, indent)
718 'roots' : {
719 'root_ids' : root_ids,
--> 720 'references' : self._references_json(root_references)
721 },
722 'version' : __version__
/usr/local/lib/python3.4/dist-packages/bokeh/document.py in _references_json(cls, references)
529 for r in references:
530 ref = r.ref
--> 531 ref['attributes'] = r._to_json_like(include_defaults=False)
532 references_json.append(ref)
533
/usr/local/lib/python3.4/dist-packages/bokeh/models/sources.py in _to_json_like(self, include_defaults)
173 attrs = super(ColumnDataSource, self)._to_json_like(include_defaults=include_defaults)
174 if 'data' in attrs:
--> 175 attrs['data'] = transform_column_source_data(attrs['data'])
176 return attrs
177
/usr/local/lib/python3.4/dist-packages/bokeh/util/serialization.py in transform_column_source_data(data)
127 data_copy[key] = transform_array(data[key])
128 else:
--> 129 data_copy[key] = traverse_data(data[key])
130 return data_copy
/usr/local/lib/python3.4/dist-packages/bokeh/util/serialization.py in traverse_data(datum, is_numpy, use_numpy)
98 """
99 is_numpy = is_numpy and use_numpy
--> 100 if is_numpy and not any(isinstance(el, (list, tuple)) for el in datum):
101 return transform_array(np.asarray(datum))
102 datum_copy = []
TypeError: 'numpy.int64' object is not iterable
from math import pi, floor
from bokeh.models import HoverTool
from bokeh.plotting import ColumnDataSource, figure, show
from bokeh.palettes import Reds9
from bokeh.io import output_notebook
groups = data.groupby(['DayOfWeek', 'hour']).size()
max_crimes = max(groups)
min_crimes = min(groups)
factor = (max_crimes - min_crimes) / 8.9999999
days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
days_abbr = [day[:4] for day in days]
hours = list(range(24))
# Set up the data for plotting. We will need to have values for every
# pair of day/hour names. Map the rate to a color.
day= []
hour = []
color = []
counts = []
for d in days:
for h in hours:
day.append(d)
hour.append(str(h))
count = groups[d][h:h+1].values[0]
counts.append(count)
color_index = floor((count - min_crimes) / factor)
color.append(Reds9[color_index]) source = ColumnDataSource(
data=dict(day=day, hour=hour, color=color, count=count)
)
TOOLS = "resize,hover,save,pan,box_zoom,wheel_zoom"
p = figure(title="Frequency of Crimes Throughout the Week", x_range=[str(h) for h in hours], y_range=days,
x_axis_location="above", plot_width=900, plot_height=400,
toolbar_location="left", tools=TOOLS)
p.grid.grid_line_color = None
p.axis.axis_line_color = None
p.axis.major_tick_line_color = None
p.axis.major_label_text_font_size = "5pt"
p.axis.major_label_standoff = 0
p.xaxis.major_label_orientation = pi/3
p.rect("day", "hour", 1, 1, source=source,
color="color", line_color=None)
p.select_one(HoverTool).tooltips = [
('Time of Week', '@day @hour'),
('Crime Count', '@count'),]
output_notebook()
show(p) # show the plot