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数组

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

下一篇:粉色围巾

相关内容

热门资讯

幼儿园元旦文艺汇演主持词 男小主持:尊敬的家长,亲爱的老师女小主持:可爱的小朋友合:大家新年好!男小主持:春夏秋冬,黑夜清晨女...
大话西游降妖篇2台词 大话西游降妖篇2台词  导语:《西游伏妖篇》也是继春节档周星驰执导电影《美人鱼》中徐克客串表演之后,...
晚会活动主持词   引导语:晚会最重要的一点就是主持,而有关晚会活动的主持词要怎么写呢?接下来是小编为你带来收集整理...
周年庆活动主持词 周年庆活动主持词9篇  借鉴诗词和散文诗是主持词的一种写作手法。在人们越来越多的参与各种活动的今天,...
《手机》经典台词 《手机》经典台词  砖头媳妇:装得跟头会想事的猪一样。  于文娟:老费吃了不管用,说明他不是不能,而...
公司工会代表大会主持词 公司工会代表大会主持词  各位代表:  请大家坐好,会议马上就要开始了,公司工会代表大会主持词。(待...
影视剧里那些讲完就领便当的台... 关于影视剧里那些讲完就领便当的台词  无论是什么类型的影视作品,片中的角色在将死之前大都会变得不太一...
高三毕业典礼主持词 高三毕业典礼主持词15篇  主持词的写作需要将主题贯穿于所有节目之中。在如今这个时代,活动集会越来越...
班会主持词 班会主持词(精选12篇)  根据活动对象的不同,需要设置不同的主持词。在如今这个时代,各种集会中主持...
学术研讨会主持词 学术研讨会主持词  什么是主持词  由主持人于节目进行过程中串联节目的串联词。如今的各种演出活动和集...
小学音乐会的主持稿 小学音乐会的主持稿范文  开场舞:群舞《仰望五星红旗》  叶XX:感谢14位同学给我们带来的精彩演出...
元旦主持词舞蹈串词 元旦主持词舞蹈串词  开场语:  华:尊敬的各位领导、各位来宾  蒋:亲爱的老师、同学们  杨:大家...
六一儿童节晚会主持词 六一儿童节晚会主持词(精选7篇)  随着六一儿童的临近,幼儿园在紧锣密鼓的为幼儿排练着各大节目,作为...
电视剧版《红高粱》中的经典台... 电视剧版《红高粱》中的经典台词  九儿:高粱下到锅里,蒸熟发酵大火灼烧,把酒气逼出来就成了酒了,你觉...
新学期欢迎新教师致辞 新学期欢迎新教师致辞(精选22篇)  在学习、工作乃至生活中,大家总免不了要接触或使用致辞吧,致辞要...
在新天地商场招商发布会上的致... 在新天地商场招商发布会上的致辞  在我们平凡的日常里,要用到致辞的情况还是蛮多的,致辞具有针对性,要...
主持词感恩的心开场白   主持词感恩的心开场白一  尊敬的各位领导、各位嘉宾、各位老师、亲爱的家长、同学们:  大家下午好...
签字仪式主持词 签字仪式主持词范文  篇一:项目签约仪式主持词  签约仪式主持词  尊敬的xxx,各位领导、各位来宾...
晚宴致辞 晚宴致辞(精选15篇)  在平平淡淡的学习、工作、生活中,要用到致辞的情况还是蛮多的,致辞受场合、事...
幼儿园大班毕业生家长会主持词 幼儿园大班毕业生家长会主持词(精选7篇)  主持词需要富有情感,充满热情,才能有效地吸引到观众。在各...