ValueError:期望Enum的元素('solid','dashed','dotted','dotdash','dashdot')

时间:2017-06-30 10:56:30

标签: bokeh

我正在尝试输出一个线图,其中破折号由数据驱动。我的例子是:

from bokeh.plotting import figure, output_notebook, show
output_notebook()

x = [1, 2, 3, 4, 5]
y = [6, 7, 2, 4, 5]
z = ['dashed', 'dashed', 'dashed', 'solid', 'solid']

p = figure(plot_width=400, plot_height=400)

p.line(x, y, line_width=2, line_dash=z)

show(p)

然而,这导致:

  

ValueError:期望Enum的元素('solid','dashed','dotted','dotdash','dashdot'),正则表达式('^(\ d +(\ s + \ d +)*)?$ ')或Seq(Int),得到['破灭','破灭','破灭','坚实','坚实']

是否可以根据我的数据集设置线型?

1 个答案:

答案 0 :(得分:1)

无法使用行字形列表设置行短划线。

一些可以帮助您解决问题的方法。尝试使用MultiLine字形作为绘图,使用ColumnDataSource来构建数据。使用MultiLine字形可以为多行的每个段设置一些行属性。可以为每个段更改line_color,但不幸的是line_dash属性不能。

您可以尝试在Bokeh's Github上打开问题,以允许line_dash字形中的MultiLine属性设置为ColumnDataSource

以下是解决方案的示例。

from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource
from bokeh.models.glyphs import MultiLine

source = ColumnDataSource(data=dict(z=['dashed', 'dashed', 'solid', 'solid'],
                                    xs=[[1, 2], [2, 3], [3, 4], [4, 5]],
                                    ys=[[6, 7], [7, 2], [2, 4], [4, 5]],
                                    color=['green', 'red', 'green', 'green']))

p = figure(plot_width=400, plot_height=400)

glyph = MultiLine(xs='xs', ys='ys', line_color = 'color', line_dash='z', line_width=2)
p.add_glyph(source, glyph)

show(p)

在上面的代码中,可以使用ColumnDataSource设置颜色,这可用于区分段,但line_dash属性仍会产生错误。