如何使用subplot在Matplotlib中创建多个子图?

作者:吉安淘贝游戏开发公司 阅读:67 次 发布时间:2023-07-07 17:23:15

摘要:Matplotlib是一个Python库,可用于绘制数据可视化图形。子图是作为单个图形中的多个小图形或绘图区域的方式出现的Matplotlib。子图可以帮助您在同一图形或图表中呈现多个相关数据集。使用Matplotlib的“subplot”功能,您可以创建和设计多个子图,以便有效地分析和展示数据。...

Matplotlib是一个Python库,可用于绘制数据可视化图形。子图是作为单个图形中的多个小图形或绘图区域的方式出现的Matplotlib。子图可以帮助您在同一图形或图表中呈现多个相关数据集。使用Matplotlib的“subplot”功能,您可以创建和设计多个子图,以便有效地分析和展示数据。

如何使用subplot在Matplotlib中创建多个子图?

在本文中,我们将了解如何使用“subplot”在Matplotlib中创建多个子图。在开始之前,请确保已按照以下方式安装Matplotlib:

`pip install matplotlib`

现在,让我们开始了解Matplotlib中子图的创建。

Matplotlib中的Subplot

Matplotlib中的子图是在同一图形中创建的多个小绘图区域。通过使用“subplot()”功能,您可以将单个图形分成多个子图,每个子图都可以包含单个或多个绘图区域。该功能接受三个参数:行,列以及子图编号,它决定了子图应该位于图形的哪个部分。以下是创建子图的语法:

```python

import matplotlib.pyplot as plt

# Create a figure and a set of subplots

fig, axs = plt.subplots(rows, columns, subplot_kw=options)

```

这将创建一个由“rows”行和“columns”列组成的格网。每个网格都包含一个子图。在此语法中,“subplot_kw”是一个可选参数,用于指定在子图上设置的属性和值。如果省略此参数,则将使用默认的绘图选项。接下来,让我们介绍一些Matplotlib中常见的子图类型。

Matplotlib中的子图类型

在Matplotlib中,有几种常见的子图类型。以下是其中的几个。

1. 简单子图:简单子图是同一图形中唯一的子图类型。它使用“subplot()”函数创建。每个子图包含单个绘图区域。以下是一个创建简单子图的示例:

```python

import matplotlib.pyplot as plt

# Create a simple subgraph

fig, axs = plt.subplots()

# Display the subgraph

plt.show()

```

在这个代码段中,“plt.subplots()”函数创建一个简单的子图,并将其存储在“fig”和“axs”变量中。最后一行将子图显示在屏幕上。

2. 网格子图:网格子图包含多个小绘图区域,这些区域在同一图形中创建。使用“subplot()”函数可轻松创建图形。接下来是创建网格子图的语法:

```python

import matplotlib.pyplot as plt

# Create a multi-plot grid with 2 rows and 3 columns

fig, axs = plt.subplots(2, 3)

# Display the subgraphs

plt.show()

```

在这个代码段中,我们传递了两个参数给“plt.subplots()”函数,即“2”和“3”,以创建一个包含2行和3列的网格子图。子图按行和列编号,从左上角开始。

3. 分层子图:可以使用多个子图显示不同的数据类型。分层子图包含多个绘图区域,每个区域显示一个独立的数据集。以下是使用Matplotlib创建分层子图的示例:

```python

import matplotlib.pyplot as plt

# Create a figure with multiple subgraphs

fig = plt.figure()

# Define the two subgraphs

ax_upper = fig.add_subplot(2, 1, 1)

ax_lower = fig.add_subplot(2, 1, 2)

# Display the subgraphs

plt.show()

```

在这个示例中,“fig.add_subplot()”函数将根据指定的编号、行和列创建子图。这取决于您希望在子图中呈现的数据类型。

4. 堆叠子图:堆叠子图是一种特殊的子图类型,可用于在单个绘图区域中显示多个数据集。在Matplotlib中,您可以使用“stackplot()”函数创建堆叠子图。以下是堆叠子图的示例:

```python

import matplotlib.pyplot as plt

# Create some data for the stackplot

x = [1, 2, 3, 4, 5]

y1 = [5, 6, 7, 8, 9]

y2 = [2, 4, 6, 8, 10]

# Create the stackplot

plt.stackplot(x, y1, y2)

# Display the graph

plt.show()

```

在这个代码段中,“plt.stackplot()”函数将两个数据集,“y1”和“y2”,定位在指定的x坐标点上。堆叠子图使用颜色填充,每个填充表示一个数据集。最后,“plt.show()”函数可用于显示图表。

如您所见,Matplotlib提供了多种类型的子图,可用于展示不同类型的数据。接下来,我们将在Matplotlib中创建一个网格子图。

创建网格子图

在本示例中,我们将创建一个包含4个子图的网格。以下是创建网格子图的代码:

```python

import numpy as np

import matplotlib.pyplot as plt

# Create some data to plot

x = np.arange(0, 4 * np.pi, 0.1)

y1 = np.sin(x)

y2 = np.cos(x)

# Create the figure and subplots

fig, axs = plt.subplots(2, 2)

# Plot the first subplot

axs[0, 0].plot(x, y1)

axs[0, 0].set_title('sin(x)')

# Plot the second subplot

axs[0, 1].plot(x, y2, 'tab:orange')

axs[0, 1].set_title('cos(x)')

# Plot the third subplot

axs[1, 0].plot(x, y1+y2, 'tab:green')

axs[1, 0].set_title('sin(x)+cos(x)')

# Plot the fourth subplot

axs[1, 1].plot(x, np.abs(y1), 'tab:red')

axs[1, 1].set_title('|sin(x)|')

# Add a title to the figure

fig.suptitle('Different Plots')

# Display the subplots

plt.show()

```

在此代码段中,我们使用Numpy创建了一些数据来绘制图形。接下来,使用“plt.subplots()”函数创建了一个2行和2列的网格子图。每个子图由其行和列编号引用,“axs [0,0]”引用第一行第一列的子图,“axs [0,1]”引用第一行第二列的子图,以此类推。

网格的每个子图都使用“plot()”函数绘制相应的数据,并使用“set_title()”函数添加一个标题。最后,“fig.suptitle()”函数用于添加网格的标题。

下面是网格子图的最终输出:

![subplot](https://cdn.jsdelivr.net/gh/WangRongsheng/cdn/img/dataplattform/matplotlib/20220316154928.png)

在本示例中,我们创建了每个子图的“plot()”方法,在子图中绘制了不同的数据集。您可以根据需要自定义每个子图的颜色和样式。

结论

在本文中,我们介绍了如何在Matplotlib中使用“subplot”创建和设计多个子图。我们开始讨论了Matplotlib中支持的不同子图类型,并深入了解了网格子图。通过在单个图形中包含多个子图,您可以轻松地对多个不同数据集进行比较和可视化。 Matplotlib是一个功能强大的工具,可用于生成高质量的数据可视化图形,并且使用“subplot”功能可轻松设计多个子图。

  • 原标题:如何使用subplot在Matplotlib中创建多个子图?

  • 本文链接:https://qipaikaifa1.com/jsbk/15364.html

  • 本文由吉安淘贝游戏开发公司小编,整理排版发布,转载请注明出处。部分文章图片来源于网络,如有侵权,请与淘贝科技联系删除。
  • 微信二维码

    CTAPP999

    长按复制微信号,添加好友

    微信联系

    在线咨询

    点击这里给我发消息QQ客服专员


    点击这里给我发消息电话客服专员


    在线咨询

    免费通话


    24h咨询☎️:189-2934-0276


    🔺🔺 棋牌游戏开发24H咨询电话 🔺🔺

    免费通话
    返回顶部