matplotlib多图布局

import matplotlib.pyplot as plt
import numpy as np

# plt.rcParms['axes.unicode_minus'] = False




        '''subplot()'''
        #2行2列
        fig=plt.figure(figsize=(6,4),dpi=170)


        x=np.linspace(-np.pi,np.pi,30)
        y=np.sin(x)

        #子图
        ax1=plt.subplot(221)#2行 2列 第1 个图
        ax1.plot(x,y)
        ax1.set_title('image-1')


        ax2=plt.subplot(222)#2行 2列 第2个图
        ax2.plot(x,np.cos(x))
        ax2.set_title('image-2')

        ax3=plt.subplot(223)#2行 2列 第3 个图
        ax3.plot(x,-np.sin(x))
        ax3.set_title('image-3')

        ax4=plt.subplot(224)#2行 2列 第4 个图
        ax4.plot(x,-np.cos(x))
        ax4.set_title('image_4')

        # 自动调整布局
        fig.tight_layout()


        plt.show()

第二行显示一张图片


#多子图布局

import matplotlib.pyplot as plt
import numpy as np

# plt.rcParms['axes.unicode_minus'] = False



plt.figure(figsize=(8,5),dpi=100)

x=np.linspace(-np.pi,np.pi,30)
y=np.sin(x)

ax1=plt.subplot(221)
ax1.plot(x,y)
ax1.set_title('image-1')

ax2=plt.subplot(2,2,2)
ax2.plot(x,np.cos(x))

ax3=plt.subplot(2,1,2) #2行1列中的第2行
ax3.plot(x,np.sin(x*x))

plt.show()

subplots()函数


#多子图布局

import matplotlib.pyplot as plt
import numpy as np

# plt.rcParms['axes.unicode_minus'] = False




x=np.linspace(0,2*np.pi)

#3行3列
fig,axs=plt.subplots(3,3)

ax1,ax2,ax3 = axs #一维数组


ax11,ax12,ax13=ax1
ax21,ax22,ax23=ax2
ax31,ax32,ax33=ax3

#设置画布大小
fig.set_figwidth(8)
fig.set_figheight(5)



#调整布局,紧凑布局
plt.tight_layout()

#显示
plt.show()

#多子图布局

import matplotlib.pyplot as plt
import numpy as np

# plt.rcParms['axes.unicode_minus'] = False




x=np.linspace(0,2*np.pi)

#3行3列
fig,axs=plt.subplots(3,3)

ax1,ax2,ax3 = axs #一维数组


ax11,ax12,ax13=ax1
ax21,ax22,ax23=ax2
ax31,ax32,ax33=ax3

#设置画布大小
fig.set_figwidth(8)
fig.set_figheight(5)

#绘制图像
ax11.plot(x,np.sin(x))
ax12.plot(x,np.cos(x))
ax13.plot(x,x*x)

ax21.plot(x,np.tan(x))
ax22.plot(x,-np.cos(x))
ax23.plot(x,x)

ax31.plot(np.sin(x),np.cos(x))
ax32.plot(x,np.sin(x+30))
ax33.plot(x,np.cos(x+30))


#调整布局,紧凑布局
plt.tight_layout()

#显示
plt.show()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于matplotlib排版,你可以使用subplot,gridspec或subplots来实现。 1. 使用subplot函数: subplot函数允许您在一个Figure对象中创建多个子。它接受三个参数:行数、列数和子索引。 以下是一个简单的例子,展示如何在一个Figure中创建2x2的子布局: ```python import matplotlib.pyplot as plt # 创建Figure对象,并设置子布局为2行2列 fig = plt.figure() # 创建子1,并设为第1个位置 ax1 = fig.add_subplot(2, 2, 1) ax1.plot([1, 2, 3, 4], [1, 4, 2, 3]) # 创建子2,并设为第2个位置 ax2 = fig.add_subplot(2, 2, 2) ax2.plot([1, 2, 3, 4], [4, 2, 3, 1]) # 创建子3,并设为第3个位置 ax3 = fig.add_subplot(2, 2, 3) ax3.plot([1, 2, 3, 4], [3, 1, 4, 2]) # 创建子4,并设为第4个位置 ax4 = fig.add_subplot(2, 2, 4) ax4.plot([1, 2, 3, 4], [2, 3, 1, 4]) # 显示形 plt.show() ``` 2. 使用gridspec模块: gridspec模块提供了更灵活的子布局选项。您可以使用gridspec.GridSpec将Figure分割成不规则的网格,并在每个网格中放置子。 以下是一个示例代码,展示如何使用gridspec实现2x2的子布局: ```python import matplotlib.pyplot as plt from matplotlib import gridspec # 创建Figure对象 fig = plt.figure() # 使用gridspec将Figure分割成2行2列 gs = gridspec.GridSpec(2, 2) # 在第1行第1列创建子 ax1 = fig.add_subplot(gs[0, 0]) ax1.plot([1, 2, 3, 4], [1, 4, 2, 3]) # 在第1行第2列创建子 ax2 = fig.add_subplot(gs[0, 1]) ax2.plot([1, 2, 3, 4], [4, 2, 3, 1]) # 在第2行第1列创建子 ax3 = fig.add_subplot(gs[1, 0]) ax3.plot([1, 2, 3, 4], [3, 1, 4, 2]) # 在第2行第2列创建子 ax4 = fig.add_subplot(gs[1, 1]) ax4.plot([1, 2, 3, 4], [2, 3, 1, 4]) # 显示形 plt.show() ``` 3. 使用subplots函数: subplots函数可以同时创建多个子,并返回一个包含所有子的Figure对象和Axes对象的数组。 以下是一个示例代码,展示如何使用subplots函数创建2x2的子布局: ```python import matplotlib.pyplot as plt # 使用subplots函数创建2x2的子布局 fig, axs = plt.subplots(2, 2) # 在第1行第1列创建子 axs[0, 0].plot([1, 2, 3, 4], [1, 4, 2, 3]) # 在第1行第2列创建子 axs[0, 1].plot([1, 2, 3, 4], [4, 2, 3, 1]) # 在第2行第1列创建子 axs[1, 0].plot([1, 2, 3, 4], [3, 1, 4, 2]) # 在第2行第2列创建子 axs[1, 1].plot([1, 2, 3, 4], [2, 3, 1, 4]) # 调整子之间的间距 plt.tight_layout() # 显示形 plt.show() ``` 这些是matplotlib中几种常见的多排版方法。您可以根据需要选择适合您的情况的方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值