15_多子图-Subplot、Subplot: 使用多个figures和 axes、替代解决方案:

本文详细介绍了在Matplotlib中创建和布局多子图的方法,包括使用subplot函数和面向对象的方式。通过示例展示了如何在图表中设置子图,去除刻度,以及填充真实数据。此外,还提到了使用元组表示合并子图的技巧以及调整子图间距的方法。
摘要由CSDN通过智能技术生成

15.多子图-Subplot
15.1.Subplot: 使用多个figures和 axes
15.2.替代解决方案:

15.多子图-Subplot

Matplotlib绘图时一个常见问题是如何在一个图中包含多个图。即如何实现在一个窗口中有多个图形,每个图形都出现在子图中。
我们将使用两种不同的方法来实现这一目标:
subplot
gridspec

15.1.Subplot: 使用多个figures和 axes

subplot及其参数:
subplot(nrows, ncols, plot_number)
如果将subplot应用于一个figure,则该figure将在概念上分为nrows * ncols个子轴。
参数plot_number标识函数调用创建的subplot。 plot_number的范围可以从1到最大nrows * ncols。
如果三个参数的值小于10,则可以使用一个int参数调用函数subplot,其中百位数表示nrows,十位数表示ncols,个位数表示plot_number。 这意味着:可以写subplot(234)来代替subplot(2, 3, 4) 。

在下面的示例中,我们实现一个2x2网格的两个子图:

import matplotlib.pyplot as plt
plt.figure(figsize=(6, 4))
plt.subplot(221) # equivalent to: plt.subplot(2, 2, 1)
plt.text(0.5, # x coordinate, 0 leftmost positioned, 1 rightmost
         0.5, # y coordinate, 0 topmost positioned, 1 bottommost
         'subplot(2,2,1)', # the text which will be printed (2,2,1即:2行,2列,第1个个位置)
         horizontalalignment='center', # shortcut 'ha'
         verticalalignment='center', # shortcut 'va'
         fontsize=20, # can be named 'font' as well
         alpha=.7 # float (0.0 transparent through 1.0 opaque)
         )
python_course_green = "#90EE90"
# 224即:2行,2列,第4个位置
plt.subplot(224, facecolor=python_course_green)
plt.text(0.5, 0.5,
         'subplot(2,2,4)',
         ha='center', va='center',
         fontsize=20,
         color="b")

plt.show()

在这里插入图片描述

如果不需要在轴上设置刻度,可以将它们设置为空元组,并添加以下代码行:

plt.xticks(())
import matplotlib.pyplot as plt
plt.figure(figsize=(6, 4))
plt.subplot(221) # equivalent to: plt.subplot(2, 2, 1)
plt.xticks(())
plt.yticks(())
plt.text(0.5, # x coordinate, 0 leftmost positioned, 1 rightmost
         0.5, # y coordinate, 0 topmost positioned, 1 bottommost
         'subplot(2,2,1)', # the text which will be printed
         horizontalalignment='center', # shortcut 'ha'
         verticalalignment='center', # shortcut 'va'
         fontsize=20, # can be named 'font' as well
         alpha=.7 # float (0.0 transparent through 1.0 opaque)
         )
python_course_green = "#90EE90"
plt.subplot(224, facecolor=python_course_green)
plt.xticks(())
plt.yticks(())
plt.text(0.5, 0.5,
         'subplot(2,2,4)',
         ha='center', va='center',
         fontsize=20,
         color="b")
plt.show()

在这里插入图片描述

我们还可以使用Figure类的实例即使用面向对象的方法。
下面重写前面的例子来展示。 在这种情况下,将add_subplot方法应用于Figure对象。

import matplotlib.pyplot as plt
fig = plt.figure(figsize=(6, 4))
sub1 = fig.add_subplot(221) # equivalent to: plt.subplot(2, 2, 1)
sub1.text(0.5, # x coordinate, 0 leftmost positioned, 1 rightmost
          0.5, # y coordinate, 0 topmost positioned, 1 bottommost
          'subplot(2,2,1)', # the text which will be printed
          horizontalalignment='center', # shortcut 'ha'
          verticalalignment='center', # shortcut 'va'
          fontsize=20, # can be named 'font' as well
          alpha=.7 # float (0.0 transparent through 1.0 opaque)
          )
python_course_green = "#90EE90"
sub2 = fig.add_subplot(224, facecolor=python_course_green)
sub2.text(0.5, 0.5,
          'subplot(2,2,4)',
          ha='center', va='center',
          fontsize=20,
          color="b")
plt.show()

在这里插入图片描述

让我们再次去除刻度。 这次不能使用plt.xticks(())和plt.yticks(())。 而必须使用set_xticks(())和set_yticks(())方法。

import matplotlib.pyplot as plt
fig = plt.figure(figsize=(6, 4))
sub1 = fig.add_subplot(221) # equivalent to: plt.subplot(2, 2, 1)
sub1.set_xticks([]) 
sub1.set_yticks([]) 
sub1.text(0.5, # x coordinate, 0 leftmost positioned, 1 rightmost
          0.5, # y coordinate, 0 topmost positioned, 1 bottommost
          'subplot(2,2,1)', # the text which will be printed
          horizontalalignment='center', # shortcut 'ha' 
          verticalalignment='center', # shortcut 'va'
          fontsize=20, # can be named 'font' as well
          alpha=.5 # float (0.0 transparent through 1.0 opaque)
          )
sub2 = fig.add_subplot(224, facecolor=python_course_green)
sub2.set_xticks([])
sub2.set_yticks([]) 
python_course_green = "#90EE90"
sub2.text(0.5, 0.5, 
          'subplot(2,2,4)', 
          ha='center', va='center',
          fontsize=20, 
          color="b")
plt.show()

在这里插入图片描述
前面的示例仅显示如何创建subplot设计。 下面我们演示如何使用一些真实的图填充先前的子图设计:

import numpy as np
from numpy import e, pi, sin, exp, cos
import matplotlib.pyplot as plt
def f(t):
    return exp(-t) * cos(2*pi*t)
def fp(t):
    return -2*pi * exp(-t) * sin(2*pi*t) - e**(-t)*cos(2*pi*t)
def g(t):
    return sin(t) * cos(1/(t+0.1))
def g(t):
    return sin(t) * cos(1/(t))
fig = plt.figure(figsize=(6, 4))
t = np.arange(-5.0, 1.0, 0.1)
sub1 = fig.add_subplot(221) # instead of plt.subplot(2, 2, 1)
sub1.set_title('The function f') # non OOP: plt.title('The function f')
sub1.plot(t, f(t))
sub2 = fig.add_subplot(222, facecolor="lightgrey")
sub2.set_title('fp, the derivation of f')
sub2.plot(t, fp(t))
t = np.arange(-3.0, 2.0, 0.02)
sub3 = fig.add_subplot(223)
sub3.set_title('The function g')
sub3.plot(t, g(t))
t = np.arange(-0.2, 0.2, 0.001)
sub4 = fig.add_subplot(224, facecolor="lightgrey")
sub4.set_title('A closer look at g')
sub4.set_xticks([-0.2, -0.1, 0, 0.1, 0.2])
sub4.set_yticks([-0.15, -0.1, 0, 0.1, 0.15])
sub4.plot(t, g(t))
plt.plot(t, g(t))
plt.tight_layout()
plt.show()

在这里插入图片描述

另外一个例子:

import  matplotlib.pyplot as plt
X = [ (2,1,1), (2,3,4), (2,3,5), (2,3,6) ]
for nrows, ncols, plot_number in X:
    plt.subplot(nrows, ncols, plot_number)
plt.show()

在这里插入图片描述

以下示例没有任何特殊之处。 我们将去除xticks并改变figure和subplots的大小。 为此,引入figure的关键字参数figsize和函数subplot_adjust及其关键字参数bottom, left,top, right:

import  matplotlib.pyplot as plt
fig =plt.figure(figsize=(6,4))
fig.subplots_adjust(bottom=0.025, left=0.025, top = 0.975, right=0.975)
X = [ (2,1,1), (2,3,4), (2,3,5), (2,3,6) ]
for nrows, ncols, plot_number in X:
    sub = fig.add_subplot(nrows, ncols, plot_number)
    sub.set_xticks([])
    sub.set_yticks([])
plt.show()

在这里插入图片描述

15.2.替代解决方案:

由于需要合并2x3网格的前三个子图,因此可以选择元组表示,在(2,3,(1,3))中(1,3) 含义为一个2x3网格的前三个元素进行了合并:

import  matplotlib.pyplot as plt
fig =plt.figure(figsize=(6,4))
fig.subplots_adjust(bottom=0.025, left=0.025, top = 0.975, right=0.975)
X = [ (2,3,(1,3)), (2,3,4), (2,3,5), (2,3,6) ]
for nrows, ncols, plot_number in X:
    sub = fig.add_subplot(nrows, ncols, plot_number)
    sub.set_xticks([])
    sub.set_yticks([])
plt.show()

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

涂作权的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值