为什么pyplot.contour()要求Z为2D数组? [英] Why does pyplot.contour() require Z to be a 2D array?
问题描述
matplotlib.pyplot.contour()
函数采用3个输入数组X
,Y
和Z
.
数组X
和Y
指定点的x和y坐标,而Z
指定在点处求值的感兴趣函数的相应值.
The matplotlib.pyplot.contour()
function takes 3 input arrays X
, Y
and Z
.
The arrays X
and Y
specify the x- and y-coordinates of points, while Z
specifies the corresponding value of the function of interest evaluated at the points.
我了解到np.meshgrid()
使得生成contour()
的参数数组变得容易:
I understand that np.meshgrid()
makes it easy to produce arrays which serve as arguments to contour()
:
X = np.arange(0,5,0.01)
Y = np.arange(0,3,0.01)
X_grid, Y_grid = np.meshgrid(X,Y)
Z_grid = X_grid**2 + Y_grid**2
plt.contour(X_grid, Y_grid, Z_grid) # Works fine
这很好.方便地,这也很好:
This works fine. And conveniently, this works fine too:
plt.contour(X, Y, Z_grid) # Works fine too
但是,为什么Z
输入 是二维数组?
However, why is the Z
input required to be a 2D-array?
为什么即使指定了所有正确对齐的相同数据,也不允许以下内容?
Why is something like the following disallowed, even though it specifies all the same data aligned appropriately?
plt.contour(X_grid.ravel(), Y_grid.ravel(), Z_grid.ravel()) # Disallowed
还有,当仅指定 Z
(没有对应的X
和Y
)时的语义是什么?
Also, what are the semantics when only Z
is specified (without the corresponding X
and Y
)?
推荐答案
Looking at the documentation of contour
one finds that there are a couple of ways to call this function, e.g. contour(Z)
or contour(X,Y,Z)
. So you'll find that it does not require any X
or Y
values to be present at all.
但是,为了绘制轮廓,函数必须知道下面的网格. Matplotlib的contour
基于矩形网格.但是即使这样,允许contour(z)
且z
是一维数组,也将使我们无法知道应如何绘制该字段.在contour(Z)
的情况下,其中Z
是2D数组,其形状明确地设置了绘图的网格.
However in order to plot a contour, the underlying grid must be known to the function. Matplotlib's contour
is based on a rectangular grid. But even so, allowing contour(z)
, with z
being a 1D array, would make it impossible to know how the field should be plotted. In the case of contour(Z)
where Z
is a 2D array, its shape unambiguously sets the grid for the plot.
一旦知道了网格,是否可选的X
和Y
数组是否被展平就无关紧要;实际上,文档告诉我们的是:
Once that grid is known, it is rather unimportant whether optional X
and Y
arrays are flattened or not; which is actually what the documentation tells us:
X和Y必须都为与Z形状相同的2D,或者它们都必须为1D,以使len(X)是Z中的列数,而len(Y)是行数在Z中.
X and Y must both be 2-D with the same shape as Z, or they must both be 1-D such that len(X) is the number of columns in Z and len(Y) is the number of rows in Z.
很明显,有点像
plt.contour(X_grid.ravel(), Y_grid.ravel(), Z_grid.ravel())
无法生成等高线图,因为有关网格形状的所有信息都将丢失,并且等高线函数无法知道如何解释数据.例如.如果len(Z_grid.ravel()) == 12
,则基础网格的形状可以是(1,12), (2,6), (3,4), (4,3), (6,2), (12,1)
中的任何一个.
It is also pretty obvious that someting like
plt.contour(X_grid.ravel(), Y_grid.ravel(), Z_grid.ravel())
cannot produce a contour plot, because all the information about the grid shape is lost and there is no way the contour function could know how to interprete the data. E.g. if len(Z_grid.ravel()) == 12
, the underlying grid's shape could be any of (1,12), (2,6), (3,4), (4,3), (6,2), (12,1)
.
一种可能的解决方法当然是允许一维数组并引入参数shape
,例如plt.contour(x,y,z, shape=(6,2))
.但是事实并非如此,因此您必须忍受Z
必须是2D的事实.
A possible way out could of course be to allow for 1D arrays and introduce an argument shape
, like plt.contour(x,y,z, shape=(6,2))
. This however is not the case, so you have to live with the fact that Z
needs to be 2D.
但是,如果您正在寻找一种获得具有平坦(散乱的)数组的计数图的方法,则可以使用
However, if you are looking for a way to obtain a countour plot with flattened (ravelled) arrays, this is possible using plt.tricontour()
.
plt.tricontour(X_grid.ravel(), Y_grid.ravel(), Z_grid.ravel())
此处,将使用Delaunay三角剖分在内部生成一个三角形网格.因此,即使完全随机化的点也将产生不错的结果,如下图所示,将其与contour
赋予的相同随机点进行比较.
Here a triangular grid will be produced internally using a Delaunay Triangualation. Therefore even completely randomized points will produce a nice result, as can be seen in the following picture, where this is compared to the same random points given to contour
.
(这里是生成此图片的代码)
这篇关于为什么pyplot.contour()要求Z为2D数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!