根据值更改matplotlib中3D条形图中的条形颜色

根据值更改matplotlib中3D条形图中的条形颜色,第1张

根据值更改matplotlib中3D条形图中的条形颜色

从bar3d的文档

color
可以看出,它可以是一个数组,每个条带一种颜色。

这样就很容易在一次调用中为所有栏着色

bar3d
。我们只需要将
data
数组转换为可以使用颜色图完成的颜色数组,

colors = plt.cm.jet(data.flatten()/float(data.max()))

(请注意,颜色图的取值介于0到1之间,因此我们需要将这些值标准化到此范围内。)

完整的例子:

import matplotlib.pyplot as pltfrom mpl_toolkits.mplot3d import Axes3Dimport numpy as npdata = np.array([ [0, 0, 0, 2, 0, 0, 1, 2, 0, 0, 0],         [0, 0, 2, 2, 0, 0, 0, 0, 2, 0, 0],         [1, 0, 2, 2, 1, 2, 0, 0, 2, 0, 2],         [1, 0, 2, 2, 0, 2, 0, 2, 2, 2, 2],         [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],         [2, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2],         [0, 2, 2, 0, 2, 2, 2, 2, 2, 2, 2],         [1, 2, 0, 0, 2, 1, 2, 2, 0, 0, 2],         [0, 0, 2, 1, 0, 0, 2, 0, 0, 0, 0],         [2, 1, 2, 2, 0, 0, 0, 2, 0, 0, 2],         [2, 2, 2, 0, 2, 0, 0, 0, 2, 2, 2],         [2, 2, 0, 0, 2, 2, 2, 2, 2, 0, 0],         [2, 2, 1, 2, 0, 0, 0, 2, 2, 2, 0],         [2, 0, 0, 2, 0, 0, 2, 2, 2, 2, 2],         [2, 0, 0, 2, 0, 2, 2, 2, 2, 2, 2]])ypos, xpos  = np.indices(data.shape)xpos = xpos.flatten()   ypos = ypos.flatten()zpos = np.zeros(xpos.shape)fig = plt.figure()ax = fig.add_subplot(111, projection='3d')colors = plt.cm.jet(data.flatten()/float(data.max()))ax.bar3d(xpos,ypos,zpos, .5,.5,data.flatten(), color=colors)ax.set_xlabel('X')ax.set_ylabel('Y')ax.set_zlabel('Z')plt.show()


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5620274.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-15
下一篇2022-12-15

发表评论

登录后才能评论

评论列表(0条)

    保存