Matplotlib 简明教程

Matplotlib - What is LaTeX?

LaTeX 是一种广泛用于制作科学和技术文档的排版系统,尤其是在数学、物理、计算机科学、工程学和学术写作等学科中。它以其对复杂的数学公式、科学符号和结构化文本格式的卓越排版而备受推崇。

LaTeX is a typesetting system widely used for producing scientific and technical documents, particularly in disciplines such as mathematics, physics, computer science, engineering and academic writing. It’s highly regarded for its superior typesetting of complex mathematical equations, scientific notations, and structured text formatting.

Key Aspects of LaTeX

以下是 LaTeX 的主要方面。

The below are the key aspects of LaTeX.

Markup Language

LaTeX 是一种标记语言,这意味着它使用命令和标记来格式化文本,不同于 WYSIWYG ,后者通常称为 What You See Is What You Get 编辑器。用户编写带有嵌入式命令的纯文本,这些命令指定结构和格式化。

LaTeX is a markup language, meaning it uses commands and tags to format text rather than WYSIWYG which is abbreviated as What You See Is What You Get editors. Users write plain text with embedded commands that specify the structure and formatting.

High-Quality Typesetting

LaTeX 擅长制作具有精确排版和排版特征的专业外观文档。它特别擅长处理复杂的结构,例如数学公式、表格、参考书目和交叉引用。

LaTeX excels in producing professional-looking documents with precise typographical and typesetting features. It handles complex structures like mathematical formulas, tables, bibliographies and cross-references exceptionally well.

Package System

LaTeX 提供了大量扩展其针对特定任务或文档类型功能的程序包,提供模板、样式和附加功能。

LaTeX offers a vast array of packages that extend its functionality for specific tasks or document types, providing templates, styles and additional features.

Free and Open Source

LaTeX 可免费使用,并得到一个强大的开源社区支持,保证持续开发,并提供丰富的程序包和资源生态系统。

LaTeX is free to use and is supported by a strong open-source community by ensuring continuous development and a rich ecosystem of packages and resources.

Components of LaTeX

matplotlib 库的 LaTex 具有以下组件。让我们详细了解其中的每一个组件。

The LaTex of matplotlib library have the following components. Let’s see each of them in detail.

Document Class

文档类指定所创建的文档类型,并且定义其整体结构、布局和格式化。它充当模板,为整个文档设置样式和行为。不同的文档类适用于各种类型的文档,例如文章、报告、书籍、演示文稿等等。

The document class specifies the type of document being created and defines its overall structure, layout and formatting. It acts as a template that sets the style and behavior for the entire document. Different document classes are available to suit various types of documents such as articles, reports, books, presentations and more.

Preamble

在 LaTeX 中,导言是文档中位于主内容和 \begin{document} 命令前面的部分。它定义了我们文档设置、加载程序包、设置参数以及针对整个文档应用的配置全局设置的位置。导言充当设置区域,在该区域中,我们为 LaTeX 处理文档正文做好准备。

In LaTeX the preamble is the section of the document that precedes the main content and the \begin{document} command. It is where we define the document settings, load packages, set parameters and configure global settings that apply to the entire document. The preamble acts as a setup area where we prepare LaTeX for processing the main body of the document.

Document Body

LaTeX 中的文档正文是我们的文档的内容所驻留的主部分。它在导言和 \begin{document} 命令之后开始,并且持续到 \end{document} 命令。此部分包括实际文本、区段、分段、方程、图片、表格以及构成文档核心内容的任何其他元素。

The document body in LaTeX is the main section where the content of our document resides. It starts after the preamble and the \begin{document} command and continues until the \end{document} command. This section includes the actual text, sections, subsections, equations, figures, tables and any other elements that constitute the core content of the document.

Advantages of LaTeX

以下是 LaTex 的优点。

The following are the advantages of LaTex.

  1. Quality Typesetting − Produces high-quality output, especially for scientific and technical documents.

  2. Cross-Referencing − Simplifies referencing and cross-referencing of equations, figures, tables, and sections.

  3. Version Control − Facilitates version control and collaboration through plain text-based files.

  4. Customization − Allows extensive customization of document styles, layouts and formatting.

Disadvantages of LaTeX

Learning Curve − 需要学习其语法和命令,这对于初学者而言可能具有威慑力。

Learning Curve − Requires learning its syntax and commands which can be daunting for beginners.

Limited WYSIWYG − 缺乏即时的视觉反馈(所见即所得),这对于习惯了图形编辑器的部分用户而言可能具有挑战性。

Limited WYSIWYG − Lack of immediate visual feedback (WYSIWYG) might be challenging for some users accustomed to graphical editors.

Usage of LaTeX

  1. Academic Writing − Academic papers, theses, dissertations

  2. Scientific − Scientific reports, articles, and journals

  3. Technical Documents − Technical documentation, manuals

  4. Presentations − Presentations using tools like Beamer

Basic document structure of the LaTex

Syntax

基本 LaTeX 文档结构包括 −

A basic LaTeX document structure includes −

\documentclass{article}
\begin{document}
\section{Introduction}
This is a simple LaTeX document.
\subsection{Subsection}
Some text in a subsection.
\end{document}

上述代码使用具有包含章节和子章节的层级结构的基本文章文档进行定义。

The above code defines a basic article document with a hierarchical structure comprising a section and a subsection.

LaTeX 是一种制作结构化、高质量文档的强大工具,尤其是在技术和学术领域。虽然它具有学习曲线,但它可以处理复杂的数学符号并制作专业外观文档的能力使其成为许多研究人员、学者和专业人士的首选。

LaTeX is a powerful tool for producing structured, high-quality documents especially in technical and academic fields. While it has a learning curve its ability to handle complex mathematical notations and produce professional-looking documents makes it a preferred choice for many researchers, academics and professionals.

Write our own LaTeX preamble

要在 Matplotlib 中编写自己的 LaTeX 导言,我们可以使用此示例作为参考。

To write our own LaTeX preamble in Matplotlib we can use this example as reference.

import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
x = np.linspace(-10, 10, 100)
y = np.exp(x)
plt.plot(x, y, color='red', label="$y=e^{x}$")
plt.legend(loc='upper right')
plt.show()

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

On executing the above code you will get the following output −

preamble

在 .py 文件中使用 Matplotlib 在图例中绘制 Latex 公式

Latex formula in the legend of a plot using Matplotlib inside a .py file

在此示例中,我们正在 .py 文件内的绘图图例中使用 Latex 公式。

In this example we are using the Latex formula in the legend of a plot inside a .py file.

import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
x = np.linspace(1, 10, 1000)
y = np.sin(x)
plt.plot(x, y, label=r'$\sin (x)$', c="red", lw=2)
plt.legend()
plt.show()

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

On executing the above code you will get the following output −

py latex2

在标签中,例如 label=r’αiπ+1=0',放置一个更为复杂的方程。

Put a little more complex equation in the label, for example, label=r’αiπ+1=0'

现在,查看绘图中右上角的图例。

Now, look at the legend at the top-right corner of the plot.

py latex3