Matplotlib 简明教程

Matplotlib - Print Stdout

Print − 通常是指在屏幕或纸张上显示文本或数字等内容。

Print − usually refers to displaying something, like text or numbers, on a screen or on paper.

stdout − 表示“标准输出”。这是一种程序将信息(如文本或数据)发送到程序自身之外的方式。当程序将内容“打印”至 stdout 时,它本质上是在说,“这里有一些信息,我想显示或分享”。

stdout − stands for "standard output." It is a way for a program to send information (like text or data) out of itself. When a program "prints" something to stdout, it is essentially saying, "Here’s some information I want to show or share".

因此,当您听到“打印 stdout”时,它的含义是某个程序正在发送一些信息,以一种标准的方式显示,通常是在您的屏幕上。它是程序与用户或计算机系统其他部分通信的一种基本方法。

So, when you hear "print stdout" together, it means a program is sending some information to be displayed in a standard way, usually on your screen. It is a basic method for programs to communicate with users or other parts of the computer system.

Print Stdout in Matplotlib

“打印 stdout”一词并非 Matplotlib 特有的。它通常是指在 Python 中将输出打印到标准输出流,也就是您运行 Python 代码的控制台或终端。

The term "print stdout" is not specific to Matplotlib. It generally refers to printing output to the standard output stream in Python, which is the console or terminal where you run your Python code.

在 Matplotlib 中,如果希望将输出打印到控制台,可以使用 Python 的内置 print() 函数。例如,可以打印关于您的绘图、数据,或任何消息或调试信息的信息。

In Matplotlib, if you want to print output to the console, you can use Python’s built-in print() function. For example, you can print information about your plot, data, or any messages or debugging information.

Print Text on Plot

在 Matplotlib 中的绘图上打印文本允许您添加标签、标题、注释或任何其他文本信息,以增强对数据可视化的理解。您可以在绘图上的特定位置放置文本,以提供说明、重点突出重点或为不同元素贴上标签。

Printing text on a plot in Matplotlib allows you to add labels, titles, annotations, or any other textual information to enhance the understanding of your data visualization. You can place text at specific locations on the plot to provide explanations, highlight important points, or label different elements.

Example

在下面这个示例中,我们绘制一条线,然后使用 text() 函数将文本“你好,Matplotlib!”添加到绘图中。我们将文本定位在具有指定“字体大小”和“颜色”的坐标(2,5)处 −

In the following example, we plot a line and add the text "Hello, Matplotlib!" to the plot using the text() function. We position the text at coordinates (2, 5) with a specified "fontsize" and "color" −

import matplotlib.pyplot as plt

# Creating a simple plot
plt.plot([1, 2, 3], [4, 5, 6])

# Adding text annotation to the plot
plt.text(2, 5, 'Hello, Matplotlib!', fontsize=12, color='red')

# Displaying the plot
plt.show()

以下是上面代码的输出: -

Following is the output of the above code −

print stdout1

Print Text on Polar Plot

在 Matplotlib 中的极坐标绘图上打印文本允许您添加标签、注释或任何其他文本信息,以增强对极坐标可视化的理解。就像在笛卡尔坐标绘图中一样,您可以在极坐标绘图上的特定位置放置文本,以提供说明、重点突出重点或为不同元素贴上标签。

Printing text on a polar plot in Matplotlib allows you to add labels, annotations, or any other textual information to enhance the understanding of your polar visualization. Just like in Cartesian plots, you can place text at specific locations on the polar plot to provide explanations, highlight important points, or label different elements.

Example

在这里,我们创建一个极坐标绘图,然后使用 text() 函数将文本“极坐标绘图”添加到绘图中。我们将文本定位在与原点成“π/2”(90 度)角且半径为“0.5”的地方,并指定了字体大小、颜色和水平对齐 −

In here, we create a polar plot and add the text "Polar Plot" to the plot using the text() function. We position the text at an angle of "π/2" (90 degrees) and a radius of "0.5" from the origin, with a specified fontsize, color, and horizontal alignment −

import matplotlib.pyplot as plt
import numpy as np

# Creating a polar plot
theta = np.linspace(0, 2*np.pi, 100)
r = np.sin(3*theta)

plt.polar(theta, r)

# Adding text annotation to the polar plot
plt.text(np.pi/2, 0.5, 'Polar Plot', fontsize=12, color='blue', ha='center')

# Displaying the polar plot
plt.show()

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

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

print stdout2

Print Text with Rotation

在 Matplotlib 中使用旋转打印文本允许您倾斜显示文本,这样做对于用倾斜或对角线方向标记元素或者为绘图添加风格效果非常有用。

Printing text with rotation in Matplotlib allows you to display text at an angle, which can be useful for labeling elements with slanted or diagonal orientations, or for adding stylistic effects to your plots.

Example

现在,我们绘制一条直线,并且使用 text() 函数的 “rotation” 参数将旋转文本添加到该直线。我们定位在坐标 (2, 5) 的文本 "Rotated Text",并添加了指定字号、颜色以及 "45" 度旋转角度 −

Now, we plot a line and add rotated text to the plot using the "rotation" parameter of the text() function. We position the text "Rotated Text" at coordinates (2, 5) with a specified fontsize, color, and rotation angle of "45" degrees −

import matplotlib.pyplot as plt

# Creating a simple plot
plt.plot([1, 2, 3], [4, 5, 6])

# Adding rotated text annotation to the plot
plt.text(2, 5, 'Rotated Text', fontsize=12, color='purple', rotation=45)

# Displaying the plot
plt.show()

执行上面的代码后,我们得到以下输出: -

After executing the above code, we get the following output −

print stdout3

Print Text with Box

在 Matplotlib 中使用一个方框打印文本是指使用一个包含它的方框将文本注释添加到一个直线,突出显示重要信息。这些注释可以包括标记、标题或说明,它们都包含一个矩形或方形框中,以引起对他人的注意。

Printing text with a box in Matplotlib refers to adding textual annotations to a plot with a surrounding box, highlighting important information. These annotations can include labels, titles, or descriptions, and are enclosed within a rectangular or square-shaped box to draw attention to them.

要使用在 Matplotlib 中围绕着文字打印一个方框,您可以使用 text() 函数的 "bbox" 参数。此参数允许您指定方框的属性,如它的颜色、透明度和边框宽度。

To print text with a box around it in Matplotlib, you can use the "bbox" parameter of the text() function. This parameter allows you to specify the properties of the box, such as its color, transparency, and border width.

Example

在下面的例子中,我们绘制一条直线,并且使用 text() 函数将包含的方框文本添加到该直线。我们定位在坐标 (2, 5) 的文本 "Boxed Text",并添加了指定字号、颜色,以及一个带有圆形角、浅黄色面的方框以及橙色边缘 −

In the following example, we plot a line and add boxed text to the plot using the text() function. We position the text "Boxed Text" at coordinates (2, 5) with a specified fontsize, color, and a box with rounded corners, light yellow face color, and orange edge color −

import matplotlib.pyplot as plt

# Creating a simple plot
plt.plot([1, 2, 3], [4, 5, 6])

# Adding boxed text annotation to the plot
plt.text(2, 5, 'Boxed Text', fontsize=12, color='orange', bbox=dict(facecolor='lightyellow', edgecolor='orange', boxstyle='round,pad=0.5'))

# Displaying the plot
plt.show()

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

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

print stdout4