Categorical Bar plot, relabel categories and groups?

Hello Bokeh users,

I’m pretty new to using Bokeh, so this might be a beginner’s question. I’m generating a Bar plot from a pandas.DataFrame by means of hvPlot which is configured to use Bokeh as its backend. I’ve added a hook that allows fiddling with the underlying Bokeh figure. I want to relabel the categories and groups (as far as I’ve understood, that’s what the two levels of a categorical bar plot are called; don’t hesitate to correct me if I’m wrong).

To be more precise I’ve attached a picture of the labels that I want to change. I don’t want to change the labels in my dataset but instead would like to know if there’s a way to do it on the plot itself.

I’m talking about the labels marked in purple and green in the above image.

Thank you

@HWiese1980 I am afraid I don’t actually know whether this will be simple or not. There is a FuncTickFormatter that can be used to override tick appearance with snippets of JavaScript. This works great for numerical axes, but as far as I know It’s not been tried with a categorical axis. I would start there, though. In particular, I would suggest just putting

return ticks

as the formatting code to start. That will allow you to see the tick structure as it comes in to the formatter (in your browser JavaScript console). You will need to output the exact same structure, but with the labels adjusted however you want.

I see, thanks for the hint. I must say, though, and I’m afraid I forgot to explicitly mention it, that I’m using Bokeh in a Jupyter Notebook, that is in Python code. That’s fine, I guess, I can still do what you suggest. However, I’d rather firstly like to see if it’s possible using pure Python. If it’s not, it shouldn’t be too much of a hassle to give your suggestion a shot.

Also, I must admit, I’m not quite sure if I’ve already gotten my head around the basic concepts of Bokeh. I’ve just used it as a backend for some higher-level abstraction libraries (Holoviews, hvPlot) in my Python code. The only direct contact that I’ve had with Bokeh so far was in the rendering hook that I used to add some horizontal lines to the categorical bar plot - which worked like a charm, and I had hoped it would be as easy to change the mentioned labels.

It’s not. :slight_smile: One of the good big-picture things to keep in mind about Bokeh is that almost no work happens in Python.[1] Being a library that renders in to browsers, almost all the real work—for instance formatting tick values on axes—is performed by the JavaScript library BokehJS. We have provided lots of pre-made things that can be adjusted from Python via some configuration or other, but this is not one of those cases. In that event, we at least try to provide “CustomJS” hooks that let users get at whatever functionality they need with a modicum of JS code.

  1. The Bokeh server being the one notable exception. ↩︎