Python Bokeh-带百分号的格式标签

Python Bokeh-带百分号的格式标签,python,bokeh,Python,Bokeh,使用Python3和Bokeh0.13.0 我有一个使用浮点数的图,比如22.6。这些数字实际上是百分比。我在一条线上的每个点都有标签,但我想不出在标签中包含%符号的方法(比如22.6%)。我能够很好地格式化axis,但是标签给我带来了困难。下面是定义标签的代码部分 labels = LabelSet(x='x', y='y', text='y', level='glyph',y_range_name="line", x_offset=-8, y_of

使用Python3和Bokeh0.13.0

我有一个使用浮点数的图,比如22.6。这些数字实际上是百分比。我在一条线上的每个点都有标签,但我想不出在标签中包含%符号的方法(比如22.6%)。我能够很好地格式化axis,但是标签给我带来了困难。下面是定义标签的代码部分

labels = LabelSet(x='x', y='y', text='y', level='glyph',y_range_name="line",
                      x_offset=-8, y_offset=10, source=source_line, render_mode='canvas',
                      text_font_size='8pt', text_color='white', background_fill_color="gray")
p.add_layout(labels)
任何帮助都将不胜感激

您有两种选择:

  • 您可以预先格式化python中的所有数据,将其存储在
    ColumnDataSource
    中的新列中,并使用该列驱动标签文本,例如

    source.data['formatted_y'] = ["%f %" % x for x in source.data['y']]
    LabelSet(text='formatted_y', ...)
    
  • 您可以使用
    CustomJSTransform
    转换JavaScript端的数据,例如

    from bokeh.models import CustomJSTransform
    from bokeh.transform import transform
    
    formatter = CustomJSTransform('y',  func="", v_func="""
        out = []
        for (i=0; i < xs.length; i ++) {
           out.push(xs[i] + " %")
        }
        return out
    """)
    LabelSet(text=transform('y', formatter), ...)
    
    来自bokeh.models导入CustomJSTransform
    从bokeh.transform导入transform
    格式化程序=CustomJSTransform('y',func=“”,v_func=“”)
    out=[]
    对于(i=0;i
    注意:未经测试,因为您没有提供完整的示例代码,所以可以使用它进行测试,但应该在大致范围内


unrelated
Area
此处回答的问题:谢谢,我最终使用了HoloView,这使得面积绘图非常简单。编辑…显然,这个答案也暗示了图书馆。