python - Matplotlib 中 Facecolors 使用的标准化颜色图

标签 python matplotlib plot physics

首先,我试图在 matplotlib 中绘制球谐函数,正如它们在 mayavi 中所见:http://docs.enthought.com/mayavi/mayavi/auto/example_spherical_harmonics.html

这里是我所在的地方:

import matplotlib.pyplot as plt
from matplotlib import cm, colors
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from scipy import special

# Create a sphere
r = 3
pi = np.pi
cos = np.cos
sin = np.sin
phi, theta = np.mgrid[0:pi:50j, 0:2*pi:50j]


x = r * sin(phi) * cos(theta)
y = r * sin(phi) * sin(theta)
z = r * cos(phi)

colorfunction=special.sph_harm(3,4,theta,phi).real

norm=colors.Normalize(vmin = np.min(colorfunction), vmax = np.max(colorfunction), clip = False)
print colorfunction



fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x, y, z)
ax.plot_surface(
    x, y, z,  rstride=1, cstride=1, norm=norm, cmap=cm.jet, facecolors=cm.jet(colorfunction))
plt.show()

想法是使用colorfunction 根据球谐函数为球体表面着色。但是,此函数的输出是一个包含负数的数组。我需要做的是“规范化”这个数组,以便它与 matplotlib 的颜色图表现得很好。但是,与这里的答案不同,Color matplotlib plot_surface command with surface gradient ,其中答案只是通过除以最大元素来进行草率的归一化,我有负元素,所以不会起作用。理想情况下,我想使用 matplotlib.colors.Normalize 但它不适用于面部颜色。

我知道该规范适用于 cmap=cm.jet,因为如果我完全删除 facecolors 参数,我会得到一个根据我的 规范功能。

这是我的问题的症结所在,我无法将标准化的颜色图应用于我的面部颜色。有什么想法吗?

这是上面的代码当前生成的图形。如您所见,负值被完全截断并且信息丢失,因为颜色图范围比实际值大得多(所以一切看起来都是蓝色的)。

最佳答案

也许这太琐碎了,但是:

ax.plot_surface(x, y, z,  rstride=1, cstride=1, facecolors=cm.jet(norm(colorfunction)))

这规范化了colorfunction。此外,通过以下方式定义规范化函数就足够了:

norm = colors.Normalize()

这将在 0..1 之间自动缩放输入。

结果:

enter image description here

cmapnorm 关键字似乎适用于使用 Z 数据对表面着色的情况,因此它们在这里没有用。

关于python - Matplotlib 中 Facecolors 使用的标准化颜色图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25023075/

相关文章:

python - 你如何在 Ruby 中做等同于 python 的列表切片?

python - 错误 : Value passed to parameter 'input' has DataType int64 not in list of allowed values: float16, bfloat16、float32、float64?

python - 执行 re.search() 直到行尾,然后更新数据帧

python - 使用 Matplotlib 绘制条形图

python - 用 matplotlib 提出图表

python - 使用函数和循环创建子图

python - 没有名为 '_plotly_utils' 的模块

Matplotlib/pyplot : Auto adjust unit of y Axis

python-2.7 - 在bokeh python 2.7中绘制图on_click Datatable行选择

python - 使用 LinkedBrush gridplot 在 Bokeh 中绘制多个系列