【问题标题】:Bokeh Stacked Bar- adding text label next to the barBokeh Stacked Bar - 在栏旁边添加文本标签
【发布时间】:2017-11-27 14:03:50
【问题描述】:

我正在尝试使用 Bokeh 0.12.4 创建堆叠条。我可以使用图表界面创建条形图。但是,我无法在栏旁边添加标签。

更具体地说:

  1. 有没有一种方法可以使用 bar() 向每个条形添加标签?我知道图表界面有局限性。如果没有,我如何使用绘图界面创建堆积条形图并在每个条形旁边创建标签?(我不希望标签在条形上,如您所见,如果打开,某些部分真的很小条,所有文本将聚集在一起。)

  2. 如何将图例移出条形图区域?因为它目前隐藏了一些栏。

  3. 对于工具提示,我应该使用什么来显示值?现在我正在使用“y”,但值不正确。

    df = pd.DataFrame(tb, columns=['Files Due', 'Files Past Due', 'Files WIP', 'No Errors', 'Errors Explained', 'Files Failed - Pending Resolution', 'date'])
    bar = Bar(df, values=blend('Files Due', 'Files Past Due', 'Files WIP', 'No Errors', 'Errors Explained', 'Files Failed - Pending Resolution', name='Number of Files', labels_name='KPI'),
          label=cat(columns='date', sort=False),
          stack=cat(columns='KPI', sort=False),
          color=color(columns='KPI', palette=['#BE4248', '#21374B', '#D7DADB', '#586473', '#E7DACB','#4A89AA'], sort=False),
          legend='top_right', tooltips=[('Status', '@KPI'),('Number of Files', '@y')], bar_width=0.2, ylabel='KPI')
    js5, div5 = components(bar)
    

Bokeh stacked bar chart

【问题讨论】:

    标签: python bar-chart bokeh stacked-chart


    【解决方案1】:

    bokeh.charts API 已被弃用并删除,这是一个完全的死胡同。最新版本的 Bokeh 在稳定且受支持的bokeh.plotting API 中提供了更多更好的选项。在用户指南部分Handling Categorical Data 中有许多完整的带有图例和悬停工具提示的条形图示例,在堆叠和分组配置中。

    如果您由于某种原因无法更新到较新版本的 Bokeh,我会建议您寻找其他工具来使用,而不是尝试让 bokeh.charts 工作。

    如果您可以更新,这里是一个完整的堆积条形图示例,带有悬停工具和图例之外的图例,可与 Bokeh 0.13.0 一起使用:

    from bokeh.io import show
    from bokeh.models import Legend
    from bokeh.plotting import figure
    
    fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
    years = ["2015", "2016", "2017"]
    colors = ["#c9d9d3", "#718dbf", "#e84d60"]
    
    data = {'fruits' : fruits,
            '2015'   : [2, 1, 4, 3, 2, 4],
            '2016'   : [5, 3, 4, 2, 4, 6],
            '2017'   : [3, 2, 4, 4, 5, 3]}
    
    p = figure(x_range=fruits, plot_height=250, title="Fruit Counts by Year",
               toolbar_location=None, tools="hover", tooltips="$name @fruits: @$name")
    
    rs = p.vbar_stack(years, x='fruits', width=0.9, color=colors, source=data)
    
    p.y_range.start = 0
    p.x_range.range_padding = 0.1
    p.xgrid.grid_line_color = None
    p.axis.minor_tick_line_color = None
    p.outline_line_color = None
    
    legend = Legend(items=[(fruit, [r]) for (fruit, r) in zip(fruits, rs)], location=(0, 30))
    p.add_layout(legend, 'right')
    
    show(p)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-11
      • 2021-02-05
      • 2016-04-03
      • 2015-02-05
      • 2015-12-23
      • 1970-01-01
      • 1970-01-01
      • 2015-09-18
      相关资源
      最近更新 更多