将两个图合并为一个图

时间:2016-10-19 09:44:57

标签: python pandas matplotlib plot dataframe

我有两个数据帧,我绘制了两个数据帧。 一个是女性,另一个是男性。

enter image description here

我希望将它们合并到一个不同颜色的图形中 (因为它们具有相同的特征)

这是代码

female[feature].plot(kind='bar')
male[feature].plot(kind = "bar")

feature是数据框的列名。 日期框架看起来像

          X1  X2  X3 ..... X46
male     100  65  75 ..... 150
female   500  75  30 ..... 350

1 个答案:

答案 0 :(得分:1)

我认为您可以使用DataFrame.plot.bar通过T转置DataFrame

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({
'X2': {'female': 75, 'male': 65}, 
'X46': {'female': 350, 'male': 150}, 
'X1': {'female': 500, 'male': 100}, 
'X3': {'female': 30, 'male': 75}})
print (df)
         X1  X2  X3  X46
female  500  75  30  350
male    100  65  75  150

df.T.plot.bar()
plt.show()

graph