Pandas groupby results for the same plot

I am dealing with the following data frame (for illustration purposes only, the actual df is quite large):

   seq          x1         y1
0  2           0.7725      0.2105
1  2           0.8098      0.3456
2  2           0.7457      0.5436
3  2           0.4168      0.7610
4  2           0.3181      0.8790
5  3           0.2092      0.5498
6  3           0.0591      0.6357
7  5           0.9937      0.5364
8  5           0.3756      0.7635
9  5           0.1661      0.8364

Trying to build a multi-line graph for the above coordinates (x as "x1 versus y as" y1 ").

Lines with the same "seq" are the same path and must be built as one separate line, like all x, y coordinates corresponding to seq = 2, belong to one line, etc.

I can build them, but on separate graphs I want all the lines to be on the same graph using subheadings , but it didn’t work out correctly.

import matplotlib as mpl
import matplotlib.pyplot as plt

%matplotlib notebook

df.groupby("seq").plot(kind = "line", x = "x1", y = "y1")

This creates 100 graphs (which equals the number of unique seqs). Suggest me a way to get all the lines in one chart.

** UPDATE *

To solve this problem, I executed the following code:

     fig, ax = plt.subplots(figsize=(12,8))
     df.groupby('seq').plot(kind='line', x = "x1", y = "y1", ax = ax)
     plt.title("abc")
     plt.show()

. seq = 2 5 1; seq = 3 .

, 1, 1 2, .

?

+4
3

,

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

+3

:

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

plt.show()
+5

df

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

,

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

enter image description here

+3

Source: https://habr.com/ru/post/1673064/


All Articles