Jupyter Bokeh:字形渲染器中不存在的列名 [英] Jupyter Bokeh: Non-existent column name in glyph renderer

查看:114
本文介绍了Jupyter Bokeh:字形渲染器中不存在的列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个GlyphRenderer,其data_source.data是

I have a GlyphRenderer whose data_source.data is

{'index': [0, 1, 2, 3, 4, 5, 6, 7],
 'color': ['#3288bd', '#66c2a5', '#abdda4', '#e6f598', '#fee08b', '#fdae61', '#f46d43', '#d53e4f']}

渲染器的字形是

Oval(height=0.1, width=0.2, fill_color="color")

渲染时,我看到了

E-1001 (BAD_COLUMN_NAME): Glyph refers to nonexistent column name: color [renderer: GlyphRenderer(id='1d1031f5-6ee3-4744-a0f7-22309798e313', ...)]

我显然丢失了一些东西,但是从已发布的示例中可以明显看出这一点.我在调试器中验证了data_source.column_names只是['index'];我不明白的是为什么'color'列没有出现在数据源的column_names中,还是为什么Bokeh发出此警告(图形似乎正确呈现).

I'm clearly missing something, but this is pretty much lifted from published examples. I verified in a debugger that data_source.column_names is just ['index']; what I don't understand is why the 'color' column doesn't appear in the data source's column_names, or why Bokeh produces this warning (the graph appears to be correctly rendered).

完整的资源可在 https://pastebin.com/HXAEEujP

推荐答案

通常最好在构造对象时提供所有相关参数,而不要在对象创建后对其进行突变.对于Bokeh尤其如此-在许多情况下,它会根据传递给__init__的参数来做一些额外的工作.

It's generally better to provide all relevant arguments when constructing an object rather than mutating the object after it's already been created. It's especially true for Bokeh - in many cases it does some additional work based on the arguments passed to __init__.

看看这个版本的代码:

import math

from bokeh.io import show
from bokeh.models import GraphRenderer, StaticLayoutProvider, Oval, GlyphRenderer, ColumnDataSource, MultiLine
from bokeh.palettes import Spectral8
from bokeh.plotting import figure

N = 8
node_indices = list(range(N))

plot = figure(title="Graph Layout Demonstration", x_range=(-1.1, 1.1), y_range=(-1.1, 1.1),
              plot_width=250, plot_height=250,
              tools="", toolbar_location=None)

node_ds = ColumnDataSource(data=dict(index=node_indices,
                                     color=Spectral8),
                           name="Node Renderer")
edge_ds = ColumnDataSource(data=dict(start=[0] * N,
                                     end=node_indices),
                           name="Edge Renderer")
### start of layout code
circ = [i * 2 * math.pi / 8 for i in node_indices]
x = [math.cos(i) for i in circ]
y = [math.sin(i) for i in circ]
graph_layout = dict(zip(node_indices, zip(x, y)))
graph = GraphRenderer(node_renderer=GlyphRenderer(glyph=Oval(height=0.1, width=0.2, fill_color="color"),
                                                  data_source=node_ds),
                      edge_renderer=GlyphRenderer(glyph=MultiLine(),
                                                  data_source=edge_ds),
                      layout_provider=StaticLayoutProvider(graph_layout=graph_layout))

plot.renderers.append(graph)

show(plot)

这篇关于Jupyter Bokeh:字形渲染器中不存在的列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆