简体   繁体   English

更改散景中双轴标签文本的颜色

[英]Changing the Color of Twin Axis Label Text in Bokeh

On a standard Bokeh line graph, the color of y-axis label text can be set with the code:在标准 Bokeh 折线图上,可以使用以下代码设置 y 轴标签文本的颜色:

graph.yaxis.major_label_text_color = "#1F77B4"

It is possible to add a second y-axis to the graph, yielding twin axes.可以在图形中添加第二个 y 轴,产生双轴。 The following code would achieve this:以下代码将实现这一点:

graph.extra_y_ranges = {"range2": bokeh.models.Range1d(start = 0, end = 500)}
graph.add_layout(bokeh.models.LinearAxis(y_range_name = "range2"), "left")

However, it's not clear how to change the color of the label text for this second y-axis.但是,尚不清楚如何更改第二个 y 轴的标签文本颜色。 The first code block doesn't specify which y-axis, but it affects the original one.第一个代码块没有指定哪个 y 轴,但它会影响原始的。 It would be nice if the color of each set of labels corresponded to the lines they measured.如果每组标签的颜色与它们测量的线条相对应,那就太好了。 How can the color of the new y-axis be changed?如何更改新 y 轴的颜色?

The attributes such as p.xaxis and p.yaxis are actually lists: p.xaxisp.yaxis等属性实际上是列表:

In [41]: p.add_layout(LinearAxis(y_range_name="foo"), 'left')

In [42]: p.yaxis
Out[42]:
[LinearAxis(id='c9d9c010-3698-4906-83b0-e8a9a244e4be', ...),
 LinearAxis(id='c991b6b3-e85a-4033-b028-4e2ee134df1c', ...)]

However, because the vastly more common case is to have a single axis, it was made possible to do this:然而,因为更常见的情况是只有一个轴,所以可以这样做:

p.yaxis.major_label_text_color = "red"

as a convenience.作为一种方便。 This will set the property value for all y axes present.这将为所有存在的 y 轴设置属性值。 But of you just want to change one, instead of all of them, you can always be explicit by indexing:但是如果你只想改变一个,而不是所有的,你总是可以通过索引来明确:

p.yaxis[1].major_label_text_color = "red"  

For reference, this is all documented in the User's Guide chapter Styling Visual Attributes作为参考,这都记录在用户指南章节样式视觉属性中

And as a reminder the styling for any additional axis needs to be applied after the p.add_layout(LinearAxis()) creates the object.作为提醒,需要在p.add_layout(LinearAxis())创建对象后应用任何附加轴的样式。

graph.add_layout( bokeh.models.LinearAxis(y_range_name='range2', axis_label_text_color='green'), 'left' )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM