3

I need to plot 3 columns of a Pandas dataframe on python ggplot, with the same index. Is that possible?

Thank you

4

2 回答 2

7

我假设您想要 ggplot 中的某些内容在 matplotlib 中复制类似的内容。

import pandas as pd
df = pd.DataFrame({'a': range(10), 'b': range(5,15), 'c': range(7,17)})
df.plot()

ggplot 期望数据为“长”格式,因此您需要进行一些整形,使用melt. 它目前也不支持绘制索引,所以需要做成一列。

from ggplot import ggplot, geom_line, aes
import pandas as pd
df = pd.DataFrame({'a': range(10), 'b': range(5,15), 'c': range(7,17)})

df['x'] = df.index
df = pd.melt(df, id_vars='x')

ggplot(aes(x='x', y='value', color='variable'), df) + \
      geom_line()
于 2014-06-29T19:32:57.717 回答
2

使用最新版本的 ggplot,它更容易:

from ggplot import ggplot, geom_line, aes
import pandas as pd

df = pd.DataFrame({'a': range(10), 'b': range(5, 15), 'c': range(7, 17)})
df['x'] = df.index
ggplot(aes(x='x'), data=df) +\
    geom_line(aes(y='a'), color='blue') +\
    geom_line(aes(y='b'), color='red') +\
    geom_line(aes(y='c'), color='green')

在此处输入图像描述

于 2017-08-09T01:04:27.143 回答