解决方案将取决于数据的组织方式。
规则网格上的数据
If the x
and y
数据已经定义了网格,它们可以轻松地重新整形为四边形网格。例如。
#x y z
4 1 3
6 1 8
8 1 -9
4 2 10
6 2 -1
8 2 -8
4 3 8
6 3 -9
8 3 0
4 4 -1
6 4 -8
8 4 8
可以绘制为contour http://matplotlib.org/devdocs/api/_as_gen/matplotlib.axes.Axes.contour.html using
import matplotlib.pyplot as plt
import numpy as np
x,y,z = np.loadtxt("data.txt", unpack=True)
plt.contour(x.reshape(4,3), y.reshape(4,3), z.reshape(4,3))
任意数据
(a)如果数据不在四边形网格上,则可以在网格上插入数据。 matplotlib 本身提供了一种方法,使用matplotlib.mlab.griddata
.
import matplotlib.mlab
xi = np.linspace(4, 8, 10)
yi = np.linspace(1, 4, 10)
zi = matplotlib.mlab.griddata(x, y, z, xi, yi, interp='linear')
plt.contour(xi, yi, zi)
(b)最后,无需使用四边形网格即可完全绘制轮廓。这可以使用以下方法完成tricontour http://matplotlib.org/devdocs/api/_as_gen/matplotlib.axes.Axes.tricontour.html.
plt.tricontour(x,y,z)
比较后两种方法的示例位于matplotlib 页面 http://matplotlib.org/examples/pylab_examples/tricontour_vs_griddata.html.