Pandas groupby results on the same plot

You need to init axis before plot like in this example

import pandas as pd
import matplotlib.pylab as plt
import numpy as np

# random df
df = pd.DataFrame(np.random.randint(0,10,size=(25, 3)), columns=['ProjID','Xcoord','Ycoord'])

# plot groupby results on the same canvas 
fig, ax = plt.subplots(figsize=(8,6))
df.groupby('ProjID').plot(kind='line', x = "Xcoord", y = "Ycoord", ax=ax)
plt.show()

enter image description here


Consider the dataframe df

df = pd.DataFrame(dict(
        ProjID=np.repeat(range(10), 10),
        Xcoord=np.random.rand(100),
        Ycoord=np.random.rand(100),
    ))

Then we create abstract art like this

df.set_index('Xcoord').groupby('ProjID').Ycoord.plot()

enter image description here


Another way:

for k,g in df.groupby('ProjID'):
  plt.plot(g['Xcoord'],g['Ycoord'])

plt.show()