2016-09-23 3 views
2

범주 형 변수와 다른 범주형 변수를 파이썬으로 그릴 수있는 가장 좋은 방법은 무엇입니까? 우리는 "남자"와 "여자"가 있고, 다른쪽에는 "지불"하고 "지불하지"않았다고 상상해보십시오. 어떻게하면 남성과 여성에 대한 정보를 설명하고 그들이 대출을 받았는지 여부를 설명하는 의미 있고 이해하기 쉬운 그림을 파이썬으로 그릴 수 있습니다.파이썬의 다른 범주 형 변수와 비교하여 범주 형 변수를 플롯

답변

0

누적 막대 차트이 유형이 사용될 수있다 : enter image description here


상기 적층 막대 그래프의 코드 :

import pandas as pd 
import matplotlib.pyplot as plt 

raw_data = {'genders': ['Male', 'Female'], 
     'Paid': [40, 60], 
     'Unpaid': [60, 40]} 

df = pd.DataFrame(raw_data, columns = ['genders', 'Paid', 'Unpaid']) 


# Create the general blog and the "subplots" i.e. the bars 

f, ax1 = plt.subplots(1, figsize=(12,8)) 

# Set the bar width 
bar_width = 0.75 

# positions of the left bar-boundaries 
bar_l = [i+1 for i in range(len(df['Paid']))] 

# positions of the x-axis ticks (center of the bars as bar labels) 
tick_pos = [i+(bar_width/2) for i in bar_l] 

# Create a bar plot, in position bar_1 
ax1.bar(bar_l, 
     # using the pre_score data 
     df['Paid'], 
     # set the width 
     width=bar_width, 
     # with the label pre score 
     label='Paid', 
     # with alpha 0.5 
     alpha=0.5, 
     # with color 
     color='#F4561D') 

# Create a bar plot, in position bar_1 
ax1.bar(bar_l, 
     # using the mid_score data 
     df['Unpaid'], 
     # set the width 
     width=bar_width, 
     # with pre_score on the bottom 
     bottom=df['Paid'], 
     # with the label mid score 
     label='Unpaid', 
     # with alpha 0.5 
     alpha=0.5, 
     # with color 
     color='#F1911E') 


# set the x ticks with names 
plt.xticks(tick_pos, df['genders']) 

# Set the label and legends 
ax1.set_ylabel("Proportion") 
ax1.set_xlabel("Genders") 
plt.legend(loc='upper left') 

# Set a buffer around the edge 
plt.xlim([min(tick_pos)-bar_width, max(tick_pos)+bar_width]) 
관련 문제