欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 产业 > python画图|灵活的subplot_mosaic()函数-初逢

python画图|灵活的subplot_mosaic()函数-初逢

2025/5/17 2:44:10 来源:https://blog.csdn.net/weixin_44855046/article/details/143624449  浏览:    关键词:python画图|灵活的subplot_mosaic()函数-初逢

【1】引言

前述学习进程中,对hist()函数画直方图已经有一定的探索。然而学无止境,在继续学习的进程中,我发现了一个显得函数subplot_mosaic(),它几乎支持我们随心所欲地排布多个子图。

经过自我探索,我有一些收获,写在这里和大家一起继续学习。

【2】官网教程

首先是进入官网,点击下方链接直达:

Complex and semantic figure composition (subplot_mosaic) — Matplotlib 3.9.2 documentation

在这里,我们看到先用常规plot方法画出了两行两列的子图,然后调用subplot_mosaic()输出了同样结果,为此我们做一下代码解读。

【3】代码解读

在plot画子图的过程中,调用了text()函数,为避免偏离主题和带来疑惑,我们主要集中精力探索下述代码段:

fig = plt.figure(layout="constrained")
ax_dict = fig.subplot_mosaic(
    [
        ["bar", "plot"],
        ["hist", "image"],
    ],
)
ax_dict["bar"].bar(["a", "b", "c"], [5, 7, 9])
ax_dict["plot"].plot([1, 2, 3])
ax_dict["hist"].hist(hist_data)
ax_dict["image"].imshow([[1, 2], [2, 1]])
identify_axes(ax_dict)

在这里我们看到定义了一个fig.subplot_mosaic()函数,函数包括四个小项"bar"、"plot"、"hist"和"image",这四个小项提前占好了位置,分别是:

"bar":[1,1]

"plot":[1,2]

"hist":[2,1]

"image":[2,2]

真正执行将fig.subplot_mosaic()函数中的小项输出的操作是:identify_axes(ax_dict)。

接下来我们回到identify_axes(ax_dict)函数的具体定义:

def identify_axes(ax_dict, fontsize=48):"""Helper to identify the Axes in the examples below.Draws the label in a large font in the center of the Axes.Parameters----------ax_dict : dict[str, Axes]Mapping between the title / label and the Axes.fontsize : int, optionalHow big the label should be.""" kw = dict(ha="center", va="center", fontsize=fontsize, color="darkgrey")for k, ax in ax_dict.items():ax.text(0.5, 0.5, k, transform=ax.transAxes, **kw)

在这里, 使用dict()函数进行了赋值操作,虽然这个函数看起来很奇怪,但是将其功能直接理解为赋值操作即可:

之后使用k和ax在ax_dict 也就是 fig.subplot_mosaic()函数里面遍历,实现将fig.subplot_mosaic()函数里面的小项输出。

运行代码后的输出图像为:

图1

至此的完整代码为:

import matplotlib.pyplot as plt  #引入画图模块
import numpy as np #引入计算模块
np.random.seed(19680801) #定义随机数种子
hist_data = np.random.randn(1_500) #定义随机数数组# Helper function used for visualization in the following examples
def identify_axes(ax_dict, fontsize=48):   #自定义函数,该函数调用ax_dict作为输入参数"""Helper to identify the Axes in the examples below.Draws the label in a large font in the center of the Axes.Parameters----------ax_dict : dict[str, Axes]Mapping between the title / label and the Axes.fontsize : int, optionalHow big the label should be."""kw = dict(ha="center", va="center", fontsize=fontsize, color="darkgrey") #dict()也是一个函数,就理解为是一个赋值操作即可for k, ax in ax_dict.items():#让k和ax在ax.dict()这个数组里面遍历ax.text(0.5, 0.5, k, transform=ax.transAxes, **kw) #调用text()函数将ax_dict中的项输出
fig = plt.figure(layout="constrained")
ax_dict = fig.subplot_mosaic([["bar", "plot"],["hist", "image"],],)
ax_dict["bar"].bar(["a", "b", "c"], [5, 7, 9]) #调用bar()函数输出直方图
ax_dict["plot"].plot([1, 2, 3]) #调用plot()函数输出直线
ax_dict["hist"].hist(hist_data) #调用hist()函数输出频数分布图
ax_dict["image"].imshow([[1, 2], [2, 1]]) #调用imshow()函数填充区域
identify_axes(ax_dict) #调用自定义函数
plt.show() #输出图形

【4】代码修改

在前述内容中,fig.subplot_mosaic()函数将子项分成了两行两列,接下来我们将其改为一行:

ax_dict = fig.subplot_mosaic([["bar", "plot","hist","image"],#[ "image"],],)

此时的输出图像为:

图2

然后再改为一列:

ax_dict = fig.subplot_mosaic([["bar"],["plot"],["hist"],["image"],],)

此时的输出图像为:

图3

可见,通过定义fig.subplot_mosaic()函数子项的位置,可以直接影响subplot()子图的位置。

【5】总结

初步学习了fig.subplot_mosaic()函数设置多子图位置的方法。

 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词