Skip to main content
 首页 » 编程设计

python之如何在 Bokeh 中仅显示每第 n 个分类票

2024年09月07日6dyllove98

两年前有同样的问题。好像当时不支持every nth categorical tickers。

https://stackoverflow.com/questions/34949298/python-bokeh-show-only-every-second-categorical-ticker

我的 Bokeh 版本是 0.12.13。我想知道现在是否支持它。

简单地设置 p.xaxis.ticker = ['A', 'B, 'C'] 不起作用(抛出错误)

在我的 dashbaord 中,初始绘图大小是浏览器视口(viewport)的四分之一,x 轴上挤满了许多股票代码和标签。所以我只想显示 10 个代码,然后在放大图时显示所有代码。

请您参考如下方法:

Bokeh 没有内置任何功能来执行此操作。你可以用 custom extension 完成一些事情:

from bokeh.models CategoricalTicker 
 
JS_CODE = """ 
import {CategoricalTicker} from "models/tickers/categorical_ticker" 
 
export class MyTicker extends CategoricalTicker 
  type: "MyTicker" 
 
  get_ticks: (start, end, range, cross_loc) -> 
    ticks = super(start, end, range, cross_loc) 
 
    # drops every other tick -- update to suit your specific needs 
    ticks.major = ticks.major.filter((element, index) -> index % 2 == 0) 
 
    return ticks 
 
""" 
 
class MyTicker(CategoricalTicker): 
    __implementation__ = JS_CODE 
 
p.xaxis.ticker = MyTicker() 

请注意,上面定义的简单 get_ticks 不会处理具有嵌套类别等的更复杂情况。