Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to plot multiline chart on Python ggplot?

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

Thank you

like image 890
Hugo Avatar asked Jun 03 '26 18:06

Hugo


1 Answers

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 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()
like image 64
chrisb Avatar answered Jun 05 '26 08:06

chrisb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!