python - 是否可以在 Python ggplot 上绘制多线图?

标签 python pandas python-ggplot

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

谢谢

最佳答案

我假设您想要在 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()

关于python - 是否可以在 Python ggplot 上绘制多线图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24478925/

相关文章:

python - 在 Pandas 中查找与数组匹配的列名

python - 如何克服 'NoneType' object has no attribute 'lower' 错误?

python - ggplot python 以每小时分辨率处理数周内的时间数据

python - 在 Python ggplot 中更改轴标签大小

python ggplot为日期时间设置轴范围

python - 如何使用 python API 将元字段添加到 Shopify 集合?

python - 为什么字典有时是空的? (在 tornado.web.Application 类实例中保存数据)

python - 为什么使用 cron 安排的 Github Actions 工作流没有在正确的时间触发?

python如何匹配两个不相等大小的列之间的部分字符串

python - Pandas 通过根据另一列的值添加列级别来 reshape 数据框