2016-02-17 6 views
5

Ich möchte eine Oberfläche mit einem Colormap, Drahtmodell und Konturen mit matplotlib plotten. Etwas wie folgt aus:Oberfläche und 3D-Kontur in Matplotlib

enter image description here

Beachten Sie, dass ich nicht über die Konturen bin zu stellen, die in der Ebene parallel liegen, aber die, die auf xy, die 3D und weiß im Bild sind.

Wenn ich die naive Art gehe und all diese Dinge plotte, kann ich die Konturen nicht sehen (siehe Code und Bild unten).

import numpy as np 
from mpl_toolkits.mplot3d import axes3d 
import matplotlib.pyplot as plt 

fig = plt.figure() 
ax = fig.add_subplot(111, projection="3d") 
X, Y = np.mgrid[-1:1:30j, -1:1:30j] 
Z = np.sin(np.pi*X)*np.sin(np.pi*Y) 
ax.plot_surface(X, Y, Z, cmap="autumn_r", lw=0.5, rstride=1, cstride=1) 
ax.contour(X, Y, Z, 10, lw=3, cmap="autumn_r", linestyles="solid", offset=-1) 
ax.contour(X, Y, Z, 10, lw=3, colors="k", linestyles="solid") 
plt.show() 

enter image description here

Wenn eine Transparenz Facetten auf die Oberfläche hinzufügen, dann kann ich die Konturen sehen, aber es sieht wirklich überladen (siehe Code und Bild unten)

import numpy as np 
from mpl_toolkits.mplot3d import axes3d 
import matplotlib.pyplot as plt 

fig = plt.figure() 
ax = fig.add_subplot(111, projection="3d") 
X, Y = np.mgrid[-1:1:30j, -1:1:30j] 
Z = np.sin(np.pi*X)*np.sin(np.pi*Y) 
ax.plot_surface(X, Y, Z, cmap="autumn_r", lw=0.5, rstride=1, cstride=1, alpha=0.5) 
ax.contour(X, Y, Z, 10, lw=3, cmap="autumn_r", linestyles="solid", offset=-1) 
ax.contour(X, Y, Z, 10, lw=3, colors="k", linestyles="solid") 
plt.show() 

enter image description here

Frage: Gibt es eine Möglichkeit, dieses Ergebnis inzu erhalten? Die Schattierung ist jedoch nicht notwendig.

Antwort

0

Ich glaube, Sie den Offset an die Kontur festlegen möchten:

ax.contour(X, Y, Z, 10, offset=-1, lw=3, colors="k", linestyles="solid", alpha=0.5) 

Sehen Sie dieses Beispiel für mehr:

http://matplotlib.org/examples/mplot3d/contour3d_demo3.html

Und die docs hier:

http://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html#contour-plots

Offset: Wenn angegeben Plot eine Projektion der Konturlinien auf dieser Position in normaler Ebene

Hinweis zdir, zdir = 'z' standardmäßig, aber Sie können in der X- oder Y-Richtung projizieren die zdir entsprechend einstellen werden.

+0

Nicht wirklich, das wird die Kontur in 2D plotten . Aber nicht in 3D, wie ich es möchte. Siehe das Beispielbild, es hat weiße Konturen. – nicoguaro

+1

Ah ok, tut mir leid, ich habe die Frage missverstanden – tom

2

Offenbar ist es ein Fehler, wenn Sie diese versuchen

import numpy as np 
from mpl_toolkits.mplot3d import axes3d 
import matplotlib.pyplot as plt 

fig = plt.figure() 
ax = fig.add_subplot(111, projection="3d") 
X, Y = np.mgrid[-1:1:30j, -1:1:30j] 
Z = np.sin(np.pi*X)*np.sin(np.pi*Y) 


ax.plot_surface(X, Y, Z, cmap="autumn_r", lw=0, rstride=1, cstride=1) 
ax.contour(X, Y, Z+1, 10, lw=3, colors="k", linestyles="solid") 
plt.show() 

und drehen sich um, sehen Sie die Konturlinien verschwinden, wenn sie nicht