Can I change tickers in a plot to custom text tickers?

六眼飞鱼酱① 提交于 2019-12-01 10:59:57

It's probably useful to draw out the distinction between a Ticker and TickeFormatter. The former chooses where to put ticks, based on the actual start and end of a plot range. The latter controls how those ticks are displayed. It sounds like you want to control the appearance of the ticks more than anything else, i.e. you want to display some normalized coordinates somehow differently. This suggests you want a custom TickFormatter to format your fixed ticks.

In particular, you might look at the FuncTickFormatter which lets you supply a line or snippet of JS to control the formatting of the tick, arbitrarily. Here is an example:

from bokeh.models import FuncTickFormatter, FixedTicker
from bokeh.plotting import figure, show, output_file

output_file("formatter.html")

p = figure(plot_width=500, plot_height=500, x_range=(0,10))
p.circle([3, 9], [4, 8], size=30)

p.xaxis.ticker=FixedTicker(ticks=[3, 9])
p.xaxis.formatter = FuncTickFormatter(code="""
    var mapping = {3: "$20 000", 9: "$50 000"};
    return mapping[tick];
""")

show(p)

Which generates this image:

Depending on your needs you may want to set the Grid ticker to also be the same fixed ticker (so that the grid lines match up to the ticks) or just disable to x-grid.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!