plt.contour在Python中

时间:2016-06-05 08:21:15

标签: python matplotlib

我想制作一个2D轮廓图给出3个相同大小的numpy数组 X Y Z,坐标为(X,Y),Z为(X,Y)处的值。

现在,函数plt.contour似乎仅适用于网格和网格交叉处的值,并且仅将二维矩阵作为输入作为输入。

请参阅http://matplotlib.org/examples/pylab_examples/contour_demo.html

如果该函数的输入是(XX,YY,ZZ),那么XX是一个2D矩形数组,其中每一行都是相同的,每列都是常量,而YY是一个2D矩形数组,每列都相同,并且每一行都是不变的。

现在,如果我想在输入值不是在网格上取值时制作轮廓图,我应该使用什么功能,或者我应该遵循什么程序呢?

感谢

1 个答案:

答案 0 :(得分:1)

尝试使用plt.tricontourhttp://matplotlib.org/api/pyplot_api.html?highlight=tricontour#matplotlib.pyplot.tricontour

它在非结构化三角形网格上绘制轮廓。

小例子:

import numpy as np
import matplotlib.pyplot as plt
x = np.random.rand(100)
y = np.random.rand(100)
z = x**2+np.sin(y)*y
f, ax = plt.subplots(1,1)
ax.tricontour(x,y,z)
ax.plot(x,y, 'r. ')
plt.show()

enter image description here