为什么此代码中需要numpy.ravel()才能产生较小的倍数? [英] Why is numpy.ravel() required in this code that produces small multiples?

查看:59
本文介绍了为什么此代码中需要numpy.ravel()才能产生较小的倍数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了一些代码来生成一组小倍数,并且运行良好.

  fig,axes = plt.subplots(6,3,figsize =(21,21))fig.subplots_adjust(hspace=.3, wspace=.175)对于 ax,zip 中的数据(axes.ravel(), clean_sets):ax.plot(data.ETo,o")

for ax, data in zip(axes.ravel(), clean_sets): contians .ravel() 但我不明白这实际上在做什么或为什么有必要.

如果我看看 解决方案

您的猜测是正确的. plt.subplots()返回一个由多个轴组成的 Axes numpy 数组,具体取决于输入.如果用参数 nrows ncols 定义2D网格,则返回的 numpy 数组也将是2D数组.

此行为在 pyplot.subplots squeeze 参数中的文档

<块引用>

squeeze :布尔,可选,默认值:True
如果为True,则从返回的Axes对象中挤出额外的尺寸:

<块引用>

  • 如果仅构造了一个子图(nrows = ncols = 1),则返回的单个Axes对象将作为标量返回.

  • 对于 Nx1 或 1xN 子图,返回的对象是一个 1D numpy 对象数组,Axes 对象作为 numpy 1D 数组返回.
  • 对于 NxM,N>1 和 M>1 的子图作为二维数组返回.
<块引用>

如果为 False,则根本不进行压缩:返回的 Axes 对象始终是包含 Axes 实例的二维数组,即使它最终是 1x1.

因为这里有 plt.subplots(6,3),因此 N>1, M>1,结果对象必然是一个 2D 数组,独立于 squeeze 设置为什么.

这使得有必要展平此数组,以便能够对其进行 zip 压缩.选项是

  • zip(axes.ravel())
  • zip(axes.flatten())
  • zip(axes.flat)

I found some code to generate a set of small multiples and it is working perfectly.

fig, axes = plt.subplots(6,3, figsize=(21,21))
fig.subplots_adjust(hspace=.3, wspace=.175)
for ax, data in zip(axes.ravel(), clean_sets):
    ax.plot(data.ETo, "o")

The line for ax, data in zip(axes.ravel(), clean_sets): contians .ravel() but I do not understand what this is actually doing or why it is necessary.

If I take a look at the docs I find the following:

Return a contiguous flattened array.

A 1-D array, containing the elements of the input, is returned. A copy is made only if needed.

I guess the return that corresponds to axes from plt.subplot() is a multidimensional array that can't be iterated over, but really I'm not sure. A simple explanation would be greatly appreciated.


What is the purpose of using .ravel() in this case?

解决方案

Your guess is correct. plt.subplots() returns either an Axes or a numpy array of several axes, depending on the input. In case a 2D grid is defined by the arguments nrows and ncols, the returned numpy array will be a 2D array as well.

This behaviour is explained in the pyplot.subplots documentation inside the squeeze argument,

squeeze : bool, optional, default: True
If True, extra dimensions are squeezed out from the returned Axes object:

  • if only one subplot is constructed (nrows=ncols=1), the resulting single Axes object is returned as a scalar.

  • for Nx1 or 1xN subplots, the returned object is a 1D numpy object array of Axes objects are returned as numpy 1D arrays.
  • for NxM, subplots with N>1 and M>1 are returned as a 2D arrays.

If False, no squeezing at all is done: the returned Axes object is always a 2D array containing Axes instances, even if it ends up being 1x1.

Since here you have plt.subplots(6,3) and hence N>1, M>1, the resulting object is necessarily a 2D array, independent of what squeeze is set to.

This makes it necessary to flatten this array in order to be able to zip it. Options are

  • zip(axes.ravel())
  • zip(axes.flatten())
  • zip(axes.flat)

这篇关于为什么此代码中需要numpy.ravel()才能产生较小的倍数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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