使用 pyplot 创建绘图网格 [英] Using pyplot to create grids of plots

查看:64
本文介绍了使用 pyplot 创建绘图网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的新手,在使用 pyplot 进行绘图时遇到了一些困难.我的目标是在 Juypter Notebook 中绘制一个内嵌网格 (%pylab inline).

I am new to python and having some difficulties with plotting using pyplot. My goal is to plot a grid of plots in-line (%pylab inline) in Juypter Notebook.

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

I programmed a function plot_CV which plots cross-validation erorr over the degree of polynomial of some x where across plots the degree of penalization (lambda) is supposed to vary. Ultimately there are 10 elements in lambda and they are controlled by the first argument in plot_CV. So

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

给予

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

Now I think I have to use add_subplot to create a grid of plots as in

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()

但是,如果我继续执行此操作,则图将变得越来越小,并开始在x和y标签上重叠.这是一张带有 3 x 3 绘图的图片.

If I continue this, however, then the plots get smaller and smaller and start to overlap on the x and y labels. Here a picture with a 3 by 3 plot.

是否有一种方法可以均匀分布图,以使它们不重叠并且更好地利用Jupyter Notebook中的水平和垂直在线空间?为了说明这一点,请使用 jupyter 的屏幕截图:

Is there a way to space the plots evenly, so that they do not overlap and make better use of the horizontal and vertical in-line space in Jupyter Notebook? To illustrate this point here a screenshot from jupyter:

最后一点:我仍然需要添加标题或注释,其中包含 plot_CV 中使用的当前lambda级别.

Final note: I still need to add a title or annotation with the current level of lambda used in plot_CV.

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

Using the tight layout as suggested, gives:

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

Edit 2: Using the fig.set_figheight and fig.set_figwidth I could finally use the full length and heigth available.

推荐答案

针对您的问题的第一个建议是查看"matplotlib 的 rel="nofollow noreferrer">Tight Layout guide".

The first suggestion to your problem would be taking a look at the "Tight Layout guide" for matplotlib.

他们有一个看起来在外观上与您的情况非常相似的示例.此外,他们还提供了考虑轴标签和绘图标题的示例和建议.

They have an example that looks visually very similar to your situation. As well they have examples and suggestions for taking into consideration axis labels and plot titles.

此外,您还可以通过使用 matplotlib.figure 类中的Figure来控制整体图形大小.

Furthermore you can control the overall figure size by using Figure from the matplotlib.figure class.

Figure(figsize = (x,y))

figsize: x,y (inches)

这是我从 matplotlib 网站中提取并添加到以下内容中的示例:

Here is an example that I pulled from the matplotlib website and added in the:

fig.set_figheight(15)
fig.set_figwidth(15)

示例:

import matplotlib.pyplot as plt

plt.rcParams['savefig.facecolor'] = "0.8"

def example_plot(ax, fontsize=12):
     ax.plot([1, 2])
     ax.locator_params(nbins=3)
     ax.set_xlabel('x-label', fontsize=fontsize)
     ax.set_ylabel('y-label', fontsize=fontsize)
     ax.set_title('Title', fontsize=fontsize)

plt.close('all')
fig = plt.figure()

fig.set_figheight(15)
fig.set_figwidth(15)


ax1 = plt.subplot2grid((3, 3), (0, 0))
ax2 = plt.subplot2grid((3, 3), (0, 1), colspan=2)
ax3 = plt.subplot2grid((3, 3), (1, 0), colspan=2, rowspan=2)
ax4 = plt.subplot2grid((3, 3), (1, 2), rowspan=2)

example_plot(ax1)
example_plot(ax2)
example_plot(ax3)
example_plot(ax4)

plt.tight_layout()

您可以通过以下方式使用tight_layout实现子图的填充:

You can achieve padding of your subplots by using tight_layout this way:

plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)

这样,您可以防止子图彼此之间进一步拥挤.

That way you can keep your subplots from crowding each other even further.

祝你好运!

这篇关于使用 pyplot 创建绘图网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆