散景图表图例不正确

问题描述 投票:0回答:1

我有一个数据帧

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']

enter image description here

python bokeh stacked-chart
1个回答
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)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.