добавьте метки данных для графиков уклонения (сгруппированных) - PullRequest
0 голосов
/ 07 мая 2019

Я новичок в Боке и Питоне.Я наконец создал сгруппированную гистограмму, но не смог добавить метку данных на соответствующую гистограмму.Как показано на скриншоте ниже, все 3 метки данных расположены на одной панели.

Не могли бы вы помочь мне добавить метку данных к каждой из полос?Спасибо.

from bokeh.core.properties import value
from bokeh.io import show, output_file
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
from bokeh.transform import dodge

output_file("dodged_bars.html")

fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
years = ['2015', '2016', '2017']

data = {'fruits' : fruits,
        '2015'   : [2, 1, 4, 3, 2, 4],
        '2016'   : [5, 3, 3, 2, 4, 6],
        '2017'   : [3, 2, 4, 4, 5, 3]}

source = ColumnDataSource(data=data)

p = figure(x_range=fruits, y_range=(0, 10), plot_height=250, title="Fruit Counts by Year",
           toolbar_location=None, tools="")

labels15=LabelSet(x='fruits',y='2015',text='2015',source=source)
labels16=LabelSet(x='fruits',y='2016',text='2016',source=source)
labels17=LabelSet(x='fruits',y='2017',text='2017',source=source)

p.vbar(x=dodge('fruits', -0.25, range=p.x_range), top='2015', width=0.2, source=source,
       color="#c9d9d3", legend=value("2015"))

p.vbar(x=dodge('fruits',  0.0,  range=p.x_range), top='2016', width=0.2, source=source,
       color="#718dbf", legend=value("2016"))

p.vbar(x=dodge('fruits',  0.25, range=p.x_range), top='2017', width=0.2, source=source,
       color="#e84d60", legend=value("2017"))

p.x_range.range_padding = 0.1
p.xgrid.grid_line_color = None
p.legend.location = "top_left"
p.legend.orientation = "horizontal"

p.add_layout(labels15)
p.add_layout(labels16)
p.add_layout(labels17)



show(p)

enter image description here

1 Ответ

1 голос
/ 07 мая 2019

Вы можете использовать тот же метод, что и для vbar.

from bokeh.core.properties import value
from bokeh.io import show, output_file
from bokeh.models import ColumnDataSource, LabelSet
from bokeh.plotting import figure
from bokeh.transform import dodge

output_file("dodged_bars.html")

fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
years = ['2015', '2016', '2017']
data = {'fruits' : fruits,
        '2015'   : [2, 1, 4, 3, 2, 4],
        '2016'   : [5, 3, 3, 2, 4, 6],
        '2017'   : [3, 2, 4, 4, 5, 3]}

source = ColumnDataSource(data=data)

p = figure(x_range=fruits, y_range=(0, 10), plot_height=250, title="Fruit Counts by Year",
           toolbar_location=None, tools="")

labels15=LabelSet(x=dodge('fruits', -0.25, range=p.x_range),y='2015',text='2015',source=source,text_align='center')
labels16=LabelSet(x=dodge('fruits', 0.0, range=p.x_range),y='2016',text='2016',source=source,text_align='center')
labels17=LabelSet(x=dodge('fruits', 0.25, range=p.x_range),y='2017',text='2017',source=source,text_align='center')

vbar1 = p.vbar(x=dodge('fruits', -0.25, range=p.x_range), top='2015', width=0.2, source=source,
       color="#c9d9d3", legend=value("2015"))

p.vbar(x=dodge('fruits',  0.0,  range=p.x_range), top='2016', width=0.2, source=source,
       color="#718dbf", legend=value("2016"))

p.vbar(x=dodge('fruits',  0.25, range=p.x_range), top='2017', width=0.2, source=source,
       color="#e84d60", legend=value("2017"))

p.x_range.range_padding = 0.1
p.xgrid.grid_line_color = None
p.legend.location = "top_left"
p.legend.orientation = "horizontal"

p.add_layout(labels15)
p.add_layout(labels16)
p.add_layout(labels17)

show(p)

enter image description here

...