如何使用缺失值matplotlib创建3D曲面图?

时间:2015-04-30 20:45:56

标签: python matplotlib 3d surface

我正在尝试创建一个3D表面能量图,其中网格上的x,y位置包含相关的z水平。问题是网格不均匀(即,每个x,y位置都没有z分量)。有没有办法通过在数组中的相应位置调用NaN来避免绘制这些值?

这是我到目前为止所尝试的内容:

import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import pylab
from matplotlib import cm

#Z levels 
energ = np.array([0,3.5,1,-0.3,-1.5,-2,-3.4,-4.8])
#function for getting x,y associated z values?
def fun(x,y,array):

    return array[x]

#arrays for grid
x = np.arange(0,7,0.5)
y = np.arange(0,7,0.5) 

#create grid
X, Y = np.meshgrid(x,y)
zs = np.array([fun(x,y,energ) for x in zip(np.ravel(X))])
Z = zs.reshape(X.shape)

plt3d = plt.figure().gca(projection='3d')

#gradients now with respect to x and y, but ideally with respect to z only
Gx, Gz = np.gradient(X * Y)  
G = (Gx ** 2 + Gz ** 2) ** .5  # gradient magnitude
N = G / G.max()  # normalize 0..1

plt3d.plot_surface(X, Y, Z, rstride=1, cstride=1,
               facecolors=cm.jet(N), edgecolor='k', linewidth=0, antialiased=False, shade=False)
plt.show()

我无法在此处发布此图的图像,但如果您运行代码,您将看到它 但是我想不绘制某些x,y对,所以这个数字应该向下三角形到最小值。这可以通过使用nan值来实现吗?还希望每个级别之间的间距,通过线连接。

n = np.NAN
#energ represents the z levels, so the overall figure should look like a triangle.
energ = np.array([[0,0,0,0,0,0,0,0,0,0,0,0,0],[n,n,n,n,n,n,n,n,n,n,n,n,n],[n,2.6,n,2.97,n,2.6,n,2.97,n,2.6,n,3.58,n],[n,n,n,n,n,n,n,n,n,n,n,n,n],[n,n,1.09,n,1.23,n,1.09,n,1.23,n,1.7,n,n],[n,n,n,n,n,n,n,n,n,n,n,n,n],[n,n,n,-0.65,n,-0.28,n,-0.65,n,0.33,n,n,n],[n,n,n,n,n,n,n,n,n,n,n,n,n],[n,n,n,n,-2.16,n,-2.02,n,-1.55,n,n,n,n],[n,n,n,n,n,n,n,n,n,n,n,n,n],[n,n,n,n,n,-3.9,n,-2.92,n,n,n,n,n,],[n,n,n,n,n,n,n,n,n,n,n,n,n],[n,n,n,n,n,n,-4.8,n,n,n,n,n,n,]])

plt3d = plt.figure().gca(projection='3d')

Gx, Gz = np.gradient(X * energ)  # gradients with respect to x and z
G = (Gx ** 2 + Gz ** 2) ** .5  # gradient magnitude
N = G / G.max()  # normalize 0..1

x = np.arange(0,13,1)
y = np.arange(0,13,1) 
X, Y = np.meshgrid(x,y)
#but the shapes don't seem to match up

plt3d.plot_surface(X, Y, energ, rstride=1, cstride=1,
               facecolors=cm.jet(N), edgecolor='k',
               linewidth=0, antialiased=False, shade=False
) 

使用掩码数组会产生以下错误:local Python [7155]:void CGPathCloseSubpath(CGMutablePathRef):no current point。

n = np.NAN
energ = np.array([[0,0,0,0,0,0,0,0,0,0,0,0,0],[n,n,n,n,n,n,n,n,n,n,n,n,n],[n,2.6,n,2.97,n,2.6,n,2.97,n,2.6,n,3.58,n],[n,n,n,n,n,n,n,n,n,n,n,n,n],[n,n,1.09,n,1.23,n,1.09,n,1.23,n,1.7,n,n],[n,n,n,n,n,n,n,n,n,n,n,n,n],[n,n,n,-0.65,n,-0.28,n,-0.65,n,0.33,n,n,n],[n,n,n,n,n,n,n,n,n,n,n,n,n],[n,n,n,n,-2.16,n,-2.02,n,-1.55,n,n,n,n],[n,n,n,n,n,n,n,n,n,n,n,n,n],[n,n,n,n,n,-3.9,n,-2.92,n,n,n,n,n,],[n,n,n,n,n,n,n,n,n,n,n,n,n],[n,n,n,n,n,n,-4.8,n,n,n,n,n,n,]])
x = np.arange(0,13,1)
y = np.arange(0,13,1) 
X, Y = np.meshgrid(x,y)

#create masked arrays
mX = ma.masked_array(X, mask=[[0,0,0,0,0,0,0,0,0,0,0,0,0],[1,1,1,1,1,1,1,1,1,1,1,1,1],[1,0,1,0,1,0,1,0,1,0,1,0,1],[1,1,1,1,1,1,1,1,1,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ot_surface(mX, mY, m_energ, rstride=1, cstride=1, edgecolor='k', linewidth=0, antialiased=False, shade=False)
plt.show()

0 个答案:

没有答案
相关问题