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

后端 未结 1 1560
我寻月下人不归
我寻月下人不归 2021-01-15 06:24

I\'m trying to change the x-axis on a plot to get ordinal text values and I\'m having a hard time trying to find a workaround. Here is my goal: I want to show the IRR of 2 p

相关标签:
1条回答
  • 2021-01-15 06:36

    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.

    0 讨论(0)
提交回复
热议问题