Matplotlib plot_surface命令的颜色栏 [英] Colorbar for matplotlib plot_surface command

查看:785
本文介绍了Matplotlib plot_surface命令的颜色栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Paul的帮助下,我为我的应用程序修改了mplot3d 示例代码.该代码显示为:

I have modified the mplot3d example code for my application with Paul's help. The code reads:

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

该代码可以很好地绘制表面.但是,当我尝试向上面的图中添加颜色条时,出现以下错误:

The code works great to plot the surface. But when I try to add a colorbar to the above plot, I get the following error:

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

如果有人能够指导我,我将非常感激.

I would be so grateful if someone is able to guide me with this.

Prabhu

推荐答案

由于未映射表面数组,因此可以使用代理可映射对象. mappable可以将任何数组的值简单地转换为由颜色图定义的RGB颜色.

You can use a proxy mappable object since your surface array is not mapped. The mappable simply converts the values of any array to RGB colors defined by a colormap.

您要使用z1数组进行此操作:

In your case you want to do this with the z1 array:

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

这篇关于Matplotlib plot_surface命令的颜色栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆