📜  如何在 Matplotlib 中创建不同的子图大小?

📅  最后修改于: 2022-05-13 01:54:45.096000             🧑  作者: Mango

如何在 Matplotlib 中创建不同的子图大小?

在本文中,我们将学习使用 Matplotlib 创建不同大小的子图的不同方法。它提供了 3 种不同的方法,我们可以使用这些方法创建不同大小的不同子图。

可用于创建子图的方法:

  • 网格规格
  • gridspec_kw
  • subplot2grid

1. Gridspec :来自 gridspec 模块的 GridSpec 用于调整子图网格的几何形状。我们可以使用不同的参数来调整形状、大小、编号。列和行。

代码 :

Python3
# importing required libraries
import matplotlib.pyplot as plt
from matplotlib import gridspec
import numpy as np
 
# create a figure
fig = plt.figure()
 
# to change size of subplot's
# set height of each subplot as 8
fig.set_figheight(8)
 
# set width of each subplot as 8
fig.set_figwidth(8)
 
# create grid for different subplots
spec = gridspec.GridSpec(ncols=2, nrows=2,
                         width_ratios=[2, 1], wspace=0.5,
                         hspace=0.5, height_ratios=[1, 2])
 
# initializing x,y axis value
x = np.arange(0, 10, 0.1)
y = np.cos(x)
 
# ax0 will take 0th position in
# geometry(Grid we created for subplots),
# as we defined the position as "spec[0]"
ax0 = fig.add_subplot(spec[0])
ax0.plot(x, y)
 
# ax1 will take 0th position in
# geometry(Grid we created for subplots),
# as we defined the position as "spec[1]"
ax1 = fig.add_subplot(spec[1])
ax1.plot(x, y)
 
# ax2 will take 0th position in
# geometry(Grid we created for subplots),
# as we defined the position as "spec[2]"
ax2 = fig.add_subplot(spec[2])
ax2.plot(x, y)
 
# ax3 will take 0th position in
# geometry(Grid we created for subplots),
# as we defined the position as "spec[3]"
ax3 = fig.add_subplot(spec[3])
ax3.plot(x, y)
 
# display the plots
plt.show()


Python3
# importing required libraries
import matplotlib.pyplot as plt
import numpy as np
 
# setting different parameters to adjust each grid
fig, ax = plt.subplots(nrows=2, ncols=2, figsize=(7, 7),
                       gridspec_kw={
                           'width_ratios': [3, 3],
                           'height_ratios': [3, 3],
                       'wspace': 0.4,
                       'hspace': 0.4})
 
 
# initializing x,y axis value
x = np.arange(0, 10, 0.1)
y = np.tan(x)
 
# ax[0][0] will take 0th position in
# geometry(Grid we created for subplots)
ax[0][0].plot(x, y)
 
# ax[0][0] will take 0th position in
# geometry(Grid we created for subplots)
ax[0][1].plot(x, y)
 
# ax[0][0] will take 0th position in
# geometry(Grid we created for subplots)
ax[1][0].plot(x, y)
 
# ax[0][0] will take 0th position in
# geometry(Grid we created for subplots)
ax[1][1].plot(x, y)
 
plt.show()


Python3
# importing required library
import matplotlib.pyplot as plt
import numpy as np
 
# creating grid for subplots
fig = plt.figure()
fig.set_figheight(6)
fig.set_figwidth(6)
 
ax1 = plt.subplot2grid(shape=(3, 3), loc=(0, 0), colspan=3)
ax2 = plt.subplot2grid(shape=(3, 3), loc=(1, 0), colspan=1)
ax3 = plt.subplot2grid(shape=(3, 3), loc=(1, 2), rowspan=2)
ax4 = plt.subplot2grid((3, 3), (2, 0))
ax5 = plt.subplot2grid((3, 3), (2, 1), colspan=1)
 
 
# initializing x,y axis value
x = np.arange(0, 10, 0.1)
y = np.cos(x)
 
# plotting subplots
ax1.plot(x, y)
ax1.set_title('ax1')
ax2.plot(x, y)
ax2.set_title('ax2')
ax3.plot(x, y)
ax3.set_title('ax3')
ax4.plot(x, y)
ax4.set_title('ax4')
ax5.plot(x, y)
ax5.set_title('ax5')
 
# automatically adjust padding horizontally
# as well as vertically.
plt.tight_layout()
 
# display plot
plt.show()


输出 :

解释 :

这里“gridspec.GridSpec()”将为子图创建网格。我们可以使用不同的参数来调整网格和每个图的大小。

  • ncols :传递您想要在网格中的列数。
  • nrows :传递我们想要在 Grid 中制作子图的行数。
  • width_ratios :设置子图的宽度比(调整图的宽度)。
  • height_ratios :设置子图的高度比例(调整图的高度)。
  • wspace :垂直给“wspace”空间量来分隔子图。
  • hspace :给“hspace”水平的空间量来分隔子图。

2. gridspec_kw :它是在 Matplotlib 中的“plt.subplots()”方法中可用的字典。通过向字典传递不同的参数,我们可以调整每个子图的形状和大小。

代码 :

蟒蛇3

# importing required libraries
import matplotlib.pyplot as plt
import numpy as np
 
# setting different parameters to adjust each grid
fig, ax = plt.subplots(nrows=2, ncols=2, figsize=(7, 7),
                       gridspec_kw={
                           'width_ratios': [3, 3],
                           'height_ratios': [3, 3],
                       'wspace': 0.4,
                       'hspace': 0.4})
 
 
# initializing x,y axis value
x = np.arange(0, 10, 0.1)
y = np.tan(x)
 
# ax[0][0] will take 0th position in
# geometry(Grid we created for subplots)
ax[0][0].plot(x, y)
 
# ax[0][0] will take 0th position in
# geometry(Grid we created for subplots)
ax[0][1].plot(x, y)
 
# ax[0][0] will take 0th position in
# geometry(Grid we created for subplots)
ax[1][0].plot(x, y)
 
# ax[0][0] will take 0th position in
# geometry(Grid we created for subplots)
ax[1][1].plot(x, y)
 
plt.show()

输出 :

解释 :

“gridspec_kw = {}”是一个带有keywards的字典,我们可以使用它来改变形状、大小和调整每个网格。

  • nrows :网格中的行数
  • ncols :网格中的列数
  • width_ratios :设置每个子图的宽度大小
  • height_ratios :设置每个子图的高度大小
  • wspace :垂直给“wspace”空间量来分隔子图。
  • hspace :给“hspace”水平的空间量来分隔子图。
  • figsize :设置子图的大小。

3. subplot2grid :它为在任何位置创建网格提供了更大的灵活性。我们可以很容易地水平和垂直扩展网格。

代码 :

蟒蛇3

# importing required library
import matplotlib.pyplot as plt
import numpy as np
 
# creating grid for subplots
fig = plt.figure()
fig.set_figheight(6)
fig.set_figwidth(6)
 
ax1 = plt.subplot2grid(shape=(3, 3), loc=(0, 0), colspan=3)
ax2 = plt.subplot2grid(shape=(3, 3), loc=(1, 0), colspan=1)
ax3 = plt.subplot2grid(shape=(3, 3), loc=(1, 2), rowspan=2)
ax4 = plt.subplot2grid((3, 3), (2, 0))
ax5 = plt.subplot2grid((3, 3), (2, 1), colspan=1)
 
 
# initializing x,y axis value
x = np.arange(0, 10, 0.1)
y = np.cos(x)
 
# plotting subplots
ax1.plot(x, y)
ax1.set_title('ax1')
ax2.plot(x, y)
ax2.set_title('ax2')
ax3.plot(x, y)
ax3.set_title('ax3')
ax4.plot(x, y)
ax4.set_title('ax4')
ax5.plot(x, y)
ax5.set_title('ax5')
 
# automatically adjust padding horizontally
# as well as vertically.
plt.tight_layout()
 
# display plot
plt.show()

输出 :

解释 :

这里的形状表示不。行和列的数量和 loc 表示网格的几何位置。假设如果我们在 ax1 中使用 colspan = 3,这意味着这些子图将覆盖该行的 3 个所有列。同样在 ax2 中,colspan=1 表示它将在其位置覆盖 1 个列空间。在 ax3 的情况下,rowspan=2 意味着它将覆盖 2 行的空间。如果我们给出不规则的输入,那么它会给出错误,所以我们给 colspan 和 rowspan 赋予了适当的值。