假设我们想要使用n-by-n
绘制Axes3d
网格网格。如果我们有n-by-n
标记矩阵包含0
和1
,我们如何根据3D图中的标注矩阵设置网格像素颜色?
答案 0 :(得分:1)
(的被修改强>)。有关matplotlib的教程:http://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html。查找plot_surface
功能。您需要做的就是将标签遮罩转换为喜欢的颜色。您可以通过本教程中描述的元组方法或通过预生成的字符矩阵来完成,如下所示:
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator
import matplotlib.pyplot as plt
import numpy as np
N=10 # matrix size
c1=np.random.randint(2,size=(N,N)) # your mask of 0 and 1
c2=1-c1 # inverted mask
char1 = np.chararray((N, N))
char1[:] = 'r' # color1
char2=np.chararray((N, N))
char2[:]='b' # color2
colors=c1*char1+c2*char2 # color mask for plotting
data=np.random.rand(N,N)*5 # data you are plotting
X=linspace(-3,3,N)
Y=linspace(0,4,N)
X,Y=meshgrid(X,Y)
Z=data
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=colors,linewidth=0, antialiased=False)
plt.show()