使用pyplot创建绘图网格

2024-05-29 04:30:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python新手,在使用pyplot绘制时遇到了一些困难。我的目标是在Juypter笔记本中绘制一个网格状的线(%pylab inline)。

我编写了一个函数plot_CV,它在一些x的多项式次数上绘制交叉验证erorr,其中交叉绘制的惩罚程度(lambda)应该是变化的。最终lambda中有10个元素,它们由plot_CV中的第一个参数控制。所以

fig = plt.figure()
ax1 = fig.add_subplot(1,1,1) 
ax1 = plot_CV(1,CV_ve=CV_ve)

给予

enter image description here

现在我想我必须使用add_subplot来创建一个绘图网格,如

fig = plt.figure()
ax1 = fig.add_subplot(2,2,1)
ax1 = plot_CV(1,CV_ve=CV_ve)
ax2 = fig.add_subplot(2,2,2)
ax2 = plot_CV(2,CV_ve=CV_ve)
ax3 = fig.add_subplot(2,2,3)
ax3 = plot_CV(3,CV_ve=CV_ve)
ax4 = fig.add_subplot(2,2,4)
ax4 = plot_CV(4,CV_ve=CV_ve)
plt.show()

enter image description here

但是,如果我继续这样做,那么绘图会越来越小,并开始在x和y标签上重叠。这是一张3乘3的图。

enter image description here

有没有一种方法可以使这些图的空间均匀分布,这样它们就不会重叠,并更好地利用Jupyter笔记本中的水平和垂直行内空间?为了说明这一点,jupyter提供了一张截图:

enter image description here

最后注意:我仍然需要添加一个标题或注释,该标题或注释的当前lambda级别在plot_CV中使用。


编辑:使用建议的紧凑布局,给出:

enter image description here


编辑2:使用fig.set_figheightfig.set_figwidth我最终可以使用可用的全长和高度。

enter image description here


Tags: lambdaadd网格绘图plotfig绘制ve

热门问题