2017-05-09 85 views
3

我在matplotlib中有一個三維條形圖,其中包含總共165個條形圖,此時它非常混亂。根據值在matplotlib中根據值更改3D條形圖中的條形顏色

enter image description here

我想根據謹慎的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() 

回答

4

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

enter image description here

+0

這是完美的,非常感謝你的解釋! –