条件等高线图

时间:2018-04-25 10:49:21

标签: python matplotlib

以下代码是我现在正在玩的内容:

x = np.linspace(0,30,1000)
y = np.linspace(0,30,1000)
X,Y = np.meshgrid(x,y)

def f(x,y):
    return x**2 + y**2

Z = f(X,Y)


plt.contour(X, Y, Z, colors='black');

我希望这个情节显示一些禁区,比如f(x,y)< 9; 我想要这个阴影并添加到情节中。 我到底该怎么做?

我已尝试使用plt.contourf,但我无法让它发挥作用。

1 个答案:

答案 0 :(得分:2)

我认为您可以使用contourf这样做,使用contourf填充纯红色,然后使用等高线图屏蔽要显示的区域:

x = np.linspace(0,30,1000)
y = np.linspace(0,30,1000)
X,Y = np.meshgrid(x,y)

def f(x,y):
    return x**2 + y**2

Z = f(X,Y)

d = np.ma.array(Z, mask=Z>9)

plt.contour(X, Y, Z, colors='black')
plt.contourf(X, Y, d, colors='red');

输出:

enter image description here

相关问题