Search code examples
data-visualizationseabornstandard-deviationkernel-density

Visualizing Data, Tracking Specific SD Values


BLUF: I want to track a specific Std Dev, e.g. 1.0 to 1.25, by color coding it and making a separate KDF or other probability density graph.

What I want to do with this is be able to pick out other Std Dev ranges and get back new graphs that I can turn around and use to predict outcomes in that specific Std Dev.

Data: https://www.dropbox.com/s/y78pynq9onyw9iu/Data.csv?dl=0

What I have so far is normalized data that looks like a shotgun blast:

enter image description here

Code used to produce it:

data = pd.read_csv("Data.csv")
sns.jointplot(data.x,data.y, space=0.2, size=10, ratio=2, kind="reg");

What I want to achieve here looks like what I have marked up below:

enter image description here

I kind of know how to do this in RStudio using RidgePlot-type functions, but I'm at a loss here in Python, even while using Seaborn. Any/All help appreciated!


Solution

  • The following code might point you in the right directly, you can tweak the appearance of the plot as you please from there.

    tips = sns.load_dataset("tips")
    g = sns.jointplot(x="total_bill", y="tip", data=tips)
    
    top_lim = 4
    bottom_lim = 2
    temp = tips.loc[(tips.tip>=bottom_lim)&(tips.tip<top_lim)]
    g.ax_joint.axhline(top_lim, c='k', lw=2)
    g.ax_joint.axhline(bottom_lim, c='k', lw=2)
    
    # we have to create a secondary y-axis to the joint-plot, otherwise the
    # kde might be very small compared to the scale of the original y-axis
    ax_joint_2 = g.ax_joint.twinx()
    sns.kdeplot(temp.total_bill, shade=True, color='red', ax=ax_joint_2, legend=False)
    ax_joint_2.spines['right'].set_visible(False)
    ax_joint_2.spines['top'].set_visible(False)
    ax_joint_2.yaxis.set_visible(False)
    

    enter image description here