【问题标题】:How can a 3d surface colormap be mapped to a scalar function?如何将 3d 表面颜色图映射到标量函数?
【发布时间】:2017-05-01 14:33:39
【问题描述】:

我有一个标量函数,它表示球面中的电势。我想针对给定的半径绘制曲面并根据势函数将其点链接到颜色图。

如何将该标量函数映射到表面中的颜色图?我怀疑它必须在传递给函数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)]

我不明白,也不知道如何链接到标量函数。

我的代码

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()

【问题讨论】:

标签: python matplotlib plot 3d


【解决方案1】:

原则上,在 matplotlib 中有两种方法可以为曲面图着色。

  1. 使用cmap 参数指定颜色图。在这种情况下,颜色将根据z 数组进行选择。如果不需要,
  2. 使用facecolors 参数。这需要与z 形状相同的颜色数组。

所以在这种情况下,我们需要选择选项 2 并构建一个颜色数组。 为此,可以选择颜色图。颜色图将 0 和 1 之间的值映射到颜色。由于势能的值远高于和低于此范围,因此需要将它们归一化到 [0,1] 范围内。
Matplotlib 已经提供了一些辅助函数来进行这种标准化,并且由于势能具有 1/x 依赖性,因此对数色标可能是合适的。

因此,最终可以给 facecolors 一个数组

colors = cmap(norm(potential(...)))

缺失的部分现在是颜色条。为了将颜色条链接到表面图中的颜色,我们需要手动设置一个带有颜色图和规范化实例的 ScalarMappable,然后我们可以将其提供给颜色条。

sm = plt.cm.ScalarMappable(cmap=plt.cm.coolwarm, norm=norm)
sm.set_array(pot)
fig.colorbar(sm, shrink=0.5, aspect=5)

这里是完整的例子。

from __future__ import division
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import matplotlib.colors
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), 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)
pot = potencial(x,y,z)


norm=matplotlib.colors.SymLogNorm(1,vmin=pot.min(),vmax=pot.max())
colors=plt.cm.coolwarm(norm(pot))

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Plot the surface
surf = ax.plot_surface(x,y,z, facecolors=colors,
                       linewidth=0, antialiased=False)
# Set up colorbar
sm = plt.cm.ScalarMappable(cmap=plt.cm.coolwarm, norm=norm)
sm.set_array(pot)
fig.colorbar(sm, shrink=0.5, aspect=5)


ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("z")
plt.show()

【讨论】:

猜你喜欢
  • 2013-02-14
  • 2017-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-04
  • 2012-04-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多