Python matplotlib 通过单个列表迭代子图轴数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20288842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:06:06  来源:igfitidea点击:

matplotlib iterate subplot axis array through single list

pythonmatplotlib

提问by greole

Is there a simple/clean way to iterate an array of axis returned by subplots like

是否有一种简单/干净的方法来迭代由子图返回的轴数组,例如

nrow = ncol = 2
a = []
fig, axs = plt.subplots(nrows=nrow, ncols=ncol)
for i, row in enumerate(axs):
    for j, ax in enumerate(row):
        a.append(ax)

for i, ax in enumerate(a):
    ax.set_ylabel(str(i))

which even works for nrowor ncol == 1.

这甚至适用于nrowor ncol == 1

I tried list comprehension like:

我尝试过列表理解,例如:

[element for tupl in tupleOfTuples for element in tupl]

but that fails if nrowsor ncols == 1

但如果nrowsncols == 1

采纳答案by Bonlenfum

The axreturn value is a numpy array, which can be reshaped, I believe, without any copying of the data. If you use the following, you'll get a linear array that you can iterate over cleanly.

ax返回值是一个numpy的阵列,其可以,我相信,重新成形而不的数据的任何复制。如果您使用以下内容,您将获得一个可以干净地迭代的线性数组。

nrow = 1; ncol = 2;
fig, axs = plt.subplots(nrows=nrow, ncols=ncol)

for ax in axs.reshape(-1): 
  ax.set_ylabel(str(i))

This doesn't hold when ncols and nrows are both 1, since the return value is not an array; you could turn the return value into an array with one element for consistency, though it feels a bit like a cludge:

当 ncols 和 nrows 都为 1 时,这不成立,因为返回值不是数组;您可以将返回值转换为具有一个元素的数组以保持一致性,尽管感觉有点像杂乱:

nrow = 1; ncol = 1;
fig, axs = plt.subplots(nrows=nrow, ncols=nrow)
axs = np.array(axs)

for ax in axs.reshape(-1):
  ax.set_ylabel(str(i))

reshape docs. The argument -1causes reshape to infer dimensions of the output.

重塑文档。该参数-1导致 reshape 推断输出的维度。

回答by ?. Jensen

The figreturn value of plt.subplotshas a list of all the axes. To iterate over all the subplots in a figure you can use:

fig返回值plt.subplots具有所有轴的列表。要迭代图中的所有子图,您可以使用:

nrow = 2
ncol = 2
fig, axs = plt.subplots(nrow, ncol)
for i, ax in enumerate(fig.axes):
    ax.set_ylabel(str(i))

This also works for nrow == ncol == 1.

这也适用于nrow == ncol == 1.

回答by Mark

I am not sure when it was added, but there is now a squeezekeyword argument. This makes sure the result is always a 2D numpy array. Turning that into a 1D array is easy:

我不确定它是什么时候添加的,但现在有一个squeeze关键字参数。这确保结果始终是一个 2D numpy 数组。将其转换为一维数组很容易:

fig, ax2d = subplots(2, 2, squeeze=False)
axli = ax2d.flatten()

Works for any number of subplots, no trick for single ax, so a little easier than the accepted answer (perhaps squeezedidn't exist yet back then).

适用于任意数量的子图,单斧没有技巧,所以比接受的答案容易一点(squeeze当时可能还不存在)。

回答by Sukjun Kim

Matplotlib has its own flatten function on axes.

Matplotlib 在轴上有自己的展平功能。

Why don't you try following code?

你为什么不试试下面的代码?

fig, axes = plt.subplots(2, 3)
for ax in axes.flat:
    ## do something with instance of 'ax'