Issue
I have a DataFrame whose list of columns look like this.
df.columns
['dr1', 'r1', 'dr9', 'r9', 'dr21', 'r21', 'dr26', 'r26',
'dr32', 'r32', 'dr37', 'r37', 'dr49', 'r49', 'dr52', 'r52',
'dr105', 'r105', 'dr118', 'r118']
DF=
dr1 r1 dr9 r9 dr21 r21 dr26 r26 dr32 r32 dr37 r37 dr49 r49 dr52 r52 dr105 r105 dr118 r118
1.370077e-03 79.492551 1.885239e-03 91.721574 1.799699e-03 75.545364 7.945449e-04 56.431429 1.608503e-02 81.127766 2.142295e-03 68.240745 1.079295e-03 81.316295 6.329889e-03 75.426940 3.396244e-03 73.181260 3.068635e-03 74.735711
1 6.434528e-04 75.018284 1.793570e-03 89.052090 2.371287e-03 72.617954 8.695473e-04 52.827016 1.322238e-02 77.128761 2.404007e-03 66.440905 1.290229e-03 77.116401 3.996065e-03 71.487773 3.867551e-03 69.449838 2.715589e-03 70.733729
2 4.515897e-04 71.005130 2.193572e-03 86.580311 2.479454e-03 69.926743 8.851577e-04 49.512332 1.333745e-02 73.533564 2.027752e-03 64.363379 1.173162e-03 72.735975 2.546558e-03 67.581776 3.918930e-03 65.663389 2.571297e-03 66.733496
3 4.306166e-04 67.021659 2.013258e-03 83.587133 2.331958e-03 67.171358 9.085228e-04 46.485528 1.455461e-02 69.784763 1.783866e-03 62.075661 1.109990e-03 68.369232 2.612790e-03 63.726161 3.752804e-03 61.925863 2.718291e-03 63.163573
4 4.106338e-04 63.078692 2.162847e-03 80.224832 2.552906e-03 64.217342 9.786517e-04 43.847398 1.431416e-02 65.971753 1.601472e-03 59.616326 1.174008e-03 64.488470 2.752785e-03 60.275407 4.022395e-03 58.637315 2.613834e-03 59.649589
I want to create a subplot with multiple graphs between each r vs dr
.
I am able to do this by individually calling each subplot axis, but I want to automate this using loop. What can be done to automate this?
Solution
You can use pd.wide_to_long
to turn your data into long form, then plot with pandas' groupby:
long_data = pd.wide_to_long(df.reset_index(), ['r','dr'], i='index', j='type').reset_index()
long_data.groupby('type').plot(x='r',y='dr')
Or with seaborn's FacetGrid:
g = sns.FacetGrid(data=long_data, col='type', col_wrap=4)
g.map(sns.lineplot, 'r','dr')
Output (with seaborn):
Answered By - Quang Hoang
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.