1

I have a pandas dataframe like this - (Creating a random dataframe)

from random import randint
from random import random
import random
import pandas as pd

x = [randint(1,20) for i in range(20)]
y1 = [random() for i in range(20)]
y2 = [random() for i in range(20)]
y3 = [random() for i in range(20)]
y4 = [random() for i in range(20)]
g = ['a', 'b', 'c']

group = [random.choice(g) for i in range(20)]
data = {'Group': group, 'x': x, 'y1':y1, 'y2':y2, 'y3':y3, 'y4':y4}

df = pd.DataFrame(data)
df.sort_values('Group')

The dataframe is like this -

>>> df.sort_values('Group')
   Group   x        y1        y2        y3        y4
17     a   9  0.400730  0.242629  0.858307  0.799613
16     a  14  0.644299  0.952255  0.257262  0.376845
5      a   3  0.784374  0.800639  0.753612  0.441645
18     a   3  0.988016  0.739003  0.741000  0.299011
11     a  18  0.672816  0.232951  0.763451  0.762478
0      b   7  0.670889  0.785928  0.604563  0.620951
15     b   3  0.838479  0.286988  0.374546  0.013822
4      b   4  0.495855  0.159839  0.984262  0.882428
13     b   3  0.756058  0.979226  0.423426  0.297381
8      b  13  0.835705  0.374927  0.492676  0.939113
12     b  17  0.643511  0.156267  0.248037  0.316526
14     c  13  0.303215  0.177303  0.980071  0.705428
9      c  16  0.829414  0.173755  0.992532  0.398509
7      c   9  0.774353  0.082118  0.089582  0.587679
6      c  14  0.551595  0.737882  0.127206  0.985017
3      c   4  0.072765  0.497016  0.634819  0.149798
2      c   1  0.971598  0.254215  0.325086  0.588159
1      c  14  0.467277  0.631844  0.927199  0.051251
10     c  13  0.346592  0.384929  0.185384  0.330408
19     c  16  0.790785  0.449498  0.176042  0.036896

Using this dataframe I intend to plot multiple graphs group wise (in this case 3 graphs as there are only 3 groups). Each graph is a multi line graph with x on x-axis and [y1, y2, y3, y4] on y-axis

How can I achieve this, I can plot a single multiline graph, but unable to plot multiple plots group -wise.

1 Answer 1

1

You can use groupby:

fig, axes = plt.subplots(1, 3, figsize=(10,3))
for (grp, data), ax in zip(df.groupby('Group'), axes.flat):
    data.plot(x='x', ax=ax)

Output:

enter image description here

Note: You don't really need to sort by group.

Sign up to request clarification or add additional context in comments.

4 Comments

Instead of giving axes.flat how can I specify columns?
What do you mean by specifying columns? That doesn’t seem to be listed in your question.
Apart from y1,y2,y3 and y4 I have more columns in the df - but I guess I will just drop them and do this. I am having trouble with the grid layout as there are usually just not 3 groups. Sometimes the groups can be around 20 or sometimes even 40. So how can I arrange the layout?
Pass 'y=['y1',...]` to .plot. You would need to know how many groups with len(df['Group'].unique()) and set the layout. You can also use seaborn as alternative for groupby.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.