mplot3d:contourf 偏移量、限制和刻度
mplot3d: contourf offset, limits and ticks
我正在尝试在 mplot3d 曲面下绘制一个漂亮的 contourf 图。
我希望它出现在 3d 轴立方体的地板上,与我的数据下限有一点偏移。
现在我正在做这样的事情:
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()))
我正在尝试在 mplot3d 曲面下绘制一个漂亮的 contourf 图。 我希望它出现在 3d 轴立方体的地板上,与我的数据下限有一点偏移。 现在我正在做这样的事情:
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()))