Matplotlib - 数学表达式



一般来说,数学表达式是在给定的数学环境中遵循特定规则的符号组合。这些符号可以表示数字、变量、运算、函数等等。数学表达式的结构遵循确定运算顺序和其他逻辑语法方面的规则。

以下是一些数学表达式的例子:

mathematical_expressions_Intro 1

Matplotlib中的数学表达式

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

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

mathematical_expressions_Intro

Matplotlib中的Mathtext

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

Mathtext的关键特性

Mathtext支持各种特性,例如:

  • 符号

  • 特殊字符

  • 下标、上标和标准函数名

  • 分数和二项式

  • 根式

  • 不同的字体等等

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

基本的数学文本

基本的数学文本可以包含像‘+’、‘-’、‘*’……这样的二元运算符,来表示两个元素之间的各种数学运算。

示例

此示例演示了包含像+,−这样的二元运算符,分别表示加法和减法。

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

根式、希腊字母和定界符

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

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

希腊字母,例如α(alpha)、β(beta)、γ(gamma)等等,是用于表示标准数学符号的符号。

定界符,包括括号、方括号和花括号,用于对表达式进行分组,同时保持数学语句中正确的运算顺序。

示例

这是一个包含带根式、希腊字母和定界符的数学表达式的例子。

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

分数、二项式和叠加数字

分数是两个数字的比率,写成一个数字在另一个数字之上,表示一个量除以另一个量。

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

叠加数字是指数值的垂直对齐,通常出现在数学符号中,例如指数或嵌套表达式。

示例

这是一个包含带分数、二项式和叠加数字的数学表达式的例子。

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

下标、上标和标准函数名

在数学符号中,下标和上标分别用于表示索引或指数。而标准函数名是常用的数学函数,例如正弦、余弦、对数和求和,通常用特定的符号或缩写表示。

示例

这是一个包含带下标、上标和标准函数名的数学表达式的例子。

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

示例

这是另一个在坐标轴标签和图例中编写下标文本的示例。

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
广告
© . All rights reserved.