是否可以在bokeh图表中具有多个图例颜色文本?

时间:2019-01-25 15:37:55

标签: python python-3.x bokeh

我正在使用titanic数据集处理bokeh中的简单条形图。我发现该框架非常灵活,但是我要控制的最后一个图表元素是:图例文本的各个颜色。

在此处查看参考文档-> http://bokeh.pydata.org/en/latest/docs/user_guide/styling.html#legends

我可以看到如何调整图例文本的颜色(整体),但是我希望能够将“ First”的颜色与我的列颜色(即#3a6587)相匹配,并且“第二”和“第三”作为颜色#aeb3b7

我在下面添加了示例代码:

from bokeh.io import show, output_file
from bokeh.models import ColumnDataSource
from bokeh.palettes import Spectral6
from bokeh.plotting import figure
from bokeh.transform import factor_cmap

output_file("bar_colormapped.html")

ticket = ['First', 'Second', 'Third']
counts = [84.15, 20.66, 13.68]


# .var width parameter controls the width of the columns
# We can add the colours to the barchart as part of a palette list.
# Note the width to height ration should be 1.618:1 ish ;-)

source = ColumnDataSource(data=dict(ticket=ticket, counts=counts))

p = figure(x_range=ticket, plot_height=480, plot_width= 647, toolbar_location=None, 
    title="Average Titanic Fare, by Class")
p.vbar(x='ticket', top='counts', width=0.7, source=source, legend="ticket",
       line_color='white', fill_color=factor_cmap('ticket', 
        palette=['#3a6587', '#aeb3b7', '#aeb3b7'], factors=ticket))


# Removes the chart gridlines (i.e.. removes the chart clutter)
p.xgrid.grid_line_color = None
p.ygrid.grid_line_color = None

# change just some things about the x-axes
p.xaxis.axis_label = "Class Type"
p.xaxis.axis_line_width = 2
p.yaxis.major_label_text_color = "#aeb3b7"
p.xaxis.axis_line_color = "#aeb3b7"


# change just some things about the y-axes
p.yaxis.axis_label = "Average Fare Price (in Pounds)"
p.yaxis.axis_line_width = 2
p.yaxis.major_label_text_color = "#aeb3b7"
p.yaxis.axis_line_color = "#aeb3b7"
p.yaxis.major_label_orientation = "vertical"


# Set the range of the chart
p.y_range.start = 0
p.y_range.end = 90


# Remove the border. Set the width to 0 does not work so we need 
# to set to 0.1 to make it less visible.
p.outline_line_width = 0.1

# Set attributes for the chart title
p.title.text_color = "black"
#p.title.text_font = "times"
#p.title.text_font_style = "italic"
p.title.align = "center"


# Set the position and orientation of the legend and remove 
# the legend border
p.legend.orientation = "vertical"
p.legend.location = "top_right"
p.legend.border_line_width = 0.1


show(p)

建议的图像模型如下:

Propsed Legend Text

1 个答案:

答案 0 :(得分:1)

从Bokeh 1.0.4开始,图例仅在所有条目中支持单一文本颜色。从来没有人问过这个问题,但是在GitHub上提出关于它的功能请求是合理的(尤其是您是否有兴趣帮助做出贡献)。