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
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
正如在註釋中,你可以使用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()
轉換dataframes到一個兩列numpy的矩陣作爲matplotlib
的hist
作品具有這種結構,而不是與非數字列兩個不同長度的熊貓dataframes。熊貓join
用於將兩列結合,MaleAge和FemaleAge。
在這裏,性別指標被刪除,並根據列順序手動標記。
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()
您在同一張圖是什麼意思? – splinter
是的我需要兩個直方圖在同一個圖上。 – ksalerno
[在同一時間與matplotlib繪製兩個柱狀圖(http://stackoverflow.com/questions/6871201/plot-two-histograms-at-the-same-time-with-matplotlib)的可能的複製 – splinter