2012-06-13 51 views
8

我使用matplotlib 1.1.0绘制曲面。使用python,numpy和matplotlib绘制蒙面表面图

情节Z轴屏蔽像这样:

Zm = ma.masked_where((abs(z_grid) < 1.09) & (abs(z_grid) > 0.91), (z_surface)) 
surf = ax.plot_surface(X, Y,Zm, rstride=2, cstride=2, cmap=colors,linewidth=0, antialiased=False) 

但我没有看到应用对剧情的面具。我把这个面具本身作为一个子区域绘制出来

surf = ax.plot_surface(X, Y,ma.getmask(Zm), rstride=2, cstride=2, cmap=colors,linewidth=0, antialiased=False) 

其中的工作,所以我知道我的面具确实包含True值。

全码:

from pylab import * 
import matplotlib.pyplot as plt 
from matplotlib.widgets import Button 
import numpy 
from mpl_toolkits.mplot3d.axes3d import Axes3D 
from matplotlib import patches 
from matplotlib.figure import Figure 
from matplotlib import rcParams 


fig = plt.figure(figsize=plt.figaspect(0.5)) 
ax = fig.add_subplot(1, 2, 1,projection='3d') 

pole_positions_orig = [-0.6+0.73j]; 
zero_positions_orig = [0.29-0.41j]; 

surface_limit = 1.7; 
min_val = -surface_limit; 
max_val = surface_limit; 

surface_resolution = 0.0333; 

X = numpy.arange(min_val,max_val,surface_resolution) 
Y = numpy.arange(min_val,max_val,surface_resolution) 
X, Y = numpy.meshgrid(X, Y) 

z_grid = X + Y*1j; 
z_surface = z_grid*0; 

pole_positions = numpy.round(pole_positions_orig,1) + surface_resolution/2+(surface_resolution/2)*1j; 
zero_positions = numpy.round(zero_positions_orig,1) + surface_resolution/2 +(surface_resolution/2)*1j; 

for k in range(0, len(zero_positions)): 
    z_surface = z_surface + 20*log10((z_grid - zero_positions[k].real - zero_positions[k].imag*1j)); 
    z_surface = z_surface + 20*log10((z_grid - zero_positions[k].real + zero_positions[k].imag*1j)); 

for k in range(0, len(pole_positions)): 
    z_surface = z_surface - 20*log10((z_grid - pole_positions[k].real - pole_positions[k].imag*1j)); 
    z_surface = z_surface - 20*log10((z_grid - pole_positions[k].real + pole_positions[k].imag*1j));  


colors = cm.jet; 
colors.set_bad('k'); 


Zm = ma.masked_where((abs(z_grid) < 1.09) & (abs(z_grid) > 0.91), (z_surface)) 

z_surface = Zm; 

surf = ax.plot_surface(X, Y,z_surface, rstride=2, cstride=2, cmap=colors,linewidth=0, antialiased=False) 


ticks = [-1, 1]; 
z_ticks = [-30,-20,-10,0,10,20,30]; 
ax.set_xticks(ticks); 
ax.set_yticks(ticks); 
ax.set_zticks(z_ticks); 

ax.set_xlabel('Re') 
ax.set_ylabel('Im') 
ax.set_zlabel('Mag(db)',ha='left') 
plt.setp(ax.get_zticklabels(), fontsize=7) 
plt.setp(ax.get_xticklabels(), fontsize=7) 
plt.setp(ax.get_yticklabels(), fontsize=7) 

ax = fig.add_subplot(1, 2, 2,projection='3d') 
surf = ax.plot_surface(X, Y,ma.getmask(z_surface), rstride=2, cstride=2, cmap=colors,linewidth=0, antialiased=False) 

ax.grid(b=None); 
show(); 

这是我有:

python plot

这就是我想要的(从MATLAB):

matlab plot

什么时我错过了?

+0

所以屏蔽的数据仍然应该绘制,但以纯色? – fraxel

+0

是的,这是可能的吗? ...如果没有,那么只是没有绘制面具是一个很好的妥协。我用“colors.set_bad('k',alpha = 0.5)”来玩这个尝试,但它根本没有改变剧情。 – stanri

+0

不要认为它可能与掩盖,看起来像'plot_surface()'[不尊重蒙版](https://github.com/matplotlib/matplotlib/issues/487)。也许可以通过一个聪明的解决方法,但它在这个时候击败我:( – fraxel

回答

1

您可以做到这一点,但您需要手动为自己着色表面;

cmap函数需要一个介于0和1之间的nubmer,所以我们只需要在对它们调用cmap函数之前对值进行规范化。

z_surface = numpy.real(z_surface) 
min_z, max_z = z_surface.min(), z_surface.max() 
colours = numpy.zeros_like(z_surface, dtype=object) 

for i in range(len(z_surface)): 
    for j in range(len(z_surface[0])): 
    if 0.91 < numpy.sqrt(X[i,j]**2 + Y[i,j]**2) < 1.09: 
     colours[i,j] = "red" 
    else: 
     colours[i,j] = plt.get_cmap("jet")((z_surface[i,j]-min_z)/(max_z - min_z)) 


surf = ax.plot_surface(X, Y, z_surface, rstride=2, cstride=2, facecolors=colours, linewidth=0, antialiased=False) 

enter image description here

我还要指出的是,matplotlib是铸造你的ž阵列现实 - 你是否是故意的利用这一点,虽然我不知道。

12

Fraxel提到了surface_plot不支持遮罩。为了避开这个问题,这是我做的:

我基本上掩盖手动设置每个屏蔽值z轴数据numpy.nan像这样:

Zm = ma.masked_where((abs(z_grid) < 1.02) & (abs(z_grid) > 0.98), (z_surface)) 
z_surface[where(ma.getmask(Zm)==True)] = numpy.nan 

Cmap Broken

然而,它搞砸了我的色彩图缩放。为了解决这个问题,我这样做:

cmap = cm.jet 
lev = numpy.arange(-30,30,1); 
norml = colors.BoundaryNorm(lev, 256) 

surf = ax.plot_surface(X, Y, z_surface,...,norm = norml) 

Fixed

不是100%我想要的东西,而是一个很好的妥协仍然。