2017-03-24 4 views
1

목표는 표면에 특정 색상을 제공하는 두 개의 다른 3D 플롯()을 "공정한"방법으로 출력하는 것입니다. 피규어 (그리고 동일한 x, y 및 z 축 고정, 그러나 이것은 쉽습니다). 또한 색상 막대는 동일해야합니다 (즉, 똑같은 진드기와 범위). 즉, 그림 1에서 (임의의) 값 8은 진한 빨강과 관련되어 있다고 가정 해 보겠습니다. 그런 다음 figure2도 신청해야합니다. 예제 X에서, Y와 Z는보다 단순하게 만들기 위해 두 그림 모두에서 동일합니다. 표면과 관련된 색상 (예 : V1 및 V2)이 변경되었습니다. 두 표면의복수의 3D 플롯에 동일한 색상 표와 색상 막대를 적용하십시오.

  1. 색상 사실 내가 '(동일 : 당신이 볼 수 있듯이,이 문제가

    import numpy as np 
    import pandas as pd 
    import matplotlib.pyplot as plt 
    from mpl_toolkits.mplot3d import Axes3D 
    from matplotlib import cm 
    # Generate data. V1 (V2) will determine the colours of the first figure1 (figure2) 
    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)) 
    V1 = 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)) 
    V2 = (V1-8) * 3 + 8 
    def my_plot3d(V): 
        # % matplotlib inline # Uncomment 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) 
        clrbar = plt.colorbar(m) 
        min_V = np.min([V1.min().min(), V2.min().min()]) 
        max_V = np.max([V1.max().max(), V2.max().max()]) 
        clrbar.set_clim(min_V, max_V) 
    my_plot3d(V1) 
    

    enter image description here

    my_plot3d(V2) 
    

    enter image description here

    있습니다 V1과 V2가 다르다는 사실에도 불구하고 V1에서 V2를 얻기 위해 선형 스케일링을 적용했습니다.

  2. 색상 막대의 값 범위는 서로 다릅니다.

좋은 점은 컬러 바가 모든 그림에서 같은 색을 동일한 값으로 연결한다는 것입니다. 이전 두 문제를 해결하는 방법?

+0

코드의 들여 쓰기를 수정하십시오. 'my_plot3d' 함수가 끝나는 곳을 알 수 없습니다. – Michael

답변

1

루프에서 m.set_array(V2)이라고 부릅니다. 그래서 모두 배열로 V2을 사용하십시오. 그들은 동일합니다. 나는 이미 착색을 위해 두 개의 서로 다른 배열을 사용하는 경우 V.의 값을 정상화 a solution 당신을 제공하는 생각

m.set_array(V) 


:

그것은 오히려 당신이 사용해야하는 lopp 변수 V해야한다 각각을 개별적으로 정규화하는 대신 동일한 정규화가 두 가지 모두에 사용되는지 확인해야합니다.

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

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

V1 = np.array([[3,2,2],[1,0,3],[2,1,0]]) 
V2 = 1+ V1*1.6 

def plot_array(V, vmin, vmax): 

    fig = plt.figure() 
    ax = fig.add_subplot(111, projection='3d') 
    norm = matplotlib.colors.Normalize(vmin=vmin, vmax=vmax) 
    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') 


plot_array(V1, np.min([V1.min(), V2.min()]), np.max([V1.max(), V2.max()])) 
plot_array(V2, np.min([V1.min(), V2.min()]), np.max([V1.max(), V2.max()])) 

plt.show() 

enter image description here

+0

버그를 발견해 주셔서 감사합니다. 그러나이 솔루션은 부분적으로 만 문제를 해결합니다. 나는 그 질문을 편집했다. – MLguy

+0

답변이 업데이트되었습니다. – ImportanceOfBeingErnest

관련 문제