简体   繁体   English

轮廓和轮廓f的使用

[英]use of contour and contourf

I have a list of points:我有一个要点列表:

pointList = [ [x1,y1,z1], [x2,y2,z2], ... [xn,yn,zn]]

and I want to draw a contour plot of this set of points.我想绘制这组点的等高线图。

I try:我尝试:

import matplotlib.pyplot as plt
pointList = [ [x1,y1,z1], [x2,y2,z2], ... [xn,yn,zn]]
x = [el[0] for el in pointList]
y = [el[1] for el in pointList]
z = [el[2] for el in pointList]
plt.contourf(x,y,z)
plt.show()

but I have this exception:但我有这个例外:

TypeError: Input z must be a 2D array.

This is strange because in the documentation of matplotlib I find:这很奇怪,因为在 matplotlib 的文档中我发现:

Call signatures:
contour(Z)
make a contour plot of an array Z. The level values are chosen automatically.
contour(X,Y,Z)
X, Y specify the (x, y) coordinates of the surface

So I don't understand why it fails ...所以我不明白为什么它失败了......

In either case, contour(Z) , or contour(X,Y,Z) , the input Z must be a 2D array.在任何一种情况下, contour(Z)contour(X,Y,Z) ,输入Z必须是二维数组。

If your data does not live on a grid, you either need to interpolate it to a grid or you cannot use contour.如果您的数据不在网格上,您要么需要将其插入到网格中,要么不能使用等高线。

An easy alternative is to use tricontour .一个简单的替代方法是使用tricontour

import matplotlib.pyplot as plt
import numpy as np
pointList = [ [x1,y1,z1], [x2,y2,z2], ... [xn,yn,zn]]
pointList = np.array(pointList)
plt.tricontour(pointList[:,0],pointList[:,1],pointList[:,2])
plt.show()

There is a good example which compares tricontour with a contour of interpolated data: tricontour_vs_griddata .有一个很好的示例将tricontour与插值数据的contour进行比较: tricontour_vs_griddata

You may also look at:您还可以查看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM