我正试图在mplot3d曲面下获得一个漂亮的contourf图。我希望它出现在三维轴立方体的地板上,与我的数据下限有一点偏移。现在我正在做这样的事情:
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
N = 50
fig = plt.figure()
ax = fig.gca(projection='3d')
surface = np.zeros((N,N))
# gaussian
for x in np.arange(N, dtype=float):
for y in np.arange(N, dtype=float):
sigma = 0.2
xn = (x - N/2)/N
yn = (y - N/2)/N
r = np.sqrt(xn**2.0 + yn**2.0)
surface[x,y] = np.exp((-r**2.0)/(2.0*sigma**2.0))
# mesh grid NxN points in [0,1]
gx, gy = np.meshgrid(np.linspace(0,1,N),np.linspace(0,1,N))
ax.plot_surface(gx, gy, surface, rstride=2, cstride=2, cmap=mpl.cm.Spectral)
# extend z axis limit to make room for contourf
ax.set_zlim3d(np.min(surface) - 0.5, np.max(surface))
# contour on the floor
levels = np.linspace(np.min(surface), np.max(surface), 20)
ax.contourf(gx, gy, surface, levels=levels,
offset=(np.min(surface) - 0.5), cmap=mpl.cm.Spectral)
绘制此图像的
这看起来很好,但它添加了几个记号,我在数据最小值下扩展zaxis。我不想在最小值下显示任何刻度,但仍然扩展zaxis以偏移contourf图。
知道吗?如何隐藏或不绘制所有带红色圆圈的记号?
这比我想象的要容易,非常感谢@gboffi为我指出了正确的api。
s_min = np.min(surface)
s_max = np.max(surface)
# filter out extra ticks that exceed data limits
ax.set_zticks(filter(lambda x: s_min <= x <= s_max, ax.get_zticks()))