2017-03-21 1 views
1

facecolors을 사용하여 사전 정의 된 색상으로 표면을 3D로 색상을 표시하려고합니다. 여기서 문제는 cm.ScalarMappable이 표면 V의 색을 정규화하지 않고 plt.cm.jet이 정규화되지 않으므로 색상과 색상 막대가 일치하지 않는다는 것입니다. V (즉 V_normalized)을 수동으로 정상화하려고 시도했지만 그 결과가 아직 정확하지 않습니다. 실제로 V의 가장 높은 값은 표면 모서리에 있어야하지만 실제로는 이미지에 반영되지 않습니다. 표면에 색상을 보정하는 것을 계획하는 방법은 무엇입니까? 당신이 matplotlib.colors.Normalize 인스턴스를 사용하여 정상화를 단순화 수도 있지만facplolors를 사용하는 matplotlib plot_surface 용 색상 막대

import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt 
from mpl_toolkits.mplot3d import Axes3D 
from matplotlib import cm 
# Create data. 
X = np.array([[ 50, 100, 150, 200, 250, 300, 350, 400, 450, 500], 
       [ 50, 100, 150, 200, 250, 300, 350, 400, 450, 500], 
       [ 50, 100, 150, 200, 250, 300, 350, 400, 450, 500], 
       [ 50, 100, 150, 200, 250, 300, 350, 400, 450, 500], 
       [ 50, 100, 150, 200, 250, 300, 350, 400, 450, 500], 
       [ 50, 100, 150, 200, 250, 300, 350, 400, 450, 500], 
       [ 50, 100, 150, 200, 250, 300, 350, 400, 450, 500], 
       [ 50, 100, 150, 200, 250, 300, 350, 400, 450, 500], 
       [ 50, 100, 150, 200, 250, 300, 350, 400, 450, 500]]) 
Y = np.array([[ 75, 75, 75, 75, 75, 75, 75, 75, 75, 75], 
       [125, 125, 125, 125, 125, 125, 125, 125, 125, 125], 
       [175, 175, 175, 175, 175, 175, 175, 175, 175, 175], 
       [225, 225, 225, 225, 225, 225, 225, 225, 225, 225], 
       [275, 275, 275, 275, 275, 275, 275, 275, 275, 275], 
       [325, 325, 325, 325, 325, 325, 325, 325, 325, 325], 
       [375, 375, 375, 375, 375, 375, 375, 375, 375, 375], 
       [425, 425, 425, 425, 425, 425, 425, 425, 425, 425], 
       [475, 475, 475, 475, 475, 475, 475, 475, 475, 475]]) 
Z = pd.DataFrame([[2.11, 2.14, 2.12, 2.10, 2.09, 2.08, 2.07, 2.07, 2.08, 2.05], 
        [2.01, 2.03, 1.99, 1.96, 1.95, 1.93, 1.90, 1.90, 1.92, 1.92], 
        [1.89, 1.90, 1.90, 1.94, 1.92, 1.89, 1.88, 1.87, 1.86, 1.86], 
        [1.79, 1.79, 1.75, 1.79, 1.77, 1.78, 1.78, 1.78, 1.79, 1.76], 
        [1.75, 1.77, 1.8, 1.79, 1.8, 1.77, 1.73, 1.73, 1.77, 1.77], 
        [1.72, 1.76, 1.77, 1.77, 1.79, 1.8, 1.78, 1.78, 1.74, 1.7], 
        [1.67, 1.66, 1.69, 1.7, 1.65, 1.62, 1.63, 1.65, 1.7, 1.69], 
        [1.64, 1.64, 1.61, 1.59, 1.61, 1.67, 1.71, 1.7, 1.72, 1.69], 
        [1.63, 1.63, 1.62, 1.67, 1.7, 1.67, 1.67, 1.69, 1.69, 1.68]], 
       index=np.arange(75, 525, 50), columns=np.arange(50, 525, 50)) 
V = pd.DataFrame([[ 7.53, 7.53, 7.53, 7.53, 7.53, 7.53, 7.53, 7.53, 7.53, 7.53], 
     [ 7.53, 7.53, 7.53, 7.53, 7.66, 8.09, 8.08, 8.05, 8.05, 8.05], 
     [ 7.53, 7.77, 8.08, 8.05, 8.19, 8.95, 8.93, 8.79,8.79, 8.62], 
     [ 8.95, 7.92, 8.95, 8.93, 8.62, 7.93, 8.96, 8.95, 9.09, 8.75], 
     [ 8.61, 8.95, 8.62, 8.61, 8.95, 8.93, 8.82, 9.42, 9.67, 8.48], 
     [ 9.23, 8.61, 8.95, 9.24, 9.42, 8.48, 8.47, 8.65, 8.92, 9.17], 
     [ 8.6 , 9.01, 9.66, 8.05, 9.42, 8.92, 8.81, 7.53, 7.53, 7.53], 
     [ 9.42, 9.25, 8.65, 8.92, 8.25, 7.97, 8.09, 8.49, 8.49, 7.58], 
     [ 10.15, 9.79, 9.1 , 9.35, 9.35, 9.35, 9.25, 9.3 , 9.3 , 8.19]], 
       index=np.arange(75, 525, 50), columns=np.arange(50, 525, 50)) 
# Create the figure, add a 3d axis, set the viewing angle 
# % matplotlib inline # If you are using IPython 
fig = plt.figure(figsize=[15,10]) 
ax = fig.add_subplot(111, projection='3d') 
ax.view_init(45,60) 
# Normalize in [0, 1] the DataFrame V that defines the color of the surface. 
V_normalized = (V - V.min().min()) 
V_normalized = V_normalized/V_normalized.max().max() 
# Plot 
ax.plot_surface(X, Y, Z, facecolors=plt.cm.jet(V_normalized)) 
ax.set_xlabel('x', fontsize=18) 
ax.set_ylabel('y', fontsize=18) 
ax.set_zlabel('z', fontsize=18) 
m = cm.ScalarMappable(cmap=cm.jet) 
m.set_array(V) 
plt.colorbar(m) 

enter image description here

답변

1

귀하의 줄거리는, 맞습니다.

norm = matplotlib.colors.Normalize(vmin=V.min().min(), vmax=V.max().max()) 
ax.plot_surface(X, Y, Z, facecolors=plt.cm.jet(norm(V))) 
m = cm.ScalarMappable(cmap=plt.cm.jet, norm=norm) 
m.set_array([]) 
plt.colorbar(m) 

당신이 그리드에 10.15의 최대 값이 표시되지 않는 이유 요점은, 다른 하나입니다

하나 개의 차원에 따라 N 포인트를 가진 플롯은 (N-1) 얼굴을 가지고있다. 즉, 입력 색상 배열의 마지막 행과 열은 단순히 플롯되지 않습니다.

이것은 다음 그림에서 볼 수 있습니다. 3x3 매트릭스가 플롯되어 2x2면이 생성됩니다. 그들은 첫 번째면이 배열의 첫 번째 요소에 의해 주어진 색상을 갖도록 색상 배열의 각 값에 따라 색상이 지정됩니다. 마지막 요소의 경우 남아있는 색상의면이 없습니다.

import numpy as np 
import matplotlib.pyplot as plt 
from mpl_toolkits.mplot3d import Axes3D 
from matplotlib import cm 
import matplotlib.colors 

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

x = np.arange(3) 
X,Y = np.meshgrid(x,x) 
Z = np.ones_like(X) 

V = np.array([[3,2,2],[1,0,3],[2,1,0]]) 

norm = matplotlib.colors.Normalize(vmin=0, vmax=3) 
ax.plot_surface(X, Y, Z, facecolors=plt.cm.jet(norm(V)), shade=False) 

m = cm.ScalarMappable(cmap=plt.cm.jet, norm=norm) 
m.set_array([]) 
plt.colorbar(m) 

ax.set_xlabel('x') 
ax.set_ylabel('y') 

plt.show() 
:

enter image description here

코드는이 음모를 재현하는 방법

관련 문제