matplotlib实现一页多图

时间:2022-07-24
本文章向大家介绍matplotlib实现一页多图,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

在matplotlib中,实现一页多图有以下两种方式

1. 直接指定

2. 动态增加

直接指定是在创建figure的时候,就直接定义好多个axes的排列方式;动态增加则是根据需要在figure上添加axes。下面来具体看下用法

1. 直接指定

直接指定可以通过pyplot子模块的subplots函数来实现,基本用法如下

>>> fig, axes = plt.subplots()
>>> axes
<matplotlib.axes._subplots.AxesSubplot object at 0x0A149E68>

>>> fig, axes = plt.subplots(2,2)
>>> axes
array([[<matplotlib.axes._subplots.AxesSubplot object at 0x0106DBE0>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x01087580>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x01099808>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x010BB1F0>]],
      dtype=object)

>>> axes[0, 0]
<matplotlib.axes._subplots.AxesSubplot object at 0x0106DBE0>
>>> axes[0, 1]
<matplotlib.axes._subplots.AxesSubplot object at 0x01087580>
>>> axes[1, 0]
<matplotlib.axes._subplots.AxesSubplot object at 0x01099808>
>>> axes[1, 1]
<matplotlib.axes._subplots.AxesSubplot object at 0x010BB1F0>

不添加任何参数的情况下,生成包含了1个axes的figure对象,也可以通过指定nrows和ncols参数来生成多个axes的figure,然后通过数组下标来访问对应的axes对象。实际使用中,可以通过解列表的形式提高编码效率,代码如下

>>> fig, (ax1, ax2) = plt.subplots(1, 2)
>>> fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)

这种方式生成的每个axes都是大小相同的,代码如下

>>> fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
>>> ax1.plot([1,2,3,4])
>>> ax2.bar(x = [1, 2, 3, 4], height = [4, 2, 3, 1])
>>> ax3.hist(x = np.random.normal(size=1000), bins=50)
>>> ax4.scatter(x= np.random.randn(10), y=np.random.randn(10),s=40 * np.arange(10),c=np.random.randn(10))
>>> plt.show()

输出结果如下

2. 动态指定

动态指定是在生成新的axes对象的同时,指定其排列的位置,通过pyplot子模块的subplot函数来实现,用法如下

>>> ax1 = plt.subplot(221)
>>> ax2 = plt.subplot(222)
>>> ax3 = plt.subplot(223)
>>> ax4 = plt.subplot(224)
>>>
>>> ax1.plot([1,2,3,4])
>>> ax2.bar(x = [1, 2, 3, 4], height = [4, 2, 3, 1])
>>> ax3.hist(x = np.random.normal(size=1000), bins=50)
>>> ax4.scatter(x= np.random.randn(10), y=np.random.randn(10),s=40 * np.arange(10),c=np.random.randn(10))
>>> plt.show()

上述代码的输出结果和直接指定方式的输出是完全一样的,subplot方法中,前两个数字分别表示布局的行数和列数,第三个数值表示的是索引。

另外,还有一种更加灵活的布局方式,支持不同axes占据不同区域,通过pyplot子模块的subplot2grid函数来实现,用法如下

>>> ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=2)
>>> ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=2, rowspan=2)
>>> ax3 = plt.subplot2grid((3, 3), (1, 2), rowspan=2)
>>>
>>> ax1.bar(x = [1, 2, 3, 4], height = [4, 2, 3, 1])
>>> ax2.scatter(x= np.random.randn(10), y=np.random.randn(10),s=40 * np.arange(10),c=np.random.randn(10))
>>> ax3.hist(x = np.random.normal(size=1000), bins=50)
>>>
>>> plt.show()

输出结果如下

第一个元组表示布局的行数和列数,第二个元组表示axes的数组下标,colspan和rowspan分别指定该axes占据的行数和列数。为不同的axes指定不同的colspan和rowspan参数,从而实现不同axes不同大小的目的。

本质上来讲,一页多图的方式都是首先将一个figure按照固定的行列划分为相等大小的区域,只不过一个是直接利用划分好的相等区域来画图,一个则另外指定了axes覆盖的相等区域的个数,,然后再进行画图而已。

figuse size的大小直接影响axes区域的大小,除此之外,设置绘图区域在figure上的范围,以及划分的相等区域的水平和垂直间隔,也会影响最终的画图区域的大小,可以通过subplots_adjust函数进行设置,用法如下

>>> fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
>>> plt.subplots_adjust(left=0.25, right=0.9, bottom=0.1, top=0.9, wspace=0.2, hspace=0.2)

将figure视为左下角为(0,0), 右上角为(1, 1)的坐标系,通过left等参数分别指定上下左右的坐标,从而设置绘图区域的大小。wspace和hspace参数分别指定分割线的宽度和高度,其数值为每个axes width和height的比例。

通过pyplot的subplots系列函数,可以轻松实现一页多图,当然,在matplotlib中,还有其他方法来实现一页多图,具体的可以查看官方的帮助文档。

·end·

—如果喜欢,快分享给你的朋友们吧—

原创不易,欢迎收藏,点赞,转发!生信知识浩瀚如海,在生信学习的道路上,让我们一起并肩作战!

本公众号深耕耘生信领域多年,具有丰富的数据分析经验,致力于提供真正有价值的数据分析服务,擅长个性化分析,欢迎有需要的老师和同学前来咨询。