2013-03-24 6 views
13

Wie würde ich ein Countour-Gitter in Python mit matplotlib.pyplot, wo das Raster ist eine Farbe, wo die z Variable ist unter Null und eine andere, wenn z ist gleich oder größer als Null? Ich bin nicht sehr vertraut mit matplotlib, also wenn jemand mir eine einfache Möglichkeit geben kann, dies zu tun, wäre das großartig.Konturdiagramm in Python

Bisher habe ich:

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() 
+1

Welche Version von Python verwenden Sie (2 oder 3) – xxmbabanexx

Antwort

37

du das levels Schlüsselwort in contourf mit tun können.

enter image description here

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() 

Sie können die Farben ändern, indem Sie und andere colormap Wahl; mit vmin, vmax; etc.

+1

Prost! Das funktioniert super :) – apkdsmith

+5

Also bitte diese Antwort als Lösung markieren ... –

Verwandte Themen