是否可以在Python ggplot上绘制多折线图? [英] Is it possible to plot multiline chart on Python ggplot?

查看:346
本文介绍了是否可以在Python ggplot上绘制多折线图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在python ggplot上绘制具有相同索引的Pandas数据帧的3列.有可能吗?

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

谢谢

推荐答案

我假设您想要ggplot中的某些内容在matplotlib中复制这样的内容.

I'm assuming you want something in ggplot that replicates something like this in matplotlib.

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

ggplot期望数据为长"格式,因此您需要使用melt进行一些重塑.目前它还不支持绘制索引,因此需要将其制成一列.

ggplot expects the data to be in 'long' format, so you need to do a little reshaping, with melt. It also currently does not support plotting the index, so that needs to made into a column.

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()

这篇关于是否可以在Python ggplot上绘制多折线图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆