gpt4 book ai didi

python - 如果仅使用两个级别,Matplotlib 轮廓剖面线将不起作用

转载 作者:行者123 更新时间:2023-12-01 08:12:50 25 4
gpt4 key购买 nike

我正在尝试在轮廓线上绘制阴影根据发现的示例统计某些标准 here 。然而,我得到了规则的轮廓(黄线)而不是阴影。任何想法如何解决这个问题。谢谢 enter image description here

import matplotlib.pyplot as plt
import numpy as np
# invent some numbers, turning the x and y arrays into simple
# 2d arrays, which make combining them together easier.
x = np.linspace(-3, 5, 150).reshape(1, -1)
y = np.linspace(-3, 5, 120).reshape(-1, 1)
z = np.cos(x) + np.sin(y)

# we no longer need x and y to be 2 dimensional, so flatten them.
x, y = x.flatten(), y.flatten()
fig2, ax2 = plt.subplots()
n_levels = 6
a=ax2.contourf(x, y, z, n_levels)
fig2.colorbar(a)
[m,n]=np.where(z > 0.5)
z1=np.zeros(z.shape)
z1[m,n]=99
cs = ax2.contour(x, y, z1,2,hatches=['','.'])
plt.show()enter code here

最佳答案

使用 contourf() 和适当的参数来获得有用的阴影图。请参阅下面工作代码中的重要注释:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-3, 5, 150).reshape(1, -1)
y = np.linspace(-3, 5, 120).reshape(-1, 1)
z = np.cos(x) + np.sin(y)

x, y = x.flatten(), y.flatten()

fig2, ax2 = plt.subplots()
n_levels = 6
a = ax2.contourf(x, y, z, n_levels)

fig2.colorbar(a)
[m,n] = np.where(z > 0.5)

z1=np.zeros(z.shape)
z1[m, n] = 99

# use contourf() with proper hatch pattern and alpha value
cs = ax2.contourf(x, y, z1 ,3 , hatches=['', '..'], alpha=0.25)

plt.show()

输出图:

enter image description here

关于python - 如果仅使用两个级别,Matplotlib 轮廓剖面线将不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55133513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com