为什么 pyplot.contour() 要求 Z 是一个二维数组? [英] Why does pyplot.contour() require Z to be a 2D array?

查看:41
本文介绍了为什么 pyplot.contour() 要求 Z 是一个二维数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

matplotlib.pyplot.contour() 函数接受 3 个输入数组 XYZ.
数组 XY 指定点的 x 和 y 坐标,而 Z 指定感兴趣的函数在点.

我知道 np.meshgrid() 可以轻松生成用作 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**2plt.contour(X_grid, Y_grid, Z_grid) # 工作正常

这很好用.而且很方便,这也很好用:

plt.contour(X, Y, Z_grid) # 也很好用

但是,为什么 Z 输入 要求 是二维数组?

为什么不允许使用以下内容,即使它指定了所有相同的数据适当对齐?

plt.contour(X_grid.ravel(), Y_grid.ravel(), Z_grid.ravel()) # 不允许

另外,当指定only Z(没有相应的XY)时,语义是什么?

解决方案

(这是生成这张图片的代码)

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.

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

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

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.

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.

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 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.

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

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

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.

(Here is the code to produce this picture)

这篇关于为什么 pyplot.contour() 要求 Z 是一个二维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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