【问题标题】:Inserting newlines in bokeh Tick Labels在散景刻度标签中插入换行符
【发布时间】:2021-05-14 05:54:25
【问题描述】:

我想在 Bokeh 中创建多行格式的刻度标签。 在常规 Python 中以这种方式格式化是微不足道的,例如

'{} \n{}'.format('Category', 'Percentage')

将打印

Percentage

在我的代码中,我这样做是为了创建我的绘图数据:

ds = OrderedDict(sorted(subdict.items(), key=lambda v: v[1], reverse=True))
ks = [i+', {:}% '.format(str(round(j/sum(ds.values())*100, None))) for i, j in ds.items()]
data = {'kz': ks, 'vals':list(ds.values())}
source = ColumnDataSource(data=data)

如果我在上述代码中的 format() 语句中插入一个换行符(例如 ', \n {:}%'),bokeh 会忽略它。

bokeh.models.PrintfTickFormatter 似乎没有提供换行参数,

bokeh.models.CategoricalTickFormatter 字面上什么都不做(方法是“通过”)

隐藏在散景文档中

https://docs.bokeh.org/en/latest/docs/reference/models/formatters.html

对于DatetimeTickFormatter 是声明:

%n 换行符。散景文本目前不支持换行符。

我只能假设这适用于散景中的所有文本。

任何人都可以确认、纠正或提供解决方法吗?

nb 在我的用例中,图表只导出为 png。

【问题讨论】:

    标签: python bokeh


    【解决方案1】:

    从 Bokeh 2.3 开始,多行刻度标签仅适用于分类因素:

    from bokeh.plotting import figure, show
    
    factors = [
        "a very long label \n that needs a break",
        "a short label",
    ]
    
    x = [50, 40]
    
    p = figure(y_range=factors)
    p.circle(x, factors, size=25, fill_color="orange", line_color="green", line_width=3)
    
    show(p)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-15
      • 2012-12-03
      • 1970-01-01
      • 1970-01-01
      • 2020-07-26
      • 2022-01-23
      • 1970-01-01
      相关资源
      最近更新 更多