L.Stefan
L.Stefan

Reputation: 346

Plot a line graph with categorical columns for each line

Given this pandas.DataFrame:

    n_size  Runtime region
0   64      0.0001  multMatPtrVet
1   64      0.0001  multMatRowVet
2   64      0.0040  multMatMatPtr
3   64      0.0035  multMatMatRow
4   100     0.0001  multMatPtrVet
5   100     0.0002  multMatRowVet
6   100     0.0158  multMatMatPtr
7   100     0.0144  multMatMatRow
8   128     0.0002  multMatPtrVet
9   128     0.0002  multMatRowVet
10  128     0.0264  multMatMatPtr
11  128     0.0247  multMatMatRow
12  2000    0.0440  multMatPtrVet
13  2000    0.0435  multMatRowVet
14  2000    306.717 multMatMatPtr
15  2000    314.907 multMatMatRow
16  2048    0.0461  multMatPtrVet
17  2048    0.0462  multMatRowVet
18  2048    373.004 multMatMatPtr
19  2048    703.654 multMatMatRow

How can I plot a line graph with X='n_size', Y='Runtime' and draw a different line for each region.

Is it possible to do this directly with df.plot()? I know that's possible if each column named as each region, but I not know to do that transformation also.

Upvotes: 2

Views: 3026

Answers (1)

voldr
voldr

Reputation: 392

You can use seaborn.

import seaborn as sns
import matplotlib.pyplot as plt
sns.lineplot(data = df, x = 'n_size', y = 'Runtime', hue = 'region')
plt.show()

Output:

enter image description here

Or if you want to use df.plot:

df.set_index('n_size', inplace=True)
df.groupby('region')['Runtime'].plot(legend=True)

which gives the same result.

Upvotes: 4

Related Questions