MoonNote

한 개의 축에 여러 개 변수 시각화하기 본문

Study/Python

한 개의 축에 여러 개 변수 시각화하기

Kisung Moon 2021. 7. 6. 19:54
  • 연속형 변수일 때
    # Multiple box plots on one Axes
    fig, ax = plt.subplots(figsize=(12,10))
    data = [df_0['age'], df_1['age']]
    labels = ['normal', 'cancer']
    bp = ax.boxplot(data, vert=True, patch_artist=True)
    #fill with colors
    colors = ['lightblue', 'pink']
    for patch, color in zip(bp['boxes'], colors):
    patch.set_facecolor(color)
    
    #adding horizontal grid lines
    ax.yaxis.grid(True)
    
    plt.title('Age', fontsize= 30)
    plt.xticks([1, 2], labels, fontsize=15)
    #ax.set_ylabel('Granzyme B', fontsize=15)
    plt.show()

 

 

  • 범주형 변수일 때
  • import matplotlib.pyplot as plt
    
    # normal
    df_0_0 = sum(df_0['smoking'] == 0)
    df_0_1 = sum(df_0['smoking'] == 1)
    df_0_2 = sum(df_0['smoking'] == 2)
    # cancer
    df_1_0 = sum(df_1['smoking'] == 0)
    df_1_1 = sum(df_1['smoking'] == 1)
    df_1_2 = sum(df_1['smoking'] == 2)
    
    smoker = ['current', 'ex', 'non']
    num = [df_0_0, df_0_1, df_0_2]
    num_smoker = [
                  [df_0_0, df_1_0], # 9, 21
                  [df_0_1, df_1_1], # 16, 18
                  [df_0_2, df_1_2]  # 25, 4
                  ]
    type_smoker = ['current', 'ex', 'non']
    
    #print(num_smoker)
    X = np.arange(2)
    fig = plt.figure()
    ax = fig.add_axes([0,0,1,1])
    ax.bar(X + 0.00, num_smoker[0], color = 'pink', width = 0.25)
    ax.bar(X + 0.25, num_smoker[1], color = 'lightgreen', width = 0.25)
    ax.bar(X + 0.50, num_smoker[2], color = 'lightblue', width = 0.25)
    ax.legend(labels=type_smoker)
    plt.xticks([0.25, 1.25], labels, fontsize=15)
    plt.title('Smoking', fontsize= 20)
    plt.show()

'Study > Python' 카테고리의 다른 글

주소  (0) 2021.09.09
EDA library 추천: pandas-profiling  (0) 2021.08.20
pandas: 특정 조건의 row를 drop하기  (0) 2021.07.17
pandas: 조건 일치하는 dataframe 출력  (0) 2021.07.16
파이썬(Python)에서 모든 조합 구하기  (0) 2021.07.14
Comments