【问题标题】:Change bar color in a 3D bar plot in matplotlib based on value根据值更改 matplotlib 中 3D 条形图中的条形颜色
【发布时间】:2017-05-09 12:23:16
【问题描述】:

我在 matplotlib 中有一个 3D 条形图,它总共包含 165 个条形图,目前它非常混乱。

我想根据谨慎的 z 值更改条形的颜色:0、1、2。

我知道可以使用 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()

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

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

    【讨论】:

    • 这很完美,非常感谢您的解释!
    • 嗨,是否可以在每个点分割一个条并为条的每个部分设置不同的颜色?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-03
    • 2012-01-19
    • 2017-03-17
    • 1970-01-01
    • 2017-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多