【问题标题】:ValueError: expected an element of either Enum('solid', 'dashed', 'dotted', 'dotdash', 'dashdot')ValueError: 期望 Enum('solid', 'dashed', 'dotted', 'dotdash', 'dashdot') 的元素
【发布时间】:2017-06-30 23:23:00
【问题描述】:

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

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'), Regex('^(\d+(\s+\d+)*)?$ ') 或 Seq(Int),得到 ['dashed', 'dashed', 'dashed', 'solid', 'solid']

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

【问题讨论】:

    标签: bokeh


    【解决方案1】:

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

    一些可以帮助您解决问题的事情。尝试使用 MultiLine 字形作为绘图并使用 ColumnDataSource 来构建数据。使用MultiLine 字形,可以设置一些线属性以针对多线的每个线段进行更改。 line_color 可以通过这种方式为每个段更改,但不幸的是 line_dash 属性不能。

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

    这是一个解决方案的示例。

    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属性仍然会产生错误。

    【讨论】:

      猜你喜欢
      • 2019-04-04
      • 2022-12-27
      • 1970-01-01
      • 2017-08-30
      • 2019-07-12
      • 2019-03-14
      • 1970-01-01
      • 2020-11-07
      • 1970-01-01
      相关资源
      最近更新 更多