Matplotlib布尔着色

时间:2014-04-17 16:57:40

标签: python matplotlib 3d

我想绘制一个球体的三维绘图,根据theta,phi坐标的某些函数对切片进行不同的着色。我可以绘制两个单独的图形,每种颜色中的一个,但我不确定绘图时网格/网格的确切工作方式。即我想在一个球体中对所有thetas / phis进行网格/网格划分,但是在给定布尔函数的情况下抛出某些对。这可能吗?附件是散点图的图片,用于表达我想对表面做的事情。colored sphere

1 个答案:

答案 0 :(得分:2)

基于scatter3d_demo.py中的matplotlib tutorial

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

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection='3d')

THETA, PHI = np.ogrid[0:2*np.pi:40j, 0:np.pi:30j]

X = 10 * np.cos(THETA) * np.sin(PHI)
Y = 10 * np.sin(THETA) * np.sin(PHI)
Z = 10 * np.ones_like(THETA) * np.cos(PHI)

def func(THETA, PHI):
    mask = (THETA < np.pi/2) & (np.pi/3 < PHI) & (PHI < 2 * np.pi/3)
    return np.where(mask, 1, 0.5)

C = func(THETA, PHI)

x = X.ravel()
y = Y.ravel()
z = Z.ravel()

c = C.ravel()
ax.scatter(x, y, z, c=c, s=30, vmin=0, vmax=1)
ax.set_aspect('equal')
plt.show()

产量

enter image description here


请注意,您还可以使用plot_surface

为球体上的色块着色
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

THETA, PHI = np.ogrid[0:2*np.pi:40j, 0:np.pi:30j]

X = 10 * np.cos(THETA) * np.sin(PHI)
Y = 10 * np.sin(THETA) * np.sin(PHI)
Z = 10 * np.ones_like(THETA) * np.cos(PHI)

def func(THETA, PHI):
    mask = (THETA < np.pi/2) & (np.pi/3 < PHI) & (PHI < 2 * np.pi/3)
    return np.where(mask, 1, 0.5) 

C = func(THETA, PHI)
jet = plt.cm.jet
ax.plot_surface(X, Y, Z, rstride=2, cstride=2, facecolors=jet(C))
ax.set_aspect('equal')
plt.show()

enter image description here