pythonseabornjointgrid

Plotting two distributions in the marginal plots of JointGrid


I'm trying to plot a scatter plot with two marginal distribution plots. I have two datasets and I want to compare the two in a single plot. The problem is that I can plot scatter plots of both datasets but I can't plot the distribution plots of the second dataset in the marginal axes. Thanks in advance.

I have come this far that I can plot scatter and distribution plots of the first dataset, and the scatter plot of the second dataset using the code below.

g = sns.JointGrid(data=data1, x="observed", y="predicted")
g.plot(sns.scatterplot, sns.distplot)
g.ax_joint.scatter(data=data2, x="observed", y="predicted", c='r')

The figure below shows what I get running my code: enter image description here

When I want to add distribution plots of the second dataset by adding the code below to the one above, the distribution plots of the second dataset replaces that of the first one. I want to have both of them for comparison purposes.

g.ax_marg_x.hist(data=data2, x= "observed")
g.ax_marg_y.hist(data=data2, x= "observed", orientation="horizontal")

This is what I get by doing so: enter image description here


Solution

  • Adding data1 seems to do the trick.

    g.ax_marg_x.hist(data=data1, x="observed")
    g.ax_marg_x.hist(data=data2, x="observed")
    g.ax_marg_y.hist(data=data1, x="observed", orientation="horizontal")
    g.ax_marg_y.hist(data=data2, x="observed", orientation="horizontal")
    

    enter image description here