Matplotlib v0.99 Surface with contour: zdir doesn't work
I am trying to plot function f(x,y)=(x+2)*y^2 with some iso level curves projected on x-y plane. The code I used is this:
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FixedLocator, FormatStrFormatter
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import axes3d, Axes3D
import pylab as p
fig = plt.figure()
ax = Axes3D(fig)
X = np.arange(0, 2.5, 0.1)
Y = np.arange(0, 2.5, 0.1)
X, Y = np.meshgr开发者_StackOverflow中文版id(X, Y)
Z = ((X+2))*(Y**2)
surf = ax.plot_surface(X, Y, Z,rstride=1, cstride=1, alpha=0.3, cmap=cm.jet)
cset=plt.contour(X,Y,Z,zdir='z',offset=0)
ax.clabel(cset, fontsize=9, inline=1)
ax.set_zlim3d(0, 30)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()
My problem is that zdir doesn't work, that is the contour lines are on the surface and not on x-y plane.
Any ideas? Thanks in advance
Your code works fine for me (matplotlib 1.0.1).
Btw: It seems that this example was added to gallery with matplotlib 1.0. Maybe this is a problem the previous version?
zdir
defines the direction to project. (zdir='x'
projects along the x axis) offset
defines the location of the plane to be projected onto (along the axis defined by zdir
)
Example
I'm going to guess what you want is:
cset=plt.contour(X,Y,Z,zdir='z',offset=30)
-> xs and ys: These are coordinates for x and y axes -> zs: This is the value(s) for z axis. It can be one for all points or one for each point --> zdir: This will choose what will be the z-axis dimension (usually this is zs, but can be xs or ys)
精彩评论