Bokeh:如何为刻度标签赋予不同的颜色

Nir*_*han 7 python plot bokeh

我试图给每个y刻度标签不同的颜色。例如,红色为“非常差”,橙色为“差”,黄色为“一般”,绿色为“良好”,蓝色为“非常好”。有没有办法用 Bokeh 做到这一点?

下面是情节的代码。

from bokeh.plotting import output_file, show,figure
from bokeh.models.sources import ColumnDataSource
from bokeh.transform import linear_cmap
from bokeh.models import Range1d, FuncTickFormatter

import pandas as pd


output_file("gridbands.html")

#The data
df = pd.DataFrame([4.5, 9.32, 3.4, 7.1,1.4], columns = ['Score'])
df.index = pd.to_datetime(['2000-12-30 22:00:00','2001-12-30 22:00:00','2002-12-30 22:00:00','2003-12-30 22:00:00','2004-12-30 22:00:00'])
df.index.name = 'Date'
df.sort_index(inplace=True)
source = ColumnDataSource(df)

#Prepare the plot area
p = figure(x_axis_type="datetime", plot_width=800, plot_height=500)
p.y_range = Range1d(0, 10)

def custom_label():
    new_labels = ["Very Poor", "Poor", "Fair", "Good", "Very Good"]
    return new_labels[(tick-1)/2 ]

p.yaxis.ticker = [1, 3, 5,7,9]
p.yaxis.formatter = FuncTickFormatter.from_py_func(custom_label)

#Draw
mapper = linear_cmap(field_name='Score', palette=["red", "orange","yellow","green","blue"] ,low=0 ,high=10)
p.circle('Date', 'Score', source=source, line_width=5, line_color=mapper,color=mapper)
p.line('Date', 'Score', source=source, line_width=2, line_color="gray",color=mapper, line_alpha = 0.5)

show(p)
Run Code Online (Sandbox Code Playgroud)

我可以将所有y刻度标签设置为一种颜色,但不能设置为不同颜色。作为一种解决方法,我尝试使用 5 个y轴,每个轴都有一个标签,这样我就可以单独设置它们的颜色。但我似乎无法控制它们的位置使它们重叠。

我想为y刻度标签实现类似的效果。

在此输入图像描述

有什么建议么?谢谢