python - 对 3D 表面的 x、y、z 以外的颜色图使用单独的函数

标签 python matplotlib colormap

我使用下面的代码生成 3D 形状 (X1,Y1,Z1)。一个单独的函数 A5 已用于颜色图。但到目前为止我只得到一个蓝色的表面。 Z1 中的值范围高于 A5 - 这可能是代码生成蓝色表面的原因吗? Facecolor是否需要Z1的限制?如果是这样,我应该如何使用plot_surface来达到此目的?

surf = ax.plot_surface(X1,Y1,Z1,rstride=1, cstride=1, linewidth=0, facecolors=plt.cm.jet(A5),antialiased=False)
m = cm.ScalarMappable(cmap=cm.jet)
q=[-col,col]
m.set_array(q)
plt.colorbar(m)

最佳答案

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

# generating data
x, y = np.meshgrid(np.linspace(-1,1,101), np.linspace(-1,1,101))
z = x+y  # z is just a plane
r = np.sqrt(x*x+y*y)
w = 2*np.cos(5*r)**2-np.sin(6*x) # w is a funny trig func

# initializing the colormap machinery
norm = matplotlib.colors.Normalize(vmin=np.min(w),vmax=np.max(w))
c_m = matplotlib.cm.Spectral
s_m = matplotlib.cm.ScalarMappable(cmap=c_m, norm=norm)
s_m.set_array([])

# a figure and a 3d subplot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# the actual plot, note that to create the facecolors array
# I've used the norm function I instantiated above
ax.plot_surface(x, y, x+y, linewidth=0,
                rstride=1, cstride=1,
                facecolors=s_m.to_rgba(w))

# draw the colormap using the ScalarMappable instantiated above and shoe
plt.colorbar(s_m)
plt.show()

enter image description here

关于python - 对 3D 表面的 x、y、z 以外的颜色图使用单独的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29947956/

相关文章:

python - 在 Django 中过滤

python - 使用 matplotlib 保存散点图动画会产生空白视频文件

python - Matplotlib 干图与 pandas 数据框在一系列 x 值上的关系

matlab - 根据点密度的 3D 散点图的颜色代码点

python - 如何在自定义颜色图中获取屏蔽值的透明度 (matplotlib/python)

c++ - Opencv C++ : Display pixel value using cursor of the image before applying colormap

python - 在 Python 中,使用哪种架构与第三方 Web API 进行交互?

python - 在 Mac OS X 上静态嵌入 Python 时动态符号查找失败

python - Pandas :groupby 和可变权重

python - 如何绘制日期列表的直方图?