0

I have data frame with the columns 'utility', 'p', 'mean', 'sd', 'iteration'. I want to check the relation between 'p' as the x and 'utility' as the y, and to see how its change with different 'mean', 'sd' and 'iteration' values. In order to visualize the data properly, I want to show few plots in the same figure, and that was the reason I chose to use relplot, a figure level function of seaborn. Here is my code and the figure:

import pandas as pd
import seaborn as sns

data = {'Utility': [0.6250544037730811, 0.6091476481763195, 0.6230131522688347, 0.5000000000000003, 0.5000000000000003, 0.5000000000000003, 0.5621765008857981, 0.5621765008857981, 0.5621765008857981, 0.6224593312018534, 0.6224593312018534, 0.6224593312018534, 0.4782238914061277, 0.4859561575141825, 0.4715435909529027, 0.5260886064478572, 0.515751827909536, 0.4867704454880584, 0.5695008915146452, 0.5454729853117513, 0.5291730220268104, 0.4808967870953403, 0.4772728462758539, 0.4577117329731061, 0.5355220476492846, 0.5255553979838615, 0.5162684446817668, 0.5863130353563192, 0.579808576836609, 0.5324238887155383, 0.4965294526200233, 0.5005441277312246, 0.4634170389692744], 'p': [0.9, 0.9, 0.9, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.2, 0.2, 0.2], 'mean': [1.1, 1.1, 1.1, 1.0, 1.0, 1.0, 1.05, 1.05, 1.05, 1.1, 1.1, 1.1, 1.0, 1.0, 1.0, 1.05, 1.05, 1.05, 1.1, 1.1, 1.1, 1.0, 1.0, 1.0, 1.05, 1.05, 1.05, 1.1, 1.1, 1.1, 1.0, 1.0, 1.0], 'sd': [0.3, 0.4, 0.5, 0.3, 0.4, 0.5, 0.3, 0.4, 0.5, 0.3, 0.4, 0.5, 0.3, 0.4, 0.5, 0.3, 0.4, 0.5, 0.3, 0.4, 0.5, 0.3, 0.4, 0.5, 0.3, 0.4, 0.5, 0.3, 0.4, 0.5, 0.3, 0.4, 0.5], 'iteration': [100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0]}
df = pd.DataFrame(data)

title = 'Utility as a function of allocation strategy'
color = ['aquamarine', 'teal', 'DarkBlue']
p1 = sns.relplot(x='p', y='Utility', data=df,
                   col='iteration',
                   row='mean',
                   hue='sd',
                   style='sd',
                   kind='line',
                   marker='X',
                   palette=color[:len(sd_list)],
                   facet_kws={'sharey': 'row', 'sharex': True})

p1.fig.suptitle(f"{title}".format(title=title), fontsize=12)
p1.tight_layout()

the figure generated from the code

I want to add on top of each subplot a trend line, while keeping the dot on the plot connected (not a scatter graph with a trend line). I thought to use lmplot, but it's less efficient for subplots. Is there a way I will be able to do this?

Edit: When I try this code I get a common trendline to all 3 plot in the axes, and I would like to get separate one for each one. Is it possible?

g = sns.relplot(data=df, x='p', y='Utility',
                col='iteration',
                row='mean',
                hue='sd',
                style='sd',
                kind='line',
                marker='X',
                palette=color[:len(sd_list)],
                facet_kws={'sharey': 'row', 'sharex': True})
g.data = df
g.map_dataframe(sns.regplot, x='p', y='Utility', color='b')

enter image description here

  • As per the duplicate, but use `sns.regplot`: `g.map(sns.regplot, 'p', 'Utility', color='r', ci=None)` – Trenton McKinney May 25 '21 at 18:07
  • When I do it with regplot, the dot are not connected. When I do it with relplot, its generate multiple graphs, not as a subplot – Bakia Glilit May 25 '21 at 18:29
  • That's tedious, `regplot` doesn't have hue. So you need to do an `g = sns.lmplot(...)` and then `g.map(sns.lineplot, ...)`. `g = sns.lmplot(data=df, x='p', y='Utility', col='iteration', row='mean', hue='sd', ci=None); g.data = df; g.map_dataframe(sns.lineplot, x='p', y='Utility', hue='sd', style='sd', marker='X')` – Trenton McKinney May 25 '21 at 19:30

0 Answers0