Matplotlib 简明教程

Matplotlib - Coords Report

坐标报告(简称“坐标报告”)是一个包含有关地图上不同位置或点的信息的文档或数据文件。使用坐标描述这些位置,坐标就像一组指示,准确地指出地球表面的某个位置。

A coords report, short for "coordinates report," is a document or data file that contains information about different locations or points on a map. These locations are described using coordinates, which are like a set of directions that pinpoint exactly where something is on the Earth’s surface.

想象一下,您有一份您所在社区的大地图,并且您想确切地告诉某人您的房子在哪里。您可以给他们坐标,这就像地图上您房子准确位置的密码。此代码可能如下所示:纬度 40.7128° N,经度 74.0060° W -

Imagine you have a big map of your neighborhood, and you want to tell someone exactly where your house is. You can give them the coordinates, which are like the map’s secret code for your house’s exact location. This code might look something like this: Latitude 40.7128° N, Longitude 74.0060° W −

coords report1

Coords Report in Matplotlib

在 Matplotlib 中,坐标报告提供有关绘图中点或对象位置的信息。此报告包括详细信息,例如每个点的 x 坐标和 y 坐标,以及任何附加属性,例如大小、颜色或标签。

In Matplotlib, a coordinates report provides information about the position of points or objects within a plot. This report includes details such as the x-coordinate and y-coordinate of each point, as well as any additional properties like size, color, or label.

您在 Matplotlib 中使用 scatter() 函数创建一个坐标报告或任何其他涉及坐标的绘图。此函数采用两个参数:x 和 y,它们表示要绘制的点的坐标。

You create a coord report or any other type of plot that involves coordinates in Matplotlib using the scatter() function. This function takes two arguments: x and y, which represent the coordinates of the points to be plotted.

Scatter Plot Coord Report

在 Matplotlib 中,散点图坐标报告提供有关散点图中显示的各个数据点的详细信息。此报告包括每个点的 x 和 y 坐标,允许您精确地在绘图中识别其位置。此外,它可能包含与每个数据点关联的其他属性,例如大小、颜色或标签。

In Matplotlib, a scatter plot coordinate report provides detailed information about the individual data points displayed in a scatter plot. This report includes the x and y coordinates of each point, allowing you to precisely identify your positions on the plot. Additionally, it may contain other properties such as the size, color, or label associated with each data point.

Example

在以下示例中,我们正在使用两组数据创建一个散点图:x 和 y,其中 x 表示 x 轴上的值,而 y 表示 y 轴上的相应值。此外,我们使用 for 循环遍历 x 和 y 中的每对坐标,并且对于每个点,我们使用 text() 函数显示坐标作为绘图中的文本。这提供了散点图上每个点的坐标的可视化表示 -

In the following example, we are creating a scatter plot with two sets of data: x and y, where x represents the values on the x-axis and y represents the corresponding values on the y-axis. Additionally, we iterate through each pair of coordinates in x and y using a for loop, and for each point, we use the text() function to display the coordinates as text on the plot. This provides a visual representation of the coordinates for each point on the scatter plot −

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]

plt.scatter(x, y)
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.title('Scatter Plot Coord Report')
plt.grid(True)

# Displaying coordinates
for i in range(len(x)):
   plt.text(x[i], y[i], f'({x[i]}, {y[i]})', fontsize=8, verticalalignment='bottom')

plt.show()

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

Following is the output of the above code −

coords report2

Line Plot Coord Report

在 Matplotlib 中,折线图坐标报告提供有关折线图中沿线绘制的点的详细信息。此报告包括每个点的 x 和 y 坐标,表示它们在绘图中的位置。此外,它可能包含与每个点关联的其他属性,例如标记、颜色或标签。

In Matplotlib, a line plot coordinate report provides detailed information about the points plotted along a line in a line plot. This report includes the x and y coordinates of each point, indicating their positions on the plot. Additionally, it may contain other attributes such as markers, colors, or labels associated with each point.

Example

在此,我们使用两组数据创建一个折线图:x 和 y,其中 x 表示 x 轴上的值,而 y 表示 y 轴上的相应值 -

In here, we are creating a line plot using two sets of data: x and y, where x represents the values on the x-axis and y represents the corresponding values on the y-axis −

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]

plt.plot(x, y)
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.title('Line Plot Coord Report')
plt.grid(True)

# Display coordinates
for i in range(len(x)):
   plt.text(x[i], y[i], f'({x[i]}, {y[i]})', fontsize=8, verticalalignment='bottom')
plt.show()

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

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

coords report3

Bar Plot Coord Report

在 Matplotlib 中,柱状图坐标报告提供有关柱状图中显示的柱状图的详细信息。此报告包括每个柱状图的 x 和 y 坐标,表示它们在绘图中的位置。此外,它可能包含其他属性,例如每个柱状图的宽度、高度或颜色。

In Matplotlib, a bar plot coordinate report provides detailed information about the bars displayed in a bar plot. This report includes the x and y coordinates of each bar, indicating their positions on the plot. Additionally, it may contain other attributes such as the width, height, or color of each bar.

Example

在以下示例中,我们首先定义两组数据“x”和“y”,创建一个柱状图。然后,我们使用 text() 函数将坐标报告放置在绘图上。此函数使用第三个数据点“C”的标签及其相应值“7”指定坐标 (2, 7),该值格式为红色并水平居中 -

In the example below, we create a bar plot by first defining two sets of data: 'x' and 'y'. Then, we place a coordinate report on the plot using the text() function. This function specifies the coordinates (2, 7) with the label of the third data point 'C' and its corresponding value '7', formatted in red color and centered horizontally −

import matplotlib.pyplot as plt

# Sample data
x = ['A', 'B', 'C', 'D', 'E']
y = [10, 15, 7, 10, 12]

plt.bar(x, y)
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.title('Bar Plot Coord Report')
plt.grid(axis='y')

# Coordinate report
plt.text(2, 7, f'({x[2]}, {y[2]})', fontsize=12, color='red', ha='center')

plt.show()

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

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

coords report4

Pie Chart Coord Report

在 Matplotlib 中,饼图坐标报告提供有关饼图中显示的各个扇区的详细信息。此报告包括每个扇区的作为整个饼图的几分之一表示的位置和大小。此外,它可能包含与每个扇区关联的其他属性,例如标签或颜色。

In Matplotlib, a pie chart coordinate report provides detailed information about the segments displayed in a pie chart. This report includes the position and size of each segment, represented as a fraction of the whole pie. Additionally, it may contain other attributes such as labels or colors associated with each segment.

Example

现在,我们正在创建一个带有标签和相应大小的饼图饼图。“autopct”参数格式化图表上显示的百分比值。此外,我们正在使用坐标 (0, 0) 将坐标报告放置在图表中心 -

Now, we are creating a pie chart pie chart with labels and corresponding sizes. The "autopct" parameter formats the percentage values displayed on the chart. Additionally, we are placing a coordinate report at the center of the chart with coordinates (0, 0) −

import matplotlib.pyplot as plt

# Sample data
labels = ['A', 'B', 'C', 'D', 'E']
sizes = [15, 30, 20, 10, 25]

plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=140)
plt.axis('equal')
plt.title('Pie Chart Coord Report')

# Coordinate report
plt.text(0, 0, f'(0.0, 0.0)', fontsize=12, color='yellow', ha='center')

plt.show()

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

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

coords report5