ColumnDataSource中的散景线图颜色

时间:2017-06-20 03:17:04

标签: python plot bokeh renderer

我想使用ColumnDataSource设置散景线图(Bokeh版本0.12.5)的颜色。但是,对于线图,没有绘制任何内容。另一方面,如果我使用圆形渲染器,一切都按预期工作。下面是一个包含线图和圆图的示例程序,您可以注释/取消注释相应的线以查看绘图行为。我还为线图绘制了一行代码,其中明确定义了颜色,并且绘图完美无缺。我已经看到了几个类似的问题,但找不到解决这个问题的可靠方法,或者确定我是否只是做了一些根本错误的事情。谢谢你的帮助。

# bokeh version 0.12.5
# run in terminal with: python -m bokeh serve --show line_plot_color.py

from bokeh.io import curdoc
from bokeh.models import ColumnDataSource
from bokeh.plotting import Figure
from bokeh.layouts import row

source = ColumnDataSource(data = dict(color = ['green','green','green'], xs = [1,2,3], ys = [1,2,3]))
fig = Figure(plot_width=300, plot_height=300)

#r = fig.circle('xs','ys',source = source, size = 12, fill_color = 'color') # works as expected
r = fig.line('xs','ys',source = source, line_color = 'color') # fails to plot; no errors or warnings in terminal
#r = fig.line('xs','ys',source = source, line_color = 'green')  # works as expected

layout = row(fig)
curdoc().add_root(layout)

1 个答案:

答案 0 :(得分:1)

首先帮助您调试散景服务器,使用Web浏览器附带的devtools非常有用。 devtools的控制台将包含有用的调试信息,就像您的示例一样。

其次,查看文档时,行字形方法未设置为接收其着色的列数据源值。如果要在单个图形上绘制具有不同颜色的多条线,则可以使用multi_line字形。要使用此字形,您需要将数据源xs和ys修改为multi_line中每行的列表。这是一个简单的例子。

source2 = ColumnDataSource(data = dict(color = ['green','red'], xs = [[1, 2],[2, 4]], ys = [[1, 2],[2, 4]]))
r = fig.multi_line('xs','ys',source = source2, line_color = 'color')