subplots()--matplotlib
创始人
2024-01-17 01:17:13
0

1. 函数功能

成一个画布和若干子区。

2. 函数语法

matplotlib.pyplot.subplots(nrows=1, ncols=1, *, sharex=False, sharey=False, squeeze=True, subplot_kw=None, 
gridspec_kw=None, **fig_kw)

3. 函数参数与示例

参数含义
nrows, ncols画布被分成的行、列数
squeeze布尔值,是否压缩维度,默认取值为True:当取值为True时,1. 若只创建一个子区,则返回子区是一个标量;2. 若是N1或者1M个子区,则返回1维子区对象;3. 若为N*M(N>1且M>1)则返回二维数组。当取值为False时,总是返回二维数组
width_ratios每一列的相对宽度,默认每列宽度相等,可以指定宽度: width_ratios[i] / sum(width_ratios)
height_ratios每一行的相对高度,默认每列高度相等,可以指定宽度: height_ratios[i] / sum(height_ratios)
subplot_kw定义子区属性的关键字参数,如:绘图区域的背景颜色: facecolor
gridspec_kw
**fig_kw传递给figure对象的其他关键字

3.1 nrows与ncols

import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mplmpl.rcParams['font.sans-serif'] = ['KaiTi']
mpl.rcParams['axes.unicode_minus'] = Falsefig, ax = plt.subplots(2, 3, facecolor='y', alpha=0.3)
plt.show()

在这里插入图片描述

3.2 生成画布与1个绘图区域

3.2.1 squeeze=False

当squeeze参数取值为False,不压缩维度时,虽然只有一个绘图区域,绘图区域ax的返回结果依然为二维数组,因此,设定绘图区域的内容需要使用行列索引

import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mplmpl.rcParams['font.sans-serif'] = ['KaiTi']
mpl.rcParams['axes.unicode_minus'] = Falsex = np.linspace(0, 2 * np.pi, 500)
y = np.sin(x) * np.cos(x)fig, ax = plt.subplots(1, 1, squeeze=False,subplot_kw=dict(facecolor='cornflowerblue', alpha=0.3))
ax[0,0].plot(x, y, ls='--', color='k', lw=2)
ax[0,0].set_xlabel('时间(s)')
ax[0,0].set_ylabel('振幅')
ax[0,0].set_title('简单折线图')
ax[0,0].grid(ls=':', lw=1, color='gray', alpha=0.8)
plt.show()

在这里插入图片描述

3.2.2 squeeze=True

当squeeze参数取值为True,压缩数组维度,产生一个绘图区域时返回一个标量,此时可以使用ax

import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mplmpl.rcParams['font.sans-serif'] = ['KaiTi']
mpl.rcParams['axes.unicode_minus'] = Falsex = np.linspace(0, 2 * np.pi, 500)
y = np.sin(x) * np.cos(x)fig, ax = plt.subplots(1, 1, squeeze=True,subplot_kw=dict(facecolor='cornflowerblue', alpha=0.3))
ax.plot(x, y, ls='--', color='k', lw=2)
ax.set_xlabel('时间(s)')
ax.set_ylabel('振幅')
ax.set_title('简单折线图')
ax.grid(ls=':', lw=1, color='gray', alpha=0.8)
plt.show()

3.3 生成画布与N1个子区或1M个子区

3.3.1 N*1个子区

当squeeze取值为False时,ax返回二维数组;当squeeze取值为True时,ax返回一维数组。

import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mplmpl.rcParams['font.sans-serif'] = ['KaiTi']
mpl.rcParams['axes.unicode_minus'] = Falsex = np.linspace(0, 2 * np.pi, 500)
y = np.sin(x) * np.cos(x)fig, ax = plt.subplots(2, 1, squeeze=True,subplot_kw=dict(facecolor='lightgreen', alpha=0.3))
ax[1].plot(x, y, ls='--', color='k', lw=2)
ax[0].set_xlabel('时间(s)')
ax[0].set_ylabel('振幅')
ax[0].set_title('简单折线图')
ax[0].grid(ls=':', lw=1, color='gray', alpha=0.8)
plt.show()

在这里插入图片描述

3.3.2 1*M个子区

import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mplmpl.rcParams['font.sans-serif'] = ['KaiTi']
mpl.rcParams['axes.unicode_minus'] = Falsex = np.linspace(0, 2 * np.pi, 500)
y = np.sin(x) * np.cos(x)
y1 = x
fig, ax = plt.subplots(1, 3, squeeze=True,subplot_kw=dict(facecolor='lightgreen', alpha=0.3))
ax[1].plot(x, y, ls='--', color='k', lw=2)
ax[0].set_xlabel('时间(s)')
ax[0].set_ylabel('振幅')
ax[0].set_title('简单折线图')
ax[2].plot(x, y1, ls=':', c='r')
ax[2].grid(ls=':', lw=1, color='gray', alpha=0.8)
plt.show()

在这里插入图片描述

3.4 N*M个子区(N>1,M>1)

import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mplmpl.rcParams['font.sans-serif'] = ['KaiTi']
mpl.rcParams['axes.unicode_minus'] = Falsex = np.linspace(0, 2 * np.pi, 500)
y = np.sin(x) * np.cos(x)
y1 = x
y2 = x ** 2
y3 = np.tan(x)
fig, ax = plt.subplots(2, 2, squeeze=False,subplot_kw=dict(facecolor='snow', alpha=0.3, polar=True))
ax[0, 0].plot(x, y, ls='--', color='k', lw=2)
ax[0, 1].plot(x, y1, c='r')
ax[1, 0].plot(x, y2, ls=':', c='g', lw=2)
ax[1, 1].plot(x, y3, c='m')
plt.show()

在这里插入图片描述

3.5 sharex与sharey取值为布尔值

3.5.1 sharex与sharey均为False

当sharex=Fasle;sharey=False,每个子区的坐标轴均相互独立
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mplmpl.rcParams['font.sans-serif'] = ['KaiTi']
mpl.rcParams['axes.unicode_minus'] = Falsex1 = np.linspace(0, 2 * np.pi, 500)
x = np.arange(0,10,0.5)
y = np.sin(x1) * np.cos(x1)
y1 = x1
y2 = x ** 2
y3 = np.tan(x)fig, ax = plt.subplots(2, 2, sharex=False, sharey=False)
ax[0, 0].plot(x1, y, ls='--', color='k', lw=2)
ax[0, 1].plot(x1, y1, c='r')
ax[1, 0].plot(x, y2, ls=':', c='g', lw=2)
ax[1, 1].plot(x, y3, c='m')
plt.show()

在这里插入图片描述

3.5.2 sharex=True,sharey=False

当sharex=True;sharey=False,每个子区共享x轴,每个子区的y轴相互独立
fig, ax = plt.subplots(2, 2, sharex=True, sharey=False)

在这里插入图片描述

3.5.3 sharex=False,sharey=True

当sharex=False;sharey=TRUE,每个子区x轴刻度相互独立,所有子区共享y轴
fig, ax = plt.subplots(2, 2, sharex=False, sharey=True)

在这里插入图片描述

3.5.4 sharex=TRUE,sharey=True

当sharex=False;sharey=TRUE,每个子区共享x轴,每个子区共享y轴
fig, ax = plt.subplots(2, 2, sharex=True, sharey=True)

在这里插入图片描述

3.5 总结

取值含义
sharex=True, sharey=True所有子区共享相同的x轴与y轴
sharex=True, sharey=False所有子区共享相同的x轴,每个子区使用各自独立的y轴
sharex=False, sharey=True每个子区使用各自独立的x轴, 所有子区共享相同的y轴
sharex=False, sharey=False每个子区均使用各自独立的x轴与y轴,默认情况

3.6 sharex与sharey取值为{‘none’, ‘all’, ‘row’, ‘col’}

3.6.1 sharex=‘none’ 且 sharey=‘none’

这种情况下相当于sharex=False,sharey=Fasle;每个子区的坐标轴均相互独立
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mplmpl.rcParams['font.sans-serif'] = ['KaiTi']
mpl.rcParams['axes.unicode_minus'] = Falsex1 = np.linspace(0, 2 * np.pi, 500)
y1 = x1
x2 = np.arange(0,10,0.5)
y2 = np.sin(x2) * np.cos(x2)
x3 = np.linspace(0,  np.pi, 500)
y3 = np.tan(x3)
x4 = np.arange(4,10,1)
y4 = x4**2fig, ax = plt.subplots(2, 2, sharex='none', sharey='none')
ax[0, 0].plot(x1, y1, ls='--', color='k', lw=2)
ax[0, 1].plot(x2, y2, c='r')
ax[1, 0].plot(x3, y3, ls=':', c='g', lw=2)
ax[1, 1].plot(x4, y4, c='m')
plt.show()

在这里插入图片描述

3.6.2 sharex=‘all’ 且 sharey=‘none’

这种情况下相当于sharex='all'  且 sharey='none';每个子区x轴的均使用相同刻度,最后一行显示刻度标签
fig, ax = plt.subplots(2, 2, sharex='all', sharey='none')

在这里插入图片描述

3.6.3 sharex=‘none’ 且 sharey=‘all’

这种情况下相当于sharex='none'  且 sharey='all';每个子区y轴的均使用相同刻度,第一列显示共同的刻度标签
fig, ax = plt.subplots(2, 2, sharex='none', sharey='all')

在这里插入图片描述

3.6.4 sharex=‘all’ 且 sharey=‘all’

 这种情况下相当于sharex='all'  且 sharey='all';每个子区x轴与y轴的均使用相同刻度,第一列与最后一行显示共同的刻度标签
fig, ax = plt.subplots(2, 2, sharex='all', sharey='all')

在这里插入图片描述

3.6.5 sharex=‘all’ 且 sharey=‘row’

此时所有子区的x轴共享,同一行的y轴共享
fig, ax = plt.subplots(2, 2, sharex='all', sharey='row')

在这里插入图片描述

3.6.6 sharex=‘all’ 且 sharey=‘col’

此时所有子区的x轴共享,同一列的y轴共享
fig, ax = plt.subplots(2, 2, sharex='all', sharey='col')

在这里插入图片描述

3.6.7 sharex=‘none’ 且 sharey=‘row’

每个子区使用独立的x轴,每行共享相同的y轴
fig, ax = plt.subplots(2, 2, sharex='none', sharey='row')

在这里插入图片描述

3.6.8 sharex=‘none’ 且 sharey=‘col’

每个子区使用独立的x轴,每列共享相同的y轴
fig, ax = plt.subplots(2, 2, sharex='none', sharey='col')

在这里插入图片描述

3.6.9 sharex=‘row’ 且 sharey=‘all’

每行子区使用相同的x轴,所有子区使用相同的y轴
fig, ax = plt.subplots(2, 2, sharex='row', sharey='all')

在这里插入图片描述

3.6.10 sharex=‘row’ 且 sharey=‘none’

每行子区使用相同的x轴,所有子区使用各自独立的y轴
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mplmpl.rcParams['font.sans-serif'] = ['KaiTi']
mpl.rcParams['axes.unicode_minus'] = Falsex1 = np.linspace(0, 2 * np.pi, 500)
y1 = x1
x2 = np.arange(0,10,0.5)
y2 = np.sin(x2) * np.cos(x2)
x3 = np.linspace(0,  np.pi, 500)
y3 = np.tan(x3)
x4 = np.arange(10,20,1)
y4 = x4**2fig, ax = plt.subplots(2, 2, sharex='row', sharey='none')
ax[0, 0].plot(x1, y1, ls='--', color='k', lw=2)
ax[0, 1].plot(x2, y2, c='r')
ax[1, 0].plot(x3, y3, ls=':', c='g', lw=2)
ax[1, 1].plot(x4, y4, c='m')
plt.show()

在这里插入图片描述

3.6.11 sharex=‘row’ 且 sharey=‘row’

每行子区使用相同的x轴,每行子区共享y轴
fig, ax = plt.subplots(2, 2, sharex='row', sharey='row')

在这里插入图片描述

3.6.12 sharex=‘row’ 且 sharey=‘col’

每行子区使用相同的x轴,每列子区共享y轴
fig, ax = plt.subplots(2, 2, sharex='row', sharey='col')

在这里插入图片描述

3.6.13 sharex=‘col’ 且 sharey=‘all’

每列共享x轴,所有子区共享y轴
fig, ax = plt.subplots(2, 2, sharex='col', sharey='all')

在这里插入图片描述

3.6.14 sharex=‘col’ 且 sharey=‘all’

每列共享x轴,每个子区独立使用各自的y轴
fig, ax = plt.subplots(2, 2, sharex='col', sharey='none')

在这里插入图片描述

3.6.15 sharex=‘col’ 且 sharey=‘row’

每列共享x轴,每行共享y轴
fig, ax = plt.subplots(2, 2, sharex='col', sharey='row')

在这里插入图片描述

3.6.16 sharex=‘col’ 且 sharey=‘col’

每列共享x轴,每列共享y轴
fig, ax = plt.subplots(2, 2, sharex='col', sharey='col')

在这里插入图片描述

3.6 总结

取值含义
sharex=‘all’, sharey=‘all’所有子区共享x轴与y轴
sharex=‘all’, sharey=‘none’所有子区共享x轴,每个子区独立使用各自的y轴
sharex=‘all’, sharey=‘row’所有子区共享x轴,每行子区共享y轴
sharex=‘all’, sharey=‘col’所有子区共享x轴,每列子区共享y轴
sharex=‘none’, sharey=‘all’每个子区独立使用各自的x轴,所有子区共享y轴
sharex=‘none’, sharey=‘none’每个子区独立使用各自的x轴与y轴
sharex=‘none’, sharey=‘row’每个子区独立使用各自的x轴,每行子区共享y轴
sharex=‘none’, sharey=‘col’每个子区独立使用各自的x轴,每列子区共享y轴
sharex=‘row’, sharey=‘all’每行子区共享x轴, 所有子区共享y轴
sharex=‘row’, sharey=‘none’每行子区共享x轴, 每个子区独立使用各自的y轴
sharex=‘row’, sharey=‘row’每行子区共享x轴, 每行子区共享y轴
sharex=‘row’, sharey=‘col’每行子区共享x轴, 每列子区共享y轴
sharex=‘col’, sharey=‘all’每列子区共享x轴, 所有子区共享y轴
sharex=‘col’, sharey=‘none’每列子区共享x轴,每个子区独立使用各自的y轴
sharex=‘col’, sharey=‘row’每列子区共享x轴, 每行子区共享y轴
sharex=‘col’, sharey=‘col’每列子区共享x轴, 每列子区共享y轴

4. 返回值

1). 4.1 figure画布
2). 4.2 axes数组

上一篇:几年后的“校友会”

下一篇:粉色围巾

相关内容

热门资讯

常用商务英语口语   商务英语是以适应职场生活的语言要求为目的,内容涉及到商务活动的方方面面。下面是小编收集的常用商务...
六年级上册英语第一单元练习题   一、根据要求写单词。  1.dry(反义词)__________________  2.writ...
复活节英文怎么说 复活节英文怎么说?复活节的英语翻译是什么?复活节:Easter;"Easter,anniversar...
2008年北京奥运会主题曲 2008年北京奥运会(第29届夏季奥林匹克运动会),2008年8月8日到2008年8月24日在中华人...
英语道歉信 英语道歉信15篇  在日常生活中,道歉信的使用频率越来越高,通过道歉信,我们可以更好地解释事情发生的...
六年级英语专题训练(连词成句... 六年级英语专题训练(连词成句30题)  1. have,playhouse,many,I,toy,i...
上班迟到情况说明英语   每个人都或多或少的迟到过那么几次,因为各种原因,可能生病,可能因为交通堵车,可能是因为天气冷,有...
小学英语教学论文 小学英语教学论文范文  引导语:英语教育一直都是每个家长所器重的,那么有关小学英语教学论文要怎么写呢...
英语口语学习必看的方法技巧 英语口语学习必看的方法技巧如何才能说流利的英语? 说外语时,我们主要应做到四件事:理解、回答、提问、...
四级英语作文选:Birth ... 四级英语作文范文选:Birth controlSince the Chinese Governmen...
金融专业英语面试自我介绍 金融专业英语面试自我介绍3篇  金融专业的学生面试时,面试官要求用英语做自我介绍该怎么说。下面是小编...
我的李老师走了四年级英语日记... 我的李老师走了四年级英语日记带翻译  我上了五个学期的小学却换了六任老师,李老师是带我们班最长的语文...
小学三年级英语日记带翻译捡玉... 小学三年级英语日记带翻译捡玉米  今天,我和妈妈去外婆家,外婆家有刚剥的`玉米棒上带有玉米籽,好大的...
七年级英语优秀教学设计 七年级英语优秀教学设计  作为一位兢兢业业的人民教师,常常要写一份优秀的教学设计,教学设计是把教学原...
我的英语老师作文 我的英语老师作文(通用21篇)  在日常生活或是工作学习中,大家都有写作文的经历,对作文很是熟悉吧,...
英语老师教学经验总结 英语老师教学经验总结(通用19篇)  总结是指社会团体、企业单位和个人对某一阶段的学习、工作或其完成...
初一英语暑假作业答案 初一英语暑假作业答案  英语练习一(基础训练)第一题1.D2.H3.E4.F5.I6.A7.J8.C...
大学生的英语演讲稿 大学生的英语演讲稿范文(精选10篇)  使用正确的写作思路书写演讲稿会更加事半功倍。在现实社会中,越...
VOA美国之音英语学习网址 VOA美国之音英语学习推荐网址 美国之音网站已经成为语言学习最重要的资源站点,在互联网上还有若干网站...
商务英语期末试卷 Part I Term Translation (20%)Section A: Translate ...