Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
369 views
in Technique[技术] by (71.8m points)

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

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 proposed modifications of a government program for the retirees who had middle incomes of 20 000$ and 50 000$ in their whole life.

So, I have 4 IRR: 2 for the ones with 20 000$, and 2 for the ones with 50 000$. What's tricky is that I don't have exact x-coordinates; instead, I used nominal x-coordinates to build my histogram bins (using .quad() method).

Then, using the FixedTicker class, I got only 2 tickers to show: the ones that sit between the bins describing each revenue category. At this point, I'm doing this hoping that I can change these fixed tickers to some other custom tickers (maybe using a dict?), but I really don't know.

I'm guessing here, so maybe I'm completey wrong with this FixedTicker strategy. Is there actually a way to modify tickers? If not, is there any other way?

I tried to use categories on the x-axis, but the problem was that I don't have pairs like category:value, it's 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'd show the plot I get here, but I can't publish images yet, since I'm new here and I don't have enough reputation. If you want to see it, the code works fine in Notebook.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

enter image description here

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...