【问题标题】:Bokeh stack chart legend incorrect散景堆栈图图例不正确
【发布时间】:2019-03-27 13:56:48
【问题描述】:

我有一个数据框

df = pd.DataFrame({'Category': ['<£5000', '£100K to £250K'],
               '01/01/2014': [8,1],
               '01/01/2015': [8,2],
               '01/01/2016': [7,1]})

我正在 Bokeh 中创建堆积图。创建图表很好,但图例不正确。

grouped = df.groupby('Category')['01/01/2014', '01/01/2015', '01/01/2016'].mean().round(0)

source = ColumnDataSource(grouped)
countries = source.data['Category'].tolist()
p = figure(x_range=countries)

p.vbar_stack(stackers=['01/01/2014', '01/01/2015', '01/01/2016'],
         x='Category', source=source,
         legend = ['01/01/2014', '01/01/2015', '01/01/2016'],
         width=0.5, color=Spectral3)


p.title.text ='Average Number of Trades by Portfolio Size'
p.legend.location = 'top_right'

p.xaxis.axis_label = 'Portfolio Size'
p.xgrid.grid_line_color = None  #remove the x grid lines

p.yaxis.axis_label = 'Average Number of Trades'

show(p)

我认为在下面的行中,我已将传奇设置为年份。然而,就像在图像中一样,它在图中设置为三个点。

legend = ['01/01/2014', '01/01/2015', '01/01/2016']

【问题讨论】:

    标签: python bokeh stacked-chart


    【解决方案1】:

    如果您在图例列表中添加其他内容,它会显示正确的图例名称,只要它与您的 ColumnDataSource 中的日期不匹配。因此,解决您的问题的一个简单方法是在图例列表中的每个日期后面添加一个空格。

    import pandas as pd
    from bokeh.models import ColumnDataSource
    from bokeh.plotting import figure, show
    from bokeh.palettes import Spectral3
    
    df = pd.DataFrame({'Category': ['<£5000', '£100K to £250K'],
                   '01/01/2014': [8,1],
                   '01/01/2015': [8,2],
                   '01/01/2016': [7,1]})
    
    
    grouped = df.groupby('Category')['01/01/2014', '01/01/2015', '01/01/2016'].mean().round(0)
    
    source = ColumnDataSource(grouped)
    countries = source.data['Category'].tolist()
    p = figure(x_range=countries)
    
    p.vbar_stack(stackers=['01/01/2014', '01/01/2015', '01/01/2016'],
             x='Category', source=source,
             legend = ['01/01/2014 ', '01/01/2015 ', '01/01/2016 '],
             width=0.5, color=Spectral3)
    
    
    p.title.text ='Average Number of Trades by Portfolio Size'
    p.legend.location = 'top_right'
    
    p.xaxis.axis_label = 'Portfolio Size'
    p.xgrid.grid_line_color = None  #remove the x grid lines
    
    p.yaxis.axis_label = 'Average Number of Trades'
    
    show(p)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-22
      • 1970-01-01
      相关资源
      最近更新 更多