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

7

我在matplotlib中有一个3D条形图,总共有165个条形,目前看起来相当混乱。

enter image description here .

我想根据离散的z值(0,1,2)更改条形图的颜色。

我知道可以使用掩码来更改基于特定值的1D条形图的颜色栏,如 Color matplotlib bar chart based on value 中所述。

还有一个问题是如何根据值更改条形图颜色: Defining colors of Matplotlib 3D bar plot

我不确定是否完全理解了给出的答案,但在这种情况下我无法使其工作。

代码如下:

   data = [[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]]

   ly = len(data[0])
   lx = len(data[:,0])
   xpos = np.arange(0,lx,1)    # Set up a mesh of positions
   ypos = np.arange(0,ly,1)
   xpos, ypos = np.meshgrid(xpos+0.25, ypos+0.25)

   xpos = xpos.flatten()   # Convert positions to 1D array
   ypos = ypos.flatten()
   zpos = np.zeros(lx*ly)

   dx = 0.5 * np.ones_like(zpos)
   dy = dx.copy()
   dz = data.flatten()


   ys = np.array([float(yi) for yi in y[1:]])

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

   # all blue bars
   #ax.bar3d(xpos,ypos,zpos, dx, dy, dz, color='b')

   # try changing color bars

   colors = ['r','g','b']
   for i in range(0,3):

       ax.bar3d(xpos[i], ypos[i], zpos[i], dx, dy, dz[i], alpha=0.1, 
                    color=colors[i])

   ax.set_xlabel('X')
   ax.set_ylabel('Y')
   ax.set_zlabel('Z')


plt.show()
1个回答

7
根据bar3d文档color可以是数组,每个柱子有一种颜色。
这使得在单次调用bar3d中给所有柱子着色变得非常容易;我们只需要将data数组转换为颜色数组,这可以使用颜色映射完成。
colors = plt.cm.jet(data.flatten()/float(data.max()))

(注意,色图将取值范围限制在0到1之间,因此我们需要将值规范化为此范围。)
完整示例:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

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

enter image description here


这很完美,非常感谢您的解释! - Helena K
你好,是否可以在每个点处分割一个条形图,并为每个部分设置不同的颜色? - Shahrooz Pooryousef

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接