Translation of question I asked no OS :
I have a scalar function that represents the electrical potential on a spherical surface. I want to plot, for a given radius, the surface and map its color points based on the potential function.
How can I map this function to the surface? I suspect there are arguments to the ax.plot_surface . I tried to use the argument: facecolors=potencial(x,y,z)
, ma received ValueError: Invalid RGBA argument.
Looking at the source code of the third example , there is:
# 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)]
That I did not understand, I have no idea how to link with a scalar function.
My code
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 = r/z #cos(theta)
asr = a/r
s=0
s += np.polyval(special.legendre(1), x) * 3/2*np.power(asr, 2)
s += np.polyval(special.legendre(3), x) * -7/8*np.power(asr, 4)
s += np.polyval(special.legendre(5), x) * 11/16*np.power(asr, 6)
return v*s
# criar dados
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')
# Plotar a superficie
surf = ax.plot_surface(x,y,z, cmap=cm.coolwarm,
linewidth=0, antialiased=False)
fig.colorbar(surf, shrink=0.5, aspect=5)
# Está mapeado aos valores do eixo-z
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("z")
plt.show()