soccer_analytics_fan
soccer_analytics_fan

Reputation: 319

How could I plot two pandas columns based on items in 3 other columns?

    id1     id2  id3     acc     s
0   57915   58  43352   0.15    1.95
1   57915   58  43352   0.45    1.65
2   57915   58  43352   0.75    0.55
3   57915   58  43352   1.05    0.55
4   57915   58  43352   1.35    1.25
5   57915   58  43352   1.65    1.15
6   57915   58  43352   1.95    1.05
7   57915   58  43352   2.25    0.85
8   57915   58  43352   2.55    0.65
9   57915   58  43387   0.15    1.45
10  57915   58  43387   0.45    2.75
11  57915   58  43387   0.75    1.45
12  57915   58  43387   1.05    2.85
13  57915   58  43387   1.35    2.45
14  57915   58  43387   1.65    2.85
15  57915   58  43387   1.95    2.25
16  57915   58  43387   2.25    2.85
17  57915   58  43387   2.55    1.95
18  57915   58  43387   2.85    2.65
19  57915   58  43387   3.15    1.65
20  57915   58  43387   3.45    2.45
21  57915   58  43387   3.75    1.25
22  57915   58  43387   4.05    2.25
23  57915   58  43387   4.35    2.05
24  57915   58  43387   4.65    1.75
25  57915   58  43387   4.95    0.85
26  57915   58  43387   5.25    0.95
27  57915   58  43387   5.55    0.85
28  57915   58  46074   0.15    1.65
29  57915   58  46074   0.45    0.35

Hi all,

I have the following pandas df and I would like to plot acc on the y axis and s on the x axis. However I would like to do it for every unique id1,id2,id3 pairing.

So if (id1,id2,id3) = (57915,58,43352) then I would like to make a scatter plot of s = [1.95,1.65,0.55,0.55,1.25,1.15,1.05,0.85,0.65] vs acc=[0.15,0.45,0.75,1.05,1.35,1.65,1.95,2.25,2.55]. I would like to do this for (id1,id2,id3) = (57915,58,43387) and (id1,id2,id3) = (57915,58,46074) as well. Could someone please help me out? Thanks!

This is a subset of the data and in later rows id1 and id2 change as well.

Upvotes: 0

Views: 53

Answers (2)

plasmon360
plasmon360

Reputation: 4199

You can use groupby to create groups and plot each group on the figure layout

fig, ax = plt.subplots(figsize=(12,9))
groups_dict = df.groupby(['id1','id2','id3']).groups
for k, v in groups_dict.items():
    ax.plot(df.loc[v,'acc'], df.loc[v,'s'],'o', label=k)
ax.set_xlabel('acc')
ax.set_ylabel('s')
plt.legend(loc=4)
plt.show()

produces

enter image description here

Upvotes: 1

Erfan
Erfan

Reputation: 42886

You can set your groups as index, then plot your figure:

df.set_index(['id1','id2', 'id3']).plot(x='s', y='acc', figsize=(15,9))
plt.show()

plot

Or with different labels per group:

plt.figure(figsize=(15,9))
for i, d in df.groupby(['id1','id2', 'id3']):
    plt.plot(d['s'], d['acc'], label=i)

plt.legend()
plt.show()

plot2

Upvotes: 1

Related Questions