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

I am trying to change the x axis in the graph to get the ordinal values ​​of the text, and it’s hard for me to find a workaround. Here is my goal: I want to show the IRR 2 of the proposed modifications to the government program for senior citizens whose average income was $ 20,000 and $ 50,000 in their entire lives.

So, I have 4 IRRs: 2 for those who have $ 20,000, and 2 for those who have $ 50,000. What difficulty is that I do not have exact x coordinates; instead, I used the nominal x-coordinates to create my histograms (using the .quad () method).

Then, using the FixedTicker class, I got only 2 tickers that show: those that are between the bunkers that describe each income category. At this point, I hope that I can change these fixed tickers to some other custom tickers (perhaps using dict?), But I really don't know.

I guess here, so maybe I'm completely wrong in this FixedTicker strategy. Is there a way to change the tickers? If not, is there another way?

I tried to use categories on the x axis, but the problem was that I didn’t have pairs like category: value, it looks more like category: (value, value).

Here is my code:

from bokeh.io import show, output_notebook from bokeh.plotting import figure from bokeh.models import FixedTicker output_notebook() irr_fed = [4.24, 3.04] irr_prov = [2.59, 2.83] plot = figure(title="Internal Rates of Return: Federal vs. Provincial", y_range=(0,5), x_range=(0,12)) plot.quad(top=[irr_fed[0], irr_prov[0], irr_fed[1], irr_prov[1]], bottom=0, left=[1,3.5,7,9.5], right=[2.5,5,8.5,11]) plot.xaxis[0].ticker=FixedTicker(ticks=[3, 9]) show(plot) 

I would show the plot that I brought here, but I can not publish the images, as I am new here, and I do not have enough reputation. If you want to see it, the code works fine in a laptop.

+1
source share
1 answer

It might be useful to distinguish between Ticker and TickeFormatter . The first one chooses where to put the labels based on the actual start and end range of the chart. The latter controls how these ticks are displayed. It looks like you want to control the appearance of ticks more than anything else, i.e. You want to display some normalized coordinates in some way. This assumes that you want the custom TickFormatter format fixed ticks.

In particular, you can watch FuncTickFormatter , which allows you to provide a string or JS fragment to control tick formatting, 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) 

What generates this image:

enter image description here

Depending on your needs, you can specify that the Grid ticker should also be the same fixed ticker (so that the grid lines coincide with the ticks) or just disable the x-grid.

+1
source

Source: https://habr.com/ru/post/1259856/


All Articles