Seaborn: How to change size of spots in a JointPlot according to feature?

Advertisements

I have codes below. You can see that a JointPlot is plotted.

But I want the size of dots to change with the values of the column "size".

So I changed the last line marker='o') to marker='o', s = "size"). Now I have error message AttributeError: 'Line2D' object has no property 's'.

I want each spot size to be different (i.e. similar to this). How can I modify my code to achieve this?

import seaborn as sns
import numpy as np
from itertools import product
sns.set(style="darkgrid")

tips = sns.load_dataset("tips")
g = sns.jointplot("total_bill", "tip", data=tips, kind="reg",
                  xlim=(0, 60), ylim=(0, 12), color='k', size = 7)

#Clear the axes containing the scatter plot
g.ax_joint.cla()

# #Plot each individual point separately
for i,row in enumerate(tips.values):
    g.ax_joint.plot(row[0], row[1], color="blue", marker='o')

Updates:

I also tried to combine two plots directly, but still doesn’t work. There is no error, but the scatterplot is just pasted to the right…

import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
from itertools import product
sns.set(style="darkgrid")

tips = sns.load_dataset("tips")

fig, ax = plt.subplots()

g = sns.jointplot("total_bill", "tip", data=tips, kind="reg",
                  xlim=(0, 60), ylim=(0, 12), color='k', size = 7)

#Clear the axes containing the scatter plot
g.ax_joint.cla()

ax2 = ax.twinx()

sns.scatterplot(
    data=tips, x="total_bill", y="tip", hue="size", size="size",
    sizes=(20, 200), legend="full"
)

plt.show()

>Solution :

You can create a seaborn scatterplot on g.ax_joint. The following code has been tested with seaborn 0.11.2 (older versions may have a problem with a column named ‘size’).

import seaborn as sns
import numpy as np

sns.set(style="darkgrid")

tips = sns.load_dataset("tips")
g = sns.jointplot(x="total_bill", y="tip", data=tips, kind="reg",
                  xlim=(0, 60), ylim=(0, 12), color='k')
g.ax_joint.cla()
sns.scatterplot(data=tips, x='total_bill', y='tip', size='size', sizes=(10, 200),
                ax=g.ax_joint)

Leave a ReplyCancel reply