python-3.x 使用€作为货币符号在NumeralTickFormatter从散景

mdfafbf1  于 2024-01-10  发布在  Python
关注(0)|答案(2)|浏览(466)

我想使用€符号而不是$来格式化我的数字在一个散景图,是由全息视图(hv.酒吧)创建。

  1. formatter = NumeralTickFormatter(format=f"{€ 0.00 a)")

字符串
不幸的是,这只产生一个格式化的数字,而不是欧元符号
此外,这里提到的变通方法
How to format bokeh xaxis ticks with currency

  1. formatter = PrintfTickFormatter(format=f'€ 0.00 a')


不起作用
我实际上认为散景应该适应这一点,并提供添加任何符号的可能性。

k5hmc34c

k5hmc34c1#

这可以使用FuncTickFormatter和一些TypeScript代码来完成。

  1. from bokeh.models import FuncTickFormatter
  2. p.xaxis.formatter = FuncTickFormatter(code='''Edit some typescript here.''')

字符串

最小示例如果您的目标是编辑0到1e7之间的值的x轴,这应该可以工作。这将为小于1000的值选择无单位,为1000到1e6之间的值选择k,为更大的值选择m

  1. p = figure(width=400, height=400, title=None, toolbar_location="below")
  2. x = [xx*1e6 for xx in range(1,6)]
  3. y = [2, 5, 8, 2, 7]
  4. p.circle(x, y, size=10)
  5. js = """
  6. if (tick < 1e3){
  7. var unit = ""
  8. var num = (tick).toFixed(2)
  9. }
  10. else if (tick < 1e6){
  11. var unit = "k"
  12. var num = (tick/1e3).toFixed(2)
  13. }
  14. else{
  15. var unit = "m"
  16. var num = (tick/1e6).toFixed(2)
  17. }
  18. return `€ ${num} ${unit}`
  19. """
  20. p.xaxis.formatter = FuncTickFormatter(code=js)
  21. show(p)

输出


的数据

展开查看全部
wb1gzix0

wb1gzix02#

NumeralTickFormatterPrintfTickFormatter是不同的,使用完全不同的格式字符串。如果你想使用PrintfTickFormatter,你需要给予一个有效的“printf”格式字符串:

  1. PrintfTickFormatter(format='€ %0.2f')

字符串


的数据
有效的printf格式都在文档中描述

相关问题