简体   繁体   English

mplot3d:轮廓线偏移量,限制和刻度

[英]mplot3d: contourf offset, limits and ticks

I'm trying to get a nice contourf plot under a mplot3d surface. 我试图在mplot3d表面下得到一个不错的轮廓图。 I'd want it to appear on the floor of the 3d axis cube with a little offset from my data lower limits. 我希望它出现在3d轴立方体的地板上,与我的数据下限有一点偏移。 Right now I'm doing something like this: 现在我正在做这样的事情:

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)

That plots this image 绘制此图像

This looks fine but it adds a couple of ticks where I extend the zaxis under the data minimum. 看起来不错,但在我将zaxis扩展到数据最小值以下的位置时增加了几个刻度。 I'd like to not show any tick under the minimum but still extend the zaxis to offset the contourf plot. 我不想在最小值以下不显示任何刻度线,但仍要扩展z轴以抵消轮廓线图。

Any idea? 任何想法? How do I hide or not draw at all the red circled ticks? 如何隐藏或不绘制所有带有红色圆圈的刻度线?

It was easier than I thought, many thanks to @gboffi for pointing me to the correct apis. 它比我想象的要容易,非常感谢@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()))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM