散景:如何给标签加上不同的颜色

时间:2018-08-15 03:35:36

标签: 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)

我可以将所有 y 刻度标签设置为一种颜色,但不能设置为其他颜色。作为一种解决方法,我尝试使用5个 y 轴,每个轴都带有一个标签,因此我可以分别设置它们的颜色。但是似乎我无法控制它们的位置以使其重叠。

我想为 y 刻度标签实现类似的功能。

enter image description here

有什么建议吗?谢谢

0 个答案:

没有答案