Matplotlib 简明教程

Matplotlib - Mathematical Expressions

通常, mathematical expressions 是符号的组合,这些符号在给定的数学上下文中遵循特定规则。这些符号可以表示数字、变量、运算、函数等等。数学表达式的结构遵循确定运算顺序和逻辑语法其他方面的规则。

以下是一些数学表达式的示例 −

mathematical expressions intro

Mathematical Expressions in Matplotlib

Matplotlib 允许用户在 text 元素(文本对象)中包含数学表达式,以增强绘图和图形中数学元素的可视化表示。

下图显示了在 matplotlib 绘图中包含数学表达式 −

mathematical expressions input

Mathtext in Matplotlib

Matplotlib 使用称为 Mathtext 的模块来在绘图中呈现数学表达式。这是一个轻量级的 TeX 表达式解析器和布局引擎,用于在 Matplotlib 中呈现数学表达式。

Key Features of Mathtext

Mathtext 支持各种特性,如:

  1. Symbols

  2. Special characters

  3. 下标、上标和标准函数名称

  4. Fractions and binomials

  5. Radicals

  6. Different fonts and more

Mathtext 表达式应包含在美元符号 ('$') 中,并且可以包含各种符号和命令。文本对象(如标题、标签、注释和图形文本)是 Matplotlib 中包含数学表达式的一般位置。

Basic Mathematical Text

基本的数学文本可以包括二元运算符符号,如“+”、“-”、“*”,来表示两个元素之间的各种数学运算。

Example

此示例展示了包含二元运算符符号,如“+”、“-”,以分别表示加法和减法。

import matplotlib.pyplot as plt

# Create a plot
fig = plt.figure(figsize=(7, 4))

# Displaying basic mathematical text
plt.text(.5, .5, r"$x^2 - 4x + 7$", fontsize=16, ha='center')

# Show the plot
plt.show()

执行上述代码,我们将得到以下输出 −

mathematical expressions ex1

Radical, Greek Letters, and Delimiters

根式、希腊字母和分隔符是数学表达式中的关键组成部分。

用方根符号(√)表示的 radical ,表示数字或表达式的根。

Greek letters ,如 α(α)、β(β)、γ(γ)等是用来表示标准数学符号的符号。

Delimiters ,包括括号、方括号和花括号,用于对表达式分组,同时在数学语句中维持正确的运算顺序。

Example

下面是一个包括根式、希腊字母和分隔符的数学表达式的示例。

import matplotlib.pyplot as plt

# Create a figure
fig = plt.figure(figsize=(7, 4))

# Add Greek Letters within the plots
plt.text(0.25, 0.2, r'Greek Letters: $\alpha, \beta, \gamma$', fontsize=16)

# Radicals
plt.text(0.3, 0.5, r'Radical: $\sqrt{2}$', fontsize=16)

# delimiters
plt.text(0.2, 0.8, r'delimiters: $(a + b) \left\{c - d\right\}$', fontsize=16)

# Show the plot
plt.show()

执行上述代码,我们将得到以下输出 −

mathematical expressions ex2

Fractions, Binomials, and Stacked Numbers

Fractions 是两个数字的比值,写成一个数字放在另一个数字上面,表示一个量除以另一个量。

Binomials 包含有两个项的表达式,通常通过加法或减法连接。在二项式系数的上下文中,二项式可以表示集合中的组合或选择。

Stacked 数字指数值的垂直对齐,通常在诸如指数或嵌套表达式之类的数学符号中可见。

Example

下面是一个包括分数、二项式和堆叠数字的数学表达式的示例。

import matplotlib.pyplot as plt

# Create a plot
fig = plt.figure(figsize=(7, 4))

# Fractions, binomials, and stacked numbers
plt.text(0.4, 0.7, r'$\frac{3}{4} \binom{3}{4} \genfrac{}{}{0}{}{3}{4}$', fontsize=16)
plt.text(0.4, 0.3, r'$\left(\frac{5 - \frac{1}{x}}{4}\right)$', fontsize=16)

plt.show()

以上示例生成以下输出 −

mathematical expressions ex3

Subscripts, Superscripts and Standard Function Names

在数学符号中, subscripts and superscripts 分别用于表示下标或上标。而 Standard function names 通常是数学函数,如正弦、余弦、对数和求和,通常用特定符号或缩写表示。

Example

下面是如何在子标、上标和标准函数名称中包含数学表达式的示例。

import matplotlib.pyplot as plt

# Create a figure
fig = plt.figure(figsize=(7, 4))

# Add mathmetical expression with Subscripts, Superscripts and Standard Function Names
plt.text(0.3, 0.6, r'$\sum_{i=0}^\infty x_i \quad \sin(\theta) \quad \log(e^x)$', fontsize=16)

# Subscripts and superscripts
plt.text(0.5, 0.3, r'$\log^a_i (x)$', fontsize=16)

# Show the plot
plt.show()

以下是上述示例的输出 −

mathematical expressions ex4

Example

下面是另一个用文字写成轴标签和图例中下标的示例。

import numpy as np
import matplotlib.pyplot as plt

# Adjust figure size and autolayout
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True

# Generate data
x = np.linspace(1, 10, 1000)
y = np.exp(x)

# Plot data
plt.plot(x, y, label=r'$e^x$', color="red", linewidth=2)

# Set axis labels
plt.xlabel("$X_{\mathrm{axis}}$")
plt.ylabel("$Y_{\mathrm{axis}}$")

# Set legend
plt.legend(loc='upper left')

# Display plot
plt.show()

执行上述代码时,您将获得以下输出 -

mathematical expressions ex5