我有一個標量函數,表示球形表面的電勢。我想繪製一個給定半徑的曲面,並根據潛在函數將它的點鏈接到一個顏色映射。如何將3d表面顏色映射映射到標量函數?
如何將該標量函數映射到曲面中的顏色映射?我懷疑它必須在傳遞給函數ax.plot_surface
的參數中。我嘗試使用參數:facecolors=potencial(x,y,z)
,但它給了我一個ValueError: Invalid RGBA argument
。縱觀source code of the third example,有:
# Create an empty array of strings with the same shape as the meshgrid, and
# populate it with two colors in a checkerboard pattern.
colortuple = ('y', 'b')
colors = np.empty(X.shape, dtype=str)
for y in range(ylen):
for x in range(xlen):
colors[x, y] = colortuple[(x + y) % len(colortuple)]
我不明白,也沒有一個IDEIA如何鏈接到一個標量函數。
我的代碼
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np
from scipy import special
def potencial(x,y,z, a=1., v=1.):
r = np.sqrt(np.square(x) + np.square(y) + np.square(z))
p = z/r #cos(theta)
asr = a/r
s=0
s += np.polyval(special.legendre(1), p) * 3/2*np.power(asr, 2)
s += np.polyval(special.legendre(3), p) * -7/8*np.power(asr, 4)
s += np.polyval(special.legendre(5), p) * 11/16*np.power(asr, 6)
return v*s
# Make data
def sphere_surface(r):
u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, np.pi, 100)
x = r * np.outer(np.cos(u), np.sin(v))
y = r * np.outer(np.sin(u), np.sin(v))
z = r * np.outer(np.ones(np.size(u)), np.cos(v))
return x,y,z
x,y,z = sphere_surface(1.5)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Plot the surface
surf = ax.plot_surface(x,y,z, cmap=cm.coolwarm,
linewidth=0, antialiased=False)
fig.colorbar(surf, shrink=0.5, aspect=5)
# This is mapping the color to the z-axis value
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("z")
plt.show()
我做了葡萄牙語此相同[問題:https://pt.stackoverflow.com/questions/201673/como-um-colormap-de-uma-superfície-pode- ser-mapeado-a-uma-função-escalar –