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,
                                                 })

这是相应的带注释的输出。我曾尝试使用p=hv.render()来取回 Bokehfigure对象,但p.yaxis.major_label_text_align = 'left'即使我将换行符\n注入长字符串标签,做类似的事情似乎也无济于事。

hvpandas.plot

4

1 回答 1

0

\n多行标签可用于分类因素的换行符。

我无法重现您的示例,但我认为解决方案是将您的 y 轴FactorRange设置为 a 并factors使用您想要的字符串列表进行设置,其中可以包括\n.

请参阅下面的示例,该示例改编自此处

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)

输出

多行堆栈

于 2021-10-15T09:09:48.113 回答