V_sqrt
V_sqrt

Reputation: 567

How to plot values by specific column for a comparison

I have the following data (two dataframes):

  df
           Name Surname P   R   F   
        0   B   N   0.41    0.76    0.53
        1   B   L   0.62    0.67    0.61
        2   B   SV  0.63    0.53    0.52
        3   B   SG  0.43    0.61    0.53
        4   B   R   0.81    0.51    0.53
        5   T   N   0.32    0.82    0.53
        6   T   L   0.58    0.69    0.62
        7   T   SV  0.67    0.61    0.64
        8   T   SG  0.53    0.63    0.57
        9   T   R   0.74    0.48    0.58

and

data = [['B','N',0.41,0.72,0.51], 
['B','L',0.66,0.67,0.62],
['B','SV',0.63,0.51,0.51],
['B','SG',0.44,0.63,0.51],
['B','R',0.81,0.51,0.62],
['T','N',0.33,0.80,0.47],
['T','L',0.58,0.61,0.63],
['T','SV',0.68,0.61,0.64],
['T','SG',0.53,0.63,0.57],
['T','R',0.74,0.48,0.58]]

df1 = pd.DataFrame(data, columns = ['Name','Surname','P','R','F']) 

I would need to compare, eventually in separate plots, the values of P, R and F based on Name and Surname.

I tried as follows:

  ax = df[['P']].plot()
    l = ax.get_lines()
    df1[['P']].plot(ax=ax, linestyle='--', color=(i.get_color() for i in l))  

But the problem is that the x-values are not Surname and the information on Name misses (for example with a legend or using different colours).

Any help on this would be appreciated.

Upvotes: 0

Views: 227

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150735

You can use seaborn to plot the two lines while looping through the value columns:

fig = plt.figure(figsize=(8,8))
for n, col in enumerate(['P','R','F'], start=1):
    ax = plt.subplot(2,2,n)
    sns.lineplot(data=df1, x='Surname', y=col, hue='Name')

Output: enter image description here

Upvotes: 1

Related Questions