如何在多个图表中绘制多列数据

2024-04-16 10:06:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数据帧(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列同时出现呢?你知道吗

我想看到每个图表都是这样的:

enter image description here


Tags: 数据importpandasdfdate图表colfloat
1条回答
网友
1楼 · 发布于 2024-04-16 10:06:09

这里只有简单的改变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

相关问题 更多 >