Matplotlib 简明教程
Matplotlib - Mathematical Expressions
通常, mathematical expressions 是符号的组合,这些符号在给定的数学上下文中遵循特定规则。这些符号可以表示数字、变量、运算、函数等等。数学表达式的结构遵循确定运算顺序和逻辑语法其他方面的规则。
In general, mathematical expressions are combinations of symbols that obey specific rules within a given mathematical context. These symbols can represent numbers, variables, operations, functions, and more. The structure of mathematical expressions follows rules that determine the order of operations and other aspects of logical syntax.
以下是一些数学表达式的示例 −
Following are a few examples of mathematical expressions −
Mathematical Expressions in Matplotlib
Matplotlib 允许用户在 text 元素(文本对象)中包含数学表达式,以增强绘图和图形中数学元素的可视化表示。
Matplotlib allows users to include mathematical expressions in text elements(text object) to enhance the visual representation of mathematical elements in plots and figures.
下图显示了在 matplotlib 绘图中包含数学表达式 −
The following image shows the inclusion of mathematical expressions in matplotlib plot −
Mathtext in Matplotlib
Matplotlib 使用称为 Mathtext 的模块来在绘图中呈现数学表达式。这是一个轻量级的 TeX 表达式解析器和布局引擎,用于在 Matplotlib 中呈现数学表达式。
Matplotlib uses a module called Mathtext to render the math expressions in plots. It is a lightweight TeX expression parser and layout engine to render the math expressions in Matplotlib.
Key Features of Mathtext
Mathtext 支持各种特性,如:
Mathtext supports various features such as −
-
Symbols
-
Special characters
-
Subscripts, Superscripts, and Standard Function Names
-
Fractions and binomials
-
Radicals
-
Different fonts and more
Mathtext 表达式应包含在美元符号 ('$') 中,并且可以包含各种符号和命令。文本对象(如标题、标签、注释和图形文本)是 Matplotlib 中包含数学表达式的一般位置。
The Mathtext expressions should be enclosed in dollar signs ('$') and can include various symbols and commands. The text objects like titles, lables, annotations, and figure text are the common places in Matplotlib where mathematical expressions are included.
Basic Mathematical Text
基本的数学文本可以包括二元运算符符号,如“+”、“-”、“*”,来表示两个元素之间的各种数学运算。
Basic mathematical text can include binary operation symbols like ‘+’, ‘-’, ‘*’,… to represent various mathematical operations between two elements.
Example
此示例展示了包含二元运算符符号,如“+”、“-”,以分别表示加法和减法。
This example demonstrates the inclusion of binary operation symbols like +, − to denote addition, subtraction respectively.
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()
执行上述代码,我们将得到以下输出 −
On executing the above code we will get the following output −
Radical, Greek Letters, and Delimiters
根式、希腊字母和分隔符是数学表达式中的关键组成部分。
Radicals, Greek letters, and delimiters are key components in mathematical expressions.
用方根符号(√)表示的 radical ,表示数字或表达式的根。
A radical, denoted by the square root symbol(√), represents the root of a number or expression.
Greek letters ,如 α(α)、β(β)、γ(γ)等是用来表示标准数学符号的符号。
Greek letters, such as α(alpha), β(beta), γ(gamma), and more are the symbols used to represente the standard mathematical notation.
Delimiters ,包括括号、方括号和花括号,用于对表达式分组,同时在数学语句中维持正确的运算顺序。
Delimiters, including parentheses, brackets, and braces, are used to group the expressions, while mainting the the proper order of operations in mathematical statements.
Example
下面是一个包括根式、希腊字母和分隔符的数学表达式的示例。
Here is an example of including the mathmetical expression with radicals, greek latters and delimiters.
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()
执行上述代码,我们将得到以下输出 −
On executing the above code we will get the following output −
Fractions, Binomials, and Stacked Numbers
Fractions 是两个数字的比值,写成一个数字放在另一个数字上面,表示一个量除以另一个量。
Fractions are ratio of two numbers, written as one number over another, conveying the division of one quantity by another.
Binomials 包含有两个项的表达式,通常通过加法或减法连接。在二项式系数的上下文中,二项式可以表示集合中的组合或选择。
Binomials involve expressions with two terms, often connected by addition or subtraction. In the context of binomial coefficients, binomials can represent combinations or choices within a set.
Stacked 数字指数值的垂直对齐,通常在诸如指数或嵌套表达式之类的数学符号中可见。
Stacked numbers refer to the vertical alignment of numerical values, commonly seen in mathematical notations such as exponents or nested expressions.
Example
下面是一个包括分数、二项式和堆叠数字的数学表达式的示例。
Here is an example of including the mathmetical expreassion with Fractions, Binomials, and Stacked Numbers.
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()
以上示例生成以下输出 −
The above example produces the following output −
Subscripts, Superscripts and Standard Function Names
在数学符号中, subscripts and superscripts 分别用于表示下标或上标。而 Standard function names 通常是数学函数,如正弦、余弦、对数和求和,通常用特定符号或缩写表示。
In mathematical notation, subscripts and superscripts are used to denote indices or exponents, respectively. Whereas, Standard function names are commonly used mathematical functions such as sine, cosine, logarithm, and summation, often denoted by specific symbols or abbreviations.
Example
下面是如何在子标、上标和标准函数名称中包含数学表达式的示例。
Here is the example of including the mathmetical expression with Subscripts, Superscripts and Standard Function Names.
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()
以下是上述示例的输出 −
Here is the output of the above example −
Example
下面是另一个用文字写成轴标签和图例中下标的示例。
Here is another example that writes text in subscript in the axis labels and the legend.
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()
执行上述代码时,您将获得以下输出 -
On executing the above code you will get the following output −