散景添加标签到水平堆积条形图

时间:2022-05-11 15:21:53

I have created a simple horizontal stacked bar chart from 0 to 100% which take only two data to know the balance between the two. I would like to be able to write the values of them inside the bar (in the middle of it if possible). But even if this example looks similar, I can't find a way to adapt it to my horizontal stacked bar chart.

我创建了一个简单的水平堆积条形图,从0到100%,只需要两个数据就可以知道两者之间的平衡。我希望能够在栏内写出它们的值(如果可能的话,在它的中间)。但即使这个例子看起来很相似,我找不到一种方法来使它适应我的水平堆积条形图。

So far, here is my code :

到目前为止,这是我的代码:

colors = ["blue", "red"]
choose = ["a", "b"]
y_axis = ['Percent']
# For example for 60.1% and 39.9%
data = {'y' : y_axis,
       'a'   : [60.1],
       'b'   : [39.9]}

p = figure(y_range=y_axis, plot_height=150, x_range=(0, 100), title="TITLE", toolbar_location=None)
p.hbar_stack(choose, y='y', height=0.6, color=colors, source=ColumnDataSource(data))
p.add_tools(HoverTool(tooltips=[("A", "@a{1.1}%"), ("B", "@b{1.1}%")]))

labels = LabelSet(x=choose, y=y_axis, text=choose, level='glyph',
        x_offset=-13.5, y_offset=0, source=ColumnDataSource(data), render_mode='canvas')
p.add_tools(labels) # Can we do that ?

p.ygrid.grid_line_color = None
p.axis.minor_tick_line_color = None
p.outline_line_color = None

Ideally I would like to have a result like this : 散景添加标签到水平堆积条形图

理想情况下,我希望得到这样的结果:

1 个解决方案

#1


1  

I used two Label instead of using one LabelSet.

我使用了两个Label而不是一个LabelSet。

left_label = Label( x=20,
                    y=0.35,
                    x_units='data',
                    y_units='data',
                    text="%s%%"%left_percent_var,
                    text_color="white",
                    text_font_size="20pt",
                    text_font_style="bold",
                    level='glyph',
                    render_mode='css'
                )
plot.add_layout(left_label)

#1


1  

I used two Label instead of using one LabelSet.

我使用了两个Label而不是一个LabelSet。

left_label = Label( x=20,
                    y=0.35,
                    x_units='data',
                    y_units='data',
                    text="%s%%"%left_percent_var,
                    text_color="white",
                    text_font_size="20pt",
                    text_font_style="bold",
                    level='glyph',
                    render_mode='css'
                )
plot.add_layout(left_label)