gpt4 book ai didi

python - matplotlib plot_surface 命令的颜色条

转载 作者:IT老高 更新时间:2023-10-28 22:08:19 28 4
gpt4 key购买 nike

我已经修改了 mplot3d example code在保罗的帮助下我的申请。代码如下:

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

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, np.pi, 100)

x = 10 * np.outer(np.cos(u), np.sin(v))
y = 10 * np.outer(np.sin(u), np.sin(v))
z = 10 * np.outer(np.ones(np.size(u)), np.cos(v))
z1 = z * np.cos(0.5*x)

N = z1 / z1.max() # normalize 0..1
surf = ax.plot_surface(x, y, z, rstride=1, cstride=1, facecolors=cm.jet(N), linewidth=0, antialiased=False, shade=False)

fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()

enter image description here

代码非常适合绘制曲面。但是当我尝试在上面的图中添加颜色条时,出现以下错误:

Traceback (most recent call last):
File "<ipython console>", line 1, in <module>
File "C:\Python26\lib\site-packages\spyderlib\widgets\externalshell\startup.py", line 122, in runfile
execfile(filename, glbs)
File "C:\Documents and Settings\mramacha\My Documents\Python\Candela\test.py", line 22, in <module>
fig.colorbar(surf, shrink=0.5, aspect=5)
File "C:\Python26\lib\site-packages\matplotlib\figure.py", line 1104, in colorbar
cb = cbar.Colorbar(cax, mappable, **kw)
File "C:\Python26\lib\site-packages\matplotlib\colorbar.py", line 706, in __init__
mappable.autoscale_None() # Ensure mappable.norm.vmin, vmax
File "C:\Python26\lib\site-packages\matplotlib\cm.py", line 261, in autoscale_None
raise TypeError('You must first set_array for mappable')
TypeError: You must first set_array for mappable

如果有人能够指导我,我将不胜感激。

帕布

最佳答案

您可以使用代理可映射对象,因为您的表面阵列未映射。可映射对象只是将任何数组的值转换为由颜色图定义的 RGB 颜色。

在您的情况下,您想对 z1 数组执行此操作:

import matplotlib.cm as cm
m = cm.ScalarMappable(cmap=cm.jet)
m.set_array(z1)
plt.colorbar(m)

关于python - matplotlib plot_surface 命令的颜色条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6600579/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com