Matplotlib 简明教程

Matplotlib - Basic Units

What are Basic Units?

在 Matplotlib 中,基本单位是指用于定义和测量绘图各个方面的基本元素,如长度、宽度、位置和坐标。了解这些单位对于准确将元素放置在绘图中并控制其整体布局至关重要。

In Matplotlib basic units refer to the fundamental elements used to define and measure various aspects of a plot such as length, width, positions and coordinates. Understanding these units is crucial for accurately placing elements within a plot and controlling its overall layout.

Common Basic Units in Matplotlib

以下是 matplotlib 库中常见的基本单位。

The following are the common basic units available in matplotlib library.

Points (pt)

在 Matplotlib 中, points 通常指用于指定绘图中各种可视属性(例如线宽、标记大小和文本大小)的测量单位。点 (pt) 是排版和图形中的标准单位,通常用于其与设备无关的大小表示。

In Matplotlib points typically refer to a unit of measurement used for specifying various visual properties within a plot such as line widths, marker sizes and text sizes. The point (pt) is a standard unit in typography and graphics often used for its device-independent size representation.

Usage of Points in Matplotlib

Line Widths − Matplotlib 函数中 linewidth 参数(例如 plt.plot() )允许我们以点为单位指定线宽。

Line Widths − The linewidth parameter in Matplotlib functions such as plt.plot() allows us to specify the width of lines in points.

Text Sizes − Matplotlib 函数 plt.xlabel(), plt.ylabel(), plt.title()fontsize 参数启用以点为单位设置文本的大小。

Text Sizes − The fontsize parameter in Matplotlib functions plt.xlabel(), plt.ylabel(), plt.title() enables setting the size of text in points.

Marker Sizes − 在绘图函数中 markersize 参数(例如 plt.plot())以点为单位定义标记的大小。

Marker Sizes − The markersize parameter in plot functions such as plt.plot() defines the size of markers in points.

Figure Size − 在使用 plt.figure(figsize=(width, height)) 指定图形大小时,尺寸以英寸为单位。然而,默认大小可能因系统而异,比如 1 英寸可能并不总等于 72 点,但这是典型惯例。

Figure Size − When specifying the figure size using plt.figure(figsize=(width, height)) the dimensions are in inches. However the default size might differ across systems such as 1 inch might not always equal 72 points but this is the typical convention.

Converting Points to Other Units

Matplotlib 在内部使用点,但设置图形大小或尺寸时通常使用英寸。作为参考, 1 英寸大约等于 72 点。

Matplotlib works internally with points but when setting figure sizes or dimensions it’s common to use inches. As a reference 1 inch is approximately equal to 72 points.

  1. 1 inch = 72 points

  2. 1 point ≈ 0.01389 inches

了解和使用点作为 Matplotlib 中的测量单位,允许精确控制绘制中元素的可视属性,从而确保所需的外观和可读性。

Understanding and utilizing points as a unit of measurement in Matplotlib allows for precise control over the visual properties of elements within plots ensuring the desired appearance and readability.

在此示例中,我们设置线绘图的线宽(以点为单位)。

In this example we are setting the line width in points for the line plot.

import matplotlib.pyplot as plt
plt.figure(figsize=(6, 4))  # 6 inches wide, 4 inches tall
plt.plot([1, 2, 3], [4, 5, 6], linewidth=3.5)  # Line width in points
plt.xlabel('X-axis', fontsize=12)  # Text size in points
plt.ylabel('Y-axis', fontsize=12)
plt.show()

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

Following is the output of the above code −

points

Inches (in)

英寸用于指定 Matplotlib 中图形或子图的整体大小。例如, figsize = (width, height) 以英寸为单位定义了图形的宽度和高度。

Inches are used to specify the overall size of a figure or subplots in Matplotlib. For example figsize = (width, height) in inches defines the width and height of a figure.

在此示例中,我们正在指定图形大小(以英寸为单位)。

In this example we are specifying the size of figure in inches.

import matplotlib.pyplot as plt
plt.figure(figsize=(4, 4))  # 6 inches wide, 4 inches tall
plt.plot([1, 2, 3], [4, 5, 6])
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Example Plot with 4x4 Inches')
plt.show()

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

Following is the output of the above code −

inches

Conversion

英寸和 Matplotlib 中常用的其他单位之间的换算系数:

The conversion factor between inches and other units commonly used in Matplotlib:

1 英寸 ≈ 2.54 厘米

1 inch ≈ 2.54 centimeters

Data Units

数据单位表示绘制在 x 轴和 y 轴上的实际值。Matplotlib 使用这些单位在图形上绘制数据点。从数据单位到绘图中物理单位的转换由轴比例(例如线性、对数等)决定。

Data units represent the actual values plotted along the x-axes and y-axes. Matplotlib uses these units to plot data points on the graph. The conversion from data units to physical units on the plot is determined by the axis scales such as linear, logarithmic etc.

Key Points about Data Units in Matplotlib

Numerical Data − 数据单位表示我们在 x 轴和 y 轴上绘制的数字值。

Numerical Data − Data units represent the numeric values that we plot along the x-axis and y-axis.

Mapping to Plot Space − Matplotlib 会根据定义的轴限制和缩放比例将这些数字数据值映射到绘图空间内的具体位置。

Mapping to Plot Space − Matplotlib maps these numerical data values to specific positions within the plot space based on the defined axis limits and scaling.

Plotting Data − 当我们使用 Matplotlib 绘制数据时,我们需要使用数字数据指定 x 和 y 坐标。Matplotlib 负责对这些数据点进行缩放和定位。

Plotting Data − When we plot data using Matplotlib then we have to specify the x and y coordinates using numerical data. Matplotlib takes care of scaling and positioning these data points within the plot.

Axis Limits − 使用 xlim 设置的 x 轴限制,以及使用 ylim 设置的 y 轴限制,定义了绘图中显示的数据单元范围。

Axis Limits − The limits set on the x-axis using xlim and y-axis using ylim define the range of data units displayed in the plot.

Axis Ticks − 轴上的刻度标记表示特定的数据单元,为绘图中的数据范围提供可视参照。

Axis Ticks − The tick marks on the axes represent specific data units providing a visual reference for the data range present in the plot.

在这个例子里,我们正在设置绘图的数据单元。

In this example we are setting the data units of a plot.

import matplotlib.pyplot as plt

# Sample data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]

# Plotting using data units
plt.plot(x, y, marker='o')

# Setting axis limits in data units
plt.xlim(0, 6)  # X-axis limits from 0 to 6
plt.ylim(0, 12)  # Y-axis limits from 0 to 12
plt.xlabel('X-axis (Data Units)')
plt.ylabel('Y-axis (Data Units)')
plt.title('Plot with Data Units')
plt.show()

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

Following is the output of the above code −

data units

了解 Matplotlib 中的数据单元对于准确可视化数据和确保绘制的图像正确对应于基础数字值至关重要。

Understanding data units in Matplotlib is essential for accurately visualizing data and ensuring that the plotted representation corresponds correctly to the underlying numerical values.

Axes Coordinates

轴坐标的范围从 0.0 到 1.0,用于指定各个子图或轴内的相对位置。 (0, 0) 表示子图的左下角, (1, 1) 表示子图的右上角。

Axes coordinates range from 0.0 to 1.0 and specify the relative position within an individual subplot or axis. (0, 0) represents the bottom-left corner and (1, 1) represents the top-right corner of the subplot.

轴坐标提供了一种灵活的方式,可按照其大小和边界在子图中定位注释、文本或其它元素,确保在不同绘图大小或尺寸之间实现一致且响应迅速的定位。

Axes coordinates offer a flexible way to position annotations, text, or other elements within a subplot relative to its size and boundaries, ensuring consistent and responsive positioning across different plot sizes or dimensions.

在这个例子里,我们正在设置绘图的 x 和 y 轴坐标。

Here n this example we are setting the axes coordinates x and y of a plot.

import matplotlib.pyplot as plt

# Creating a subplot
fig, ax = plt.subplots()

# Plotting data
ax.plot([1, 2, 3], [2, 4, 3])

# Annotating at axes coordinates
ax.annotate('Important Point',
   xy=(2, 4),               # Data point to annotate
   xytext=(0.5, 0.5),       # Position in axes coordinates
   textcoords='axes fraction',  # Specifying axes coordinates
   arrowprops=dict(facecolor='black', arrowstyle='->'))
plt.show()

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

Following is the output of the above code −

axes

Figure Coordinates

图形坐标的范围也从 0.01.0 ,但指定的是相对于整个图形画布的位置。 (0, 0) 是整个图形的左下角, (1, 1) 是整个图形的右上角。

Figure coordinates also range from 0.0 to 1.0 but specify positions relative to the entire figure canvas. (0, 0) is the bottom-left corner and (1, 1) is the top-right corner of the entire figure.

这是一个设置绘图的图形坐标的例子。

This is an example of setting the figure coordinates of a plot.

import matplotlib.pyplot as plt

# Creating subplots
fig, (ax1, ax2) = plt.subplots(1, 2)

# Plotting data in subplot 1
ax1.plot([1, 2, 3], [2, 4, 3])

# Plotting data in subplot 2
ax2.plot([1, 2, 3], [3, 1, 4])

# Annotating at figure coordinates
fig.text(0.5, 0.9, 'Figure Annotation', ha='center', fontsize=12, color='red')
plt.show()

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

Following is the output of the above code −

figure coordinates

Plot with Matplolib all basic units

这个例子展示了在 Matplotlib 中使用各种单位,例如用于图形大小的英寸、用于文本大小的点、用于绘图数据点的数字单元以及用于定位子图内注释的轴坐标。

This example showcases the usage of various units in Matplotlib such as inches for figure size, points for text sizes, data units for plotting data points and axes coordinates for positioning annotations within a subplot.

import matplotlib.pyplot as plt

# Define a figure with specified size in inches
fig = plt.figure(figsize=(6, 4))  # Width: 6 inches, Height: 4 inches

# Create an axis within the figure
ax = fig.add_subplot(111)

# Plotting data points using data units
x = [1, 2, 3, 4]
y = [2, 4, 6, 8]
ax.plot(x, y, marker='o')

# Setting labels and titles using text in points
ax.set_xlabel('X-axis Label', fontsize=12)
ax.set_ylabel('Y-axis Label', fontsize=12)
ax.set_title('Plot Title', fontsize=14)

# Setting the position of a text annotation using axes coordinates
ax.text(0.5, 0.5, 'Annotation', transform=ax.transAxes, fontsize=10)
plt.show()

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

Following is the output of the above code −

overall units