python - 使用 mplot3d 缩放面部颜色的颜色图

标签 python matplotlib mplot3d

我有一个简单的任务,应该有一个简单的解决方案,但我已经尝试了好几天了。我尽量具体。

  • 我尝试使用 matplotlib 的 mplot3d 和 plot_surface 绘制曲面。 当我绘制数据集“z”的表面并尝试将颜色图缩放到某个最大值时,我将“vmax”属性更改为此值。效果很好。

  • 当我尝试绘制一个数据集 (z) 的表面并使用第二个数据集 (fc) 的面色时,这也能正常工作。

  • 当我想要缩放 facecolors 的颜色图时,vmax 属性被 facecolors 值否决了。因此 Vmax 没有效果(尝试 1)。线条也消失了,但这是另一个问题。

  • 还尝试更改面部颜色数据集 (fc) 的值,但未达到预期效果(尝试 2)。

我尝试得到一个带有缩放颜色图的图形(如下图“缩放”),但缩放到面部颜色,而不是 z 值。

下面的代码是我现在的,结果是这样的:

enter image description here

有谁知道我在这里缺少什么?任何想法表示赞赏!

import pylab as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

plt.ion()

# creating dataset
profile = np.arange(20)**2
z = profile.repeat(20).reshape(20,20)
fc= np.rot90(z.copy())

x = np.arange(z.shape[0])
y = np.arange(z.shape[1])
X, Y = np.meshgrid(x,y)

# plotting
vmax = 100
fig = plt.figure()
ax = fig.add_subplot(1,4,1, projection='3d', azim=210)
ax.plot_surface(X,Y,z, cmap=plt.cm.jet, cstride=1, rstride=1)
ax.set_title('normal')

ax = fig.add_subplot(1,4,2, projection='3d', azim=210)
ax.plot_surface(X,Y,z, cmap=plt.cm.jet, cstride=1, rstride=1, vmax=vmax)
ax.set_title('scaled')

ax = fig.add_subplot(1,4,3, projection='3d', azim=210)
ax.plot_surface(X,Y,z, facecolors=plt.cm.jet(fc), cstride=1, rstride=1, vmax=vmax)
ax.set_title('rotated (attempt1)')

ax = fig.add_subplot(1,4,4, projection='3d', azim=210)
fc[fc> vmax] = vmax
ax.plot_surface(X,Y,z, facecolors=plt.cm.jet(fc), cstride=1, rstride=1)
ax.set_title('rotated (attempt2)')

最佳答案

一个 - 肮脏的 - 解决方案是重新调整剪裁的面色,使最大值等于高度图的最大值(除了基本上你建议的尝试 2):

ax.plot_surface(X,Y,z, facecolors=plt.cm.jet(np.clip(fc,0,vmax)*np.max(z)/vmax), cstride=1, rstride=1, vmax=vmax)

这是否给出了您正在寻找的结果?

关于python - 使用 mplot3d 缩放面部颜色的颜色图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11469626/

相关文章:

Python - 来自套接字数据的正则表达式通配符?

python - 为 SymPy 图检索 matplotlib ContourSet

Python - 为 3d 线图着色

python - Spyder 终端内的 3D matplotlib 图

python - 如何在 QTreeWidget 中制作可编辑值

python - 从 Python 列表中删除日期

python - django 服务器代码不更新

python - 在 Python Matplotlib 中将图形保存为 eps 时出错

python - 我怎样才能停用 'Warning: Source ID 510 was not found when attempting to remove it - GLib.source_remove(self._idle_event_id)' ?

python - bar3d 图的颜色图/颜色问题