我正在Python 3 Jupyter Notebook中使用Bokeh来创建简单的热图。我以employees.py示例(来自https://bokeh.pydata.org/en/latest/docs/gallery/unemployment.html)为工作基础。我对Python和Bokeh相当陌生,但是由于我同时在x和y轴上使用分类数据,因此我无法让LabelSet正常工作。它似乎设计为使用x-y坐标。
我尝试将分类数据用作LabelSet的x和y,但始终出现错误。我不确定从这里要去哪里。
我正在使用https://bokeh.pydata.org/en/latest/docs/user_guide/annotations.html的“标签”部分来指导我。
labels = LabelSet(x='AppointmentDate', y='BookingMasterPage', text='EventCount', level='glyph', x_offset=0, y_offset=0, source=df, render_mode='canvas')
AppointmentDate和BookingMasterPage是分类数据。我希望在每个矩形内显示EventCount。上面的代码最终引发以下错误:
ValueError Traceback (most recent call last)
<ipython-input-20-44d66cdb039c> in <module>
47
48 labels = LabelSet(x='AppointmentDate', y='BookingMasterPage', text='EventCount', level='glyph',
---> 49 x_offset=0, y_offset=0, source=df, render_mode='canvas')
50
51 color_bar = ColorBar(color_mapper=mapper, major_label_text_font_size="8pt",
~\Anaconda3\lib\site-packages\bokeh\model.py in __init__(self, **kwargs)
259 self._document = None
260 self._temp_document = None
--> 261 super(Model, self).__init__(**kwargs)
262 default_theme.apply_to_model(self)
263
~\Anaconda3\lib\site-packages\bokeh\core\has_props.py in __init__(self, **properties)
251
252 for name, value in properties.items():
--> 253 setattr(self, name, value)
254
255 def __setattr__(self, name, value):
~\Anaconda3\lib\site-packages\bokeh\core\has_props.py in __setattr__(self, name, value)
278
279 if name in props or (descriptor is not None and descriptor.fset is not None):
--> 280 super(HasProps, self).__setattr__(name, value)
281 else:
282 matches, text = difflib.get_close_matches(name.lower(), props), "similar"
~\Anaconda3\lib\site-packages\bokeh\core\property\descriptors.py in __set__(self, obj, value, setter)
543 raise RuntimeError("%s.%s is a readonly property" % (obj.__class__.__name__, self.name))
544
--> 545 self._internal_set(obj, value, setter=setter)
546
547 def __delete__(self, obj):
~\Anaconda3\lib\site-packages\bokeh\core\property\descriptors.py in _internal_set(self, obj, value, hint, setter)
764
765 '''
--> 766 value = self.property.prepare_value(obj, self.name, value)
767
768 old = self.__get__(obj, obj.__class__)
~\Anaconda3\lib\site-packages\bokeh\core\property\bases.py in prepare_value(self, obj_or_cls, name, value)
325 break
326 else:
--> 327 raise e
328 else:
329 value = self.transform(value)
~\Anaconda3\lib\site-packages\bokeh\core\property\bases.py in prepare_value(self, obj_or_cls, name, value)
318 try:
319 if validation_on():
--> 320 self.validate(value)
321 except ValueError as e:
322 for tp, converter in self.alternatives:
~\Anaconda3\lib\site-packages\bokeh\core\property\instance.py in validate(self, value, detail)
112 if not isinstance(value, self.instance_type):
113 msg = "" if not detail else "expected an instance of type %s, got %s of type %s" % (self.instance_type.__name__, value, type(value).__name__)
--> 114 raise ValueError(msg)
115
116 def _may_have_unstable_default(self):
ValueError: expected an instance of type DataSource, got AppointmentDate BookingMasterPage EventCount
答案 0 :(得分:0)
问题在于 level='glyph'
。您应该使用 level=overlay
,因为您希望您的标签可见。