2017-04-16 858 views
2
male[['Gender','Age']].plot(kind='hist', x='Gender', y='Age', bins=50) 
female[['Gender','Age']].plot(kind='hist', x='Gender', y='Age', bins=50) 

所以基本上,我用一個文件中的數據創建兩個基於性別和年齡的直方圖。從一開始我就按照性別將數據分爲最初的情節。現在我很難將兩個直方圖放在一起。如何合併兩個直方圖python

+0

您在同一張圖是什麼意思? – splinter

+0

是的我需要兩個直方圖在同一個圖上。 – ksalerno

+1

[在同一時間與matplotlib繪製兩個柱狀圖(http://stackoverflow.com/questions/6871201/plot-two-histograms-at-the-same-time-with-matplotlib)的可能的複製 – splinter

回答

0

正如在註釋中,你可以使用matplotlib做這個任務。我還沒有想出如何使用Pandas繪製兩個直方圖(想看看人們是如何做到的)。

import matplotlib.pyplot as plt 
import random 

# example data 
age = [random.randint(20, 40) for _ in range(100)] 
sex = [random.choice(['M', 'F']) for _ in range(100)] 

# just give a list of age of male/female and corresponding color here 
plt.hist([[a for a, s in zip(age, sex) if s=='M'], 
      [a for a, s in zip(age, sex) if s=='F']], 
     color=['b','r'], alpha=0.5, bins=10) 
plt.show() 
0
考慮

轉換dataframes到一個兩列numpy的矩陣作爲matplotlibhist作品具有這種結構,而不是與非數字列兩個不同長度的熊貓dataframes。熊貓join用於將兩列結合,MaleAgeFemaleAge

在這裏,性別指標被刪除,並根據列順序手動標記。

import numpy as np 
import pandas as pd 
from matplotlib import pyplot as plt 

... 
# RESET INDEX AND RENAME COLUMN AFTER SUBSETTING 
male = df2[df2['Gender'] == "M"].reset_index(drop=True).rename(columns={'Age':'MaleAge'}) 
female = df2[df2['Gender'] == "F"].reset_index(drop=True).rename(columns={'Age':'FemaleAge'}) 

# OUTER JOIN TO ACHIEVE SAME LENGTH 
gendermat = np.array(male[['MaleAge']].join(female[['FemaleAge']], how='outer')) 

plt.hist(gendermat, bins=50, label=['male', 'female']) 
plt.legend(loc='upper right') 
plt.show() 
plt.clf() 
plt.close()