I’m trying to create a plot with two “categorical” axis:
- the left-vertical axis is a real categorical axis: each category is unique.
- the right-vertical axis is a “fake” categorical axis: it represents a subgroup of some kind. Hence, categories might not be unique.
A picture is worth a thousand words, so here is what I’m trying to achieve:
Note the red texts on the right: these are my subgroups, and I painted over my resulting figure. There are 3 subgroups, c1, c2, c3.
My idea was to create a new categorical axis, with unique categories in the form of numbers, then use the major_label_overrides
attribute to provide a mapping with the correct labels. However, as can be seen from the picture above, the mapping doesn’t work.
How can I achieve my goal?
from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource, FactorRange, CategoricalAxis
from bokeh.io import output_notebook
output_notebook()
data = {
"categories": ["A", "B", "C", "D"],
"values": [10, 5, 8, 12],
"other": ["c1", "c2", "c2", "c3"],
}
fig = figure(height=300, y_range=data["categories"])
fig.hbar(y="categories", right="values", height=0.9, source=ColumnDataSource(data))
fig.x_range.update(start=0)
fig.extra_y_ranges['new_cat'] = FactorRange(
factors=[str(i) for i in range(len(data["categories"]))]
)
new_y_axis = CategoricalAxis(
x_range_name="default",
y_range_name="new_cat",
)
fig.add_layout(new_y_axis, "right")
new_y_axis.major_label_overrides = {
2: "test 1",
"2": "test 2",
"2.0": "test 3",
}
show(fig)