1

I have dataset as below,

index 10_YR_CAGR 5_YR_CAGR 1_YR_CAGR
c1_rev 20.5 21.5 31.5
c2_rev 20.5 22.5 24
c3_rev 21 24 27
c4_rev 20 26 30
c5_rev 24 19 15
c1_eps 21 22 23
c2_eps 21 24 25

This data has 5 companies and its parameters like rev, eps, profit etc. I need to plot as below:

rev:

  • x_axis-> index_col c1_rev, ...c5_rev
  • y_axis -> 10_YR_CAGR .. 1_YR_CAGR

eps:

  • x_axis -> index_col: c1_eps,...c5_eps
  • y_axis -> 10_YR_CAGR,... 1_YR_CAGR

etc...

I have tried with following code:

eps = analysis_df[analysis_df.index.str.contains('eps',regex=True)]

for i1 in eps.columns[eps.columns!='index']:
    sns.lineplot(x="index",y=i1,data=eps,label=i1)

I have to make a dataframe from source and then loop it. How can I try to create a for loop which loops from the main source dataframe itself?

Instead of creating a loop for separate parameters, how can I loop from the main source dataframe to create a chart of plots with parameters like rev, eps, profit to facegrid parameters? How to apply those filter in facetgrid?

My sample output of the above code,

enter image description here

How to plot the same sort of plot for different parameters in a single for loop?

1 Answer 1

2

The way facets are typically plotted is by "melting" your analysis_df into id/variable/value columns.

  1. split() the index column into Company and Parameter, which we'll later use as id columns when melting:

    analysis_df[['Company', 'Parameter']] = analysis_df['index'].str.split('_', expand=True)
    
    #      index  10_YR_CAGR  5_YR_CAGR  1_YR_CAGR  Company  Parameter
    #  0  c1_rev         100         21          1       c1        rev
    #  1  c2_rev           1         32         24       c2        rev
    # ...
    
  2. melt() the CAGR columns:

    melted = analysis_df.melt(
        id_vars=['Company', 'Parameter'],
        value_vars=['10_YR_CAGR', '5_YR_CAGR', '1_YR_CAGR'],
        var_name='Period',
        value_name='CAGR',
    )
    
    #      Company  Parameter      Period  CAGR
    #  0        c1        rev  10_YR_CAGR   100
    #  1        c2        rev  10_YR_CAGR     1
    #  2        c3        rev  10_YR_CAGR    14
    #  3        c1        eps  10_YR_CAGR     1
    # ...
    # 25        c2        pft   1_YR_CAGR    14
    # 26        c3        pft   1_YR_CAGR    17
    
  3. relplot() CAGR vs Company (colored by Period) for each Parameter using the melted dataframe:

    sns.relplot(
        data=melted,
        kind='line',
        col='Parameter',
        x='Company',
        y='CAGR',
        hue='Period',
        col_wrap=1,
        facet_kws={'sharex': False, 'sharey': False},
    )
    

relplot 3x1

Sample data to reproduce this plot:

import io
import pandas as pd
csv = '''
index,10_YR_CAGR,5_YR_CAGR,1_YR_CAGR
c1_rev,100,21,1
c2_rev,1,32,24
c3_rev,14,23,7
c1_eps,1,20,50
c2_eps,21,20,25
c3_eps,31,20,37
c1_pft,20,1,10
c2_pft,25,20,14
c3_pft,11,55,17
'''
analysis_df = pd.read_csv(io.StringIO(csv))
Sign up to request clarification or add additional context in comments.

4 Comments

thanks for the update, how to plot the graph row by row and use ylim based on plot instead of common ylim, so that chart look good?
You can use col_wrap=1 to set 1 column (so 3 rows here) and facet_kws={'sharey': False} to unlink the y-axes. I edited my answer with these options.
Thanks @tdy it worked. But x labels are missing while doing this, only last chart shows xlabels. Any idea on how to update xlabels to all plots?
You're welcome. For the x-labels, you can unlink the x-axes as well: facet_kws={'sharex': False, 'sharey': False} (answer updated)

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.