2017-04-30 1 views
1

저는 Kaggle Titanic Machine Learning을 통해 현재 Python을 익숙하게 다루기 위해 http://nbviewer.jupyter.org/github/donnemartin/data-science-ipython-notebooks/blob/master/kaggle/titanic.ipynb을 사용하고 있습니다. 나는 처음 몇 걸음이 무엇을하고 있었는지를 이해하고, 여러 걸음으로 그림을 그려서 이전 단계를 재현하려고 노력하고 있다고 생각했습니다. 플롯을 실제로 표시하지 못하는 것 같습니다. 여기 pyplot에서 여러 개의 숫자로 문제가 생겼습니다

내 코드입니다 :

` 
import pandas as pd 
import numpy as np 
import pylab as plt 

train=pd.read_csv("train.csv") 

#Set the global default size of matplotlib figures 
plt.rc('figure', figsize=(10, 5)) 

#Size of matplotlib figures that contain subplots 
figsize_with_subplots = (10, 10) 

# Size of matplotlib histogram bins 
bin_size = 10 
females_df = train[train['Sex']== 'female'] 
print("females_df", females_df) 
females_xt = pd.crosstab(females_df['Pclass'],train['Survived']) 
females_xt_pct = females_xt.div(females_xt.sum(1).astype(float), axis = 0) 

males = train[train['Sex'] == 'male'] 
males_xt = pd.crosstab(males['Pclass'], train['Survived']) 
males_xt_pct= males_xt.div(males_xt.sum(1).astype(float), axis = 0) 


plt.figure(5) 
plt.subplot(221) 
females_xt_pct.plot(kind='bar', title='Female Survival Rate by Pclass') 
plt.xlabel('Passenger Class') 
plt.ylabel('Survival Rate') 

plt.subplot(222) 

males_xt_pct.plot(kind='bar', title= 'Male Survival Rate by Pclass') 
plt.xlabel('Passenger Class') 
plt.ylabel('Survival Rate') 
` 

그리고 이것은 (다음 222 위치에 새 그림에 플롯 (221) 위치에 하나 등) 및 남성과 다음 다른 음모 별도로 두 개의 빈 플롯을 표시입니다 그것은 결국 실제로 작동합니다. 여기서 내가 뭘 잘못하고 있니?

+0

같을 수도, 최소 완벽한하고 검증 예 (https://stackoverflow.com/help/mcve) 도움이 될 것입니다. – DavidG

+0

내가 조작하고있는 데이터 프레임임을 표시하는 코드를 추가했습니다. 최소한, 완전하고 검증 가능한가? 내가 사용한 데이터는 Titanic Kaggle 경쟁 페이지에 있습니다. 죄송합니다 이것은 stackoverflow를 사용하고 실제로 질문을 내 처음입니다. @DavidG – Dbrim

+0

문제를 재현하거나 위의 내용을 볼 수 있도록 여기에 csv 파일의 일부를 복사하는 '가짜'데이터 세트를 만들어야합니다. – DavidG

답변

0

판다 플롯을 이전에 생성 된 서브 플롯에 플롯하기 위해 팬더 플롯 기능에 대한 인수를 사용할 수 있습니다.

ax=plt.subplot(..) 
df.plot(..., ax=ax) 

그래서,이 경우의 코드

plt.figure(5) 
ax=plt.subplot(221) 
females_xt_pct.plot(kind='bar', title='Female Survival Rate by Pclass',ax=ax) 

ax2=plt.subplot(222) 
males_xt_pct.plot(kind='bar', title= 'Male Survival Rate by Pclass',ax=ax2) 

enter image description here

관련 문제