首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用bokeh2.x中的annual_wedge在饼图/圆环图中添加标签

使用bokeh2.x中的annual_wedge在饼图/圆环图中添加标签
EN

Stack Overflow用户
提问于 2020-04-25 20:51:27
回答 2查看 334关注 0票数 0

我在将正确对齐的标签集应用于使用bokeh中的annual_wedge字形创建的甜甜圈样式图表时遇到问题。

我很难对齐属于圆环图“水平”中“环”类别的标签,其中水平是指标签从annual_wedgeinner_radiusouter_radius的对齐。目前,标签集似乎没有对应于annual_wedgeinner_radius设置,这使得将标签集应用和对齐到年度楔形变得非常困难。

我遵循了Adding labels in pie chart wedge in bokeh中使用字符串填充的示例,但这似乎是一个非常肮脏的水平移动标签的技巧。

如何以与annual_wedge字形的inner_radius对齐的方式将标签应用于annual_wedge?

下面是我的代码示例:

代码语言:javascript
复制
# > gics_sector_data
gics_sector_data["gics_name"] = gics_sector_data["gics_name"].astype(str)
gics_sector_data["gics_name"] = gics_sector_data["gics_name"].str.pad(47, side = "left")

    # Sector Ring
    p.annular_wedge(x=9, y=9, inner_radius=0.8,outer_radius=2.5,
            start_angle=cumsum('angle', include_zero=True), end_angle=cumsum('angle'),
            line_color="white", fill_color='color', source=gics_sector_data)

    sourceSector = ColumnDataSource(gics_sector_data)
    labelsSector = LabelSet(x=9, y=9, text='gics_name',
                        angle=cumsum('angle', include_zero=True), source=sourceSector, render_mode='canvas',
                        text_font_size="8pt", text_align='left',background_fill_color='white')
    p.add_layout(labelsSector)

在图像中,我将标签的背景颜色设置为白色,以便可以更好地看到标签的对准和间距。

]

仅供参考:这是要显示的数据Global Industry Classification Standard

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-04-25 23:47:51

你必须自己计算坐标。

在链接的示例上展开:

代码语言:javascript
复制
from math import pi

import numpy as np
import pandas as pd

from bokeh.io import show
from bokeh.models import LabelSet, ColumnDataSource
from bokeh.palettes import Category20c
from bokeh.plotting import figure

x = {'United States': 157,
     'United Kingdom': 93,
     'Japan': 89,
     'China': 63,
     'Germany': 44,
     'India': 42,
     'Italy': 40,
     'Australia': 35,
     'Brazil': 32,
     'France': 31,
     'Taiwan': 31,
     'Spain': 29}

R = 0.4
data = (pd.Series(x)
        .reset_index(name='value')
        .rename(columns={'index': 'country'})
        .assign(end_angle=lambda d: np.cumsum(d['value'] / d['value'].sum() * 2 * pi),
                start_angle=lambda d: np.pad(d['end_angle'], (1, 0))[:-1],
                color=Category20c[len(x)],
                label_x=lambda d: R * 0.95 * np.cos(d['start_angle']),
                label_y=lambda d: R * 0.95 * np.sin(d['start_angle'])))

source = ColumnDataSource(data)

p_range = (-R * 1.2, R * 1.2)
p_size = 500
p = figure(plot_height=p_size, plot_width=p_size, toolbar_location=None,
           x_range=p_range, y_range=p_range,
           tools="hover", tooltips="@country: @value")
for r in [p.xaxis, p.yaxis, p.grid]:
    r.visible = False

p.wedge(x=0, y=0, radius=R,
        start_angle='start_angle', end_angle='end_angle',
        line_color="white", fill_color='color', legend_label='country', source=source)

p.add_layout(LabelSet(x='label_x', y='label_y', text='value', text_align='right',
                      angle='start_angle', source=source, render_mode='canvas'))

show(p)

票数 0
EN

Stack Overflow用户

发布于 2020-04-26 04:54:10

多亏了尤金,我被引导到了启蒙的道路上,并且能够如愿以偿地得到图表。我不得不通过计算“甜甜圈”扇区的起始角和结束角之间的中间来修改标签位置的计算,因为我想让标签位于每个楔子的中间。

我将Eugene示例修改为这样,其中" num“是我用来计算每个值的相对宽度的值(在他的示例中,我使用num表示”annual_wedge“上所有相似宽度的槽中每个楔形的占用槽数,我希望这足够清楚):

代码语言:javascript
复制
R = 3
gics_sector_data= gics_sector_data.assign(
                end_angle=lambda d: np.cumsum(d['num'] / d['num'].sum() * 2 * pi),
                start_angle=lambda d: np.pad(d['end_angle'], (1, 0))[:-1],
                label_x=lambda d: R* 1.00 * np.cos(((d['end_angle']-d['start_angle'])/2)+d['start_angle']),
                label_y=lambda d: R* 1.00 * np.sin(((d['end_angle']-d['start_angle'])/2)+d['start_angle']),
                label_angle=lambda d: (((d['end_angle']-d['start_angle'])/2)+d['start_angle']))

关于1.00的乘法,你可以通过f.e.将标签从楔形的外边缘移开。使用0.95。

另外:我通过添加label_shortname列缩短了标签名称,因为bokeh不能对标签进行换行。短名称将用于标签,而长名称用于工具提示:

代码语言:javascript
复制
gics_sector_data['gics_name_short'] = gics_sector_data['gics_name'].str.slice(stop=20)

然后使用start_angles和end_anglescalculated绘制annual_wedge:

代码语言:javascript
复制
# Sector Ring
p.annular_wedge(x=0, y=0, inner_radius=gics_sector_radius-ringWidth, outer_radius=gics_sector_radius,
                start_angle='start_angle', end_angle='end_angle',
                line_color="white", fill_color='color', source=gics_sector_data)

最后绘制LabelSet,计算出label_x,label_y和label_angle,这将在楔形的中间绘制标签,text_baseline=‘中间’是强制性的,以获得良好的结果:

代码语言:javascript
复制
sourceSector = ColumnDataSource(gics_sector_data)
labelsSector = LabelSet(x='label_x', y='label_y', text='gics_name_short',
                   angle='label_angle', source=sourceSector, render_mode='canvas',
                    text_font_size="7pt", text_align='right', text_baseline='middle')
p.add_layout(labelsSector)

这里是99.999%完美的结果GICS result bokeh chart,我希望这将有助于创建类似的图表

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61425921

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档