2017-05-09 11 views
3

matplotlibに3Dバープロットがあります。これは合計165バーで構成されており、現時点では非常に混沌です。値に基づいてmatplotlibの3Dバープロットのバーの色を変更します

enter image description here

私は控えめなz値に基づいてバーの色を変更したいと思います:0,1,2。

Color matplotlib bar chart based on valueのようにマスクを使用して特定の値に基づいて1Dバープロットのカラーバーを変更するオプションがあることは知っています。

や価値観に基づいてバーの色を変更する方法についての質問もあります:私は完全に与えられた答えを理解するが、私はそれがこのケースで動作させることができない場合 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への1回のコールですべてのバーを色付けすることが非常に簡単になります。私たちは、カラーマップを使用して行うことができる色の配列に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

、あなたの説明のために非常に多くの感謝! –

関連する問題