0
import hvplot.pandas
from bokeh.sampledata.autompg import autompg_clean
autompg_clean['origin']=autompg_clean.origin.map({'North America': 'North America '*5,
                                                  'Asia': 'Asia '*5,
                                                  'Europe': 'Europe '*5,
                                                 })

Here is the corresponding annotated output. I have tried using p=hv.render() to get the Bokeh figure object back, but doing something like p.yaxis.major_label_text_align = 'left' does not seem to do anything even if I inject newline \n characters into the long string label.

hvpandas.plot

reckoner
  • 2,861
  • 3
  • 33
  • 43
  • Does this answer your question? [Inserting newlines in bokeh Tick Labels](https://stackoverflow.com/questions/52219125/inserting-newlines-in-bokeh-tick-labels) – mosc9575 Oct 15 '21 at 09:01

1 Answers1

0

Multiline labels are available with the newline charactert \n for categorical factors.

I was not able to reproduce your example, but I think the solution is to set you y-axis to a FactorRange and set the factors with a list of your wanted strings, which can include \n.

See the example below, which is adapted from here.

from bokeh.io import output_file, show
from bokeh.models import ColumnDataSource
from bokeh.palettes import GnBu3, OrRd3
from bokeh.plotting import figure

output_file("stacked_split.html")

fruits = [f'{item}\n{item}' for item in ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']]
years = ["2015", "2016", "2017"]

exports = {'fruits' : fruits,
           '2015'   : [2, 1, 4, 3, 2, 4],
           '2016'   : [5, 3, 4, 2, 4, 6],
           '2017'   : [3, 2, 4, 4, 5, 3]}
imports = {'fruits' : fruits,
           '2015'   : [-1, 0, -1, -3, -2, -1],
           '2016'   : [-2, -1, -3, -1, -2, -2],
           '2017'   : [-1, -2, -1, 0, -2, -2]}

p = figure(y_range=fruits, height=250, x_range=(-16, 16), title="Fruit import/export, by year",
           toolbar_location=None)

p.hbar_stack(years, y='fruits', height=0.9, color=GnBu3, source=ColumnDataSource(exports),
             legend_label=["%s exports" % x for x in years])

p.hbar_stack(years, y='fruits', height=0.9, color=OrRd3, source=ColumnDataSource(imports),
             legend_label=["%s imports" % x for x in years])

p.y_range.range_padding = 0.1
p.ygrid.grid_line_color = None
p.legend.location = "top_left"
p.axis.minor_tick_line_color = None
p.outline_line_color = None

show(p)

Output

Multiline stack

mosc9575
  • 5,618
  • 2
  • 9
  • 32
  • This would work except when you don't know the text that is eventually going to wind up in the labels. Even if you use `textwrap`, it won't help with the alignment issue. – reckoner Oct 18 '21 at 19:44
  • You can write a function, which loops over all labels, counts the letters and after `n` letters you could replace the firts whitespace ` ` with `\n`. This could also work, if you don't know the label beforhand. – mosc9575 Oct 19 '21 at 07:13
  • There're string methods `rjust` and `ljust` that you can use but this approach will only work with monospaced fonts. I tried. – reckoner Oct 22 '21 at 00:43
  • I can help you, if you want, but now you are talking about string methods, which aren't anymore related to bokeh. Please open a new question and make a [mininimal workin example](https://stackoverflow.com/help/minimal-reproducible-example), show what you have, what you want and what you have tried. If you want to manipulate strings, the are powerful options, like [regular expressions](https://docs.python.org/3/library/re.html). – mosc9575 Oct 22 '21 at 06:54