python中的轮廓图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15601096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Contour graph in python
提问by apkdsmith
How would I make a countour grid in python using matplotlib.pyplot, where the grid is one colour where the zvariable is below zero and another when zis equal to or larger than zero? I'm not very familiar with matplotlibso if anyone can give me a simple way of doing this, that would be great.
我将如何使用 python 在 python 中制作一个计数网格matplotlib.pyplot,其中网格是一种颜色,其中z变量低于零,另一种颜色z等于或大于零?我不是很熟悉,matplotlib所以如果有人能给我一个简单的方法,那就太好了。
So far I have:
到目前为止,我有:
x= np.arange(0,361)
y= np.arange(0,91)
X,Y = np.meshgrid(x,y)
area = funcarea(L,D,H,W,X,Y) #L,D,H and W are all constants defined elsewhere.
plt.figure()
plt.contourf(X,Y,area)
plt.show()
采纳答案by tom10
You can do this using the levelskeyword in contourf.
您可以使用levelscontourf 中的关键字来执行此操作。


import numpy as np
import matplotlib.pyplot as plt
fig, axs = plt.subplots(1,2)
x = np.linspace(0, 1, 100)
X, Y = np.meshgrid(x, x)
Z = np.sin(X)*np.sin(Y)
levels = np.linspace(-1, 1, 40)
zdata = np.sin(8*X)*np.sin(8*Y)
cs = axs[0].contourf(X, Y, zdata, levels=levels)
fig.colorbar(cs, ax=axs[0], format="%.2f")
cs = axs[1].contourf(X, Y, zdata, levels=[-1,0,1])
fig.colorbar(cs, ax=axs[1])
plt.show()
You can change the colors by choosing and different colormap; using vmin, vmax; etc.

