简体   繁体   English

如何使用我的字符串标签添加辅助 x 轴

[英]how to add secondary x-axis with my String labels

I have plot我有 plot 在此处输入图像描述

with secondary axis added like this:像这样添加辅助轴:

    plot.extra_x_ranges['sec_x_axis'] = Range1d(0, 100)
    ax2 = LinearAxis(x_range_name="sec_x_axis", axis_label="secondary x-axis")
    plot.add_layout(ax2, 'above')

x_axis is x_axis_type='datetime', so bokeh show ms on second x-axis too. x_axis 是 x_axis_type='datetime',因此散景在第二个 x 轴上也显示 ms。 This is not good.情况不妙。
Is there a way I can put my labels on this axis?有没有办法可以把我的标签放在这个轴上? I have a list of str labels like:我有一个 str 标签列表,例如:

my_labels = ['21.5; 315.1', '21.6; 315.0', '21.7; 315.0', '21.7; 314.9',.....]

I found FuncTickFormatter but it takes JS code inside, so I can't handle it.我找到了 FuncTickFormatter,但它需要 JS 代码,所以我无法处理它。 Maybe there is another way to do this?也许还有另一种方法可以做到这一点?

To override the values of the labels use major_label_overrides on the appropriate axis.要覆盖标签的值,请在适当的轴上使用major_label_overrides You can pass a dictionary like {1:'A',...} , where 1 is the place to overwrite and A is the new label.您可以传递像{1:'A',...}这样的字典,其中1是要覆盖的位置, A是新的 label。 To avoid "wrong" labels while zooming, you can set the ticker direcetlly as list unsing ticker .为避免缩放时出现“错误”标签,您可以将代码直接设置为 list unsing ticker In your case the axis is p.above[0] .在您的情况下,轴是p.above[0]

Comment评论

If you add a LinearAxis to a figure with an already existing DatetimeAxis, the new axis shoudn't be effected and therefor shouldn't be formatted as datetime.如果将 LinearAxis 添加到具有已存在 DatetimeAxis 的图形中,则不应影响新轴,因此不应将其格式化为日期时间。 I used the latest version 2.4.3 and it works as expected.我使用了最新版本 2.4.3,它按预期工作。 Use the minimal example to try it on your own.使用最小示例自行尝试。

Minimal Example最小的例子

This code is based on the twin_axis.py example published by the authors of bokeh.此代码基于散景作者发布的twin_axis.py示例。

from numpy import arange, linspace, pi, sin

from bokeh.models import LinearAxis, Range1d
from bokeh.plotting import figure, show, output_notebook
output_notebook()

x = arange(-2*pi, 2*pi, 0.2)
x2 = arange(-pi, pi, 0.1)
y = sin(x)
y2 = sin(x2)

p = figure(
    width=400,
    height=400,
    x_range=(-6.5, 6.5),
    y_range=(-1.1, 1.1),
    min_border=80,
    x_axis_type="datetime"
)

p.circle(x, y, color="crimson", size=8)
p.yaxis.axis_label = "red circles"
p.yaxis.axis_label_text_color ="crimson"

p.extra_x_ranges['foo'] = Range1d(-pi, pi)
p.circle(x2, y2, color="navy", size=8, x_range_name="foo")
ax2 = LinearAxis(x_range_name="foo", axis_label="blue circles")
ax2.axis_label_text_color ="navy"
p.add_layout(ax2, 'above')

# set ticker to avoid wrong formatted labels while zooming
p.above[0].ticker = list(range(-3,4))
# overwrite labels
p.above[0].major_label_overrides = {key: item for key, item in zip(range(-3,4), list('ABCDEFG'))}

show(p)
default默认 overwritten labels覆盖标签
窦性随着时间的推移默认 随着时间的推移鼻窦

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

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