Home > Enterprise >  seaborn: cdf of dataframe columns in 1 figure
seaborn: cdf of dataframe columns in 1 figure

Time:09-29

My dataframe looks like the following:

df = pd.DataFrame(
    {'id': [543476, 539345, 536068, 537710, 538255],
 'true_distance': [22836.49,7920.67,720.39,1475.87,35212.81],
 'simulated_distance': [19670.69,7811.64,386.67,568.95,24720.94]}
)

df
      id    true_distance   simulated_distance
0   543476   22836.49            19670.69
1   539345    7920.67             7811.64
2   536068     720.39              386.67
3   537710    1475.87              568.95
4   538255   35212.81            24720.94

I need to compare the true distance and simulated distance in a single cdf plot.

EDIT I want the cdf of true_distance and simulated_distance in one figure (identified by legend).

CodePudding user response:

import pandas as pd
import seaborn as sns


#create a long format of your df
df_long = df.melt(id_vars=["id"], 
            value_vars= ["true_distance", 
            "simulated_distance"], 
            var_name="Variable", 
            value_name= "Distance", 
            ignore_index=True,
)

# create lineplot filtered by your two 
  Variables
sns.lineplot(data = df_long,
             y = "Distance",
             x = "id",
             hue = "Variable",
             linewidth = 2,
)

output

  • Related