如何在一个以上的图中绘制多个列

时间:2019-03-06 12:50:01

标签: python matplotlib seaborn

我有一个看起来像这样的数据框(df):

import pandas as pd

df = pd.DataFrame({'PORTFOLIO': 'A A A A B B B B'.split(),
                   'DATE': '01.01.2018 01.04.2018 01.07.2018 01.10.2018 01.01.2018 01.04.2018 01.07.2018 01.10.2018'.split(),
                   'TWR': '0.902394258 0.070277784 0.550490473 0.46175313 0.238824009 0.39631305 0.174549818 0.39739729'.split(),
                   'IRR': '0.109757902 0.234597079 0.049599131 0.936973087 0.455933496 0.60647549 0.154498108 0.887030381'.split()})

df['TWR'] = df['TWR'].astype('float')
df['IRR'] = df['IRR'].astype('float')

在我的真实数据框中,我有大约10个投资组合,我希望在其自己的图中显示每个投资组合。我的尝试是(尽管仅对其中一列成功):

sns.set(style ='ticks', color_codes = True)
g = sns.FacetGrid(df, col="PORTFOLIO", col_wrap = 4, height = 4)
g = g.map(plt.plot, 'DATE','IRR')

我很高兴每个投资组合都有自己的图(彼此相邻),但是如何使IRR和TWR列同时出现?

我想看到每个图看起来像这样:

CreateTimerQueueTimer

1 个答案:

答案 0 :(得分:0)

这里仅是简单的更改g = g.map(plt.plot, 'DATE', 'IRR', 'TWR') 使用

df = pd.DataFrame({'PORTFOLIO': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'],
                   'DATE': ['01.01.2018', '01.04.2018', '01.07.2018', '01.10.2018', '01.01.2018',
                            '01.04.2018', '01.07.2018', '01.10.2018', ],
                   'IRR': [.7, .8, .9, .4, .2, .3, .4, .9],
                   'TWR': [.1, .3, .5, .7, .1, .0, .4, .9],
                   })

print(df)
sns.set(style='ticks', color_codes=True)
g = sns.FacetGrid(df, col="PORTFOLIO", col_wrap=4, height=4)
g = g.map(plt.plot, 'DATE', 'IRR', color='#FFAA11')
g = g.map(plt.plot, 'DATE', 'TWR', color='#22AA11')
plt.show()

enter image description here