数学符号#

Bokeh 支持使用 LaTeXMathML 标记语言表达的数学符号,并且支持的元素数量正在不断增加。目前,您可以在以下元素中使用 LaTeX 和 MathML 符号:

Bokeh 使用 MathJax 库来处理 LaTeX 和 MathML。有关 MathJax 的更多信息,请参阅官方的 MathJax 文档

注意

如果你使用components()函数,请确保在你的html模板中包含bokeh-mathjax-资源。

LaTeX#

要使用LaTeX表示法,您可以直接将字符串传递给任何支持的元素。 这个字符串需要以MathJax默认分隔符之一开始和结束。这些分隔符是$$...$$\[...\]\(...\)。例如:r"$$\sin(x)$$"

LaTeX on axis labels, titles, and labels

要将LaTeX符号用作轴标签标题标签,请传递一个以MathJax默认分隔符开头和结尾并包含LaTeX符号的原始字符串字面量。例如:

from numpy import arange, pi, sin

from bokeh.models.annotations.labels import Label
from bokeh.plotting import figure, show

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

p = figure(height=250, title=r"$$\sin(x)$$ for \[x\] between \(-2\pi\) and $$2\pi$$")
p.scatter(x, y, alpha=0.6, size=7)

label = Label(
    text=r"$$y = \sin(x)$$",
    x=150, y=130,
    x_units="screen", y_units="screen",
)
p.add_layout(label)

p.yaxis.axis_label = r"\(\sin(x)\)"
p.xaxis.axis_label = r"\[x\pi\]"

show(p)
LaTeX and tick labels

要在刻度标签中添加LaTeX符号,请使用带有轴的major_label_overrides()函数。

此函数用于用自定义文本替换现有刻度标签的值。它接受一个字典,其中刻度标签的原始值作为键,您的自定义值作为字典的值。

使用此函数将任何纯文本刻度标签替换为LaTeX表示法:

from numpy import arange

from bokeh.plotting import figure, show

x = arange(1, 4.5, 0.25)
y = 1 / x

plot = figure(height=200)
plot.title = "Current over Resistance at a static voltage of 1 volt"
plot.scatter(x, y, fill_color="blue", size=5)
plot.line(x, y, color="darkgrey")

plot.xaxis.axis_label = "Resistance"
plot.xaxis.ticker = [1, 2, 3, 4]
plot.xaxis.major_label_overrides = {
    1: r"1 $$\Omega$$",
    2: r"2 $$\Omega$$",
    3: r"3 $$\Omega$$",
    4: r"4 $$\Omega$$",
}

plot.yaxis.axis_label = "Current"
plot.yaxis.ticker = [0.2, 0.4, 0.6, 0.8, 1.0]
plot.yaxis.major_label_overrides = {
    0.2: "0.2 $$A$$",
    0.4: "0.4 $$A$$",
    0.6: "0.6 $$A$$",
    0.8: "0.8 $$A$$",
    1: "1 $$A$$",
}
show(plot)
LaTeX on RangeSlider and Slider widget titles

要在RangeSliderSlider小部件的标题中使用LaTeX表示法,请传递一个以MathJax默认分隔符开头和结尾的原始字符串字面量,并包含LaTeX表示法作为title参数。例如:

from bokeh.io import show
from bokeh.models import Slider

slider = Slider(start=0, end=10, value=1, step=.1, title=r"$$\delta \text{ (damping factor, 1/s)}$$")

show(slider)
LaTeX with div and paragraph widgets

要在div widgetparagraph widget的文本中包含LaTeX符号,可以在字符串中的任何位置使用标准的MathJax默认分隔符

from bokeh.io import show
from bokeh.models import Div

div = Div(
    width=400, height=100, background="#fafafa",
    text=r"The Pythagorean identity is $$\sin^2(x) + \cos^2(x) = 1$$",
)

show(div)

要禁用 div 或段落小部件的 LaTeX 渲染,请将小部件的 disable_math 属性设置为 True。

你可以使用一些Bokeh的标准文本属性来改变渲染的数学文本的外观。使用text_font_size来改变字体大小,使用text_color来改变颜色。例如:

p.xaxis.axis_label = r"$$\nu \:(10^{15} s^{-1})$$"
p.xaxis.axis_label_text_color = "green"
p.xaxis.axis_label_text_font_size = "50px"

Bokeh主题中定义的文本颜色和大小也适用。

此外,您可以选择使用MathJax中包含的LaTeX扩展。 例如,使用\text{}将文字与数学表达式结合。或者 使用颜色扩展来更改渲染的LaTeX符号的颜色: \color{white} \sin(x)。使用LaTeX扩展设置的文本属性会覆盖 代码中或主题中其他地方设置的任何文本属性。

注意

LaTeX MathJax 支持的功能有一定的限制。详情请参阅 MathJax 文档中的 与真实 TeX 的差异

MathML#

要添加用MathML编写的数学符号,请直接使用Bokeh的 MathML 模型。该模型有一个 text 属性,该属性接受包含MathML的字符串。例如:

from numpy import arange

from bokeh.models import MathML
from bokeh.plotting import figure, show

x = arange(-10, 10, 0.1)
y = (x * 0.5) ** 2

mathml = """
<math>
  <mrow>
    <mfrac>
      <mn>1</mn>
      <mn>4</mn>
    </mfrac>
    <msup>
      <mi>x</mi>
      <mn>2</mn>
    </msup>
  </mrow>
</math>
"""

plot = figure(height=200)
plot.line(x, y)

plot.xaxis.axis_label = MathML(text=mathml)

show(plot)

类似于LaTeX,你也可以使用Bokeh的标准文本属性 text_font_sizetext_color 来改变MathML 符号的字体大小和颜色。例如:

plot.xaxis.axis_label = MathML(text=mathml)
plot.xaxis.axis_label_text_color = "green"
plot.xaxis.axis_label_text_font_size = "50px"

欲了解更多信息,请参阅MathML参考指南中。