简体   繁体   English

Seaborn:如何根据特征改变 JointPlot 中斑点的大小?

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

I have codes below.我有下面的代码。 You can see that a JointPlot is plotted.您可以看到绘制了一个 JointPlot。

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") .所以我将最后一行marker='o')更改为marker='o', s = "size") Now I have error message AttributeError: 'Line2D' object has no property 's' .现在我有错误消息AttributeError: 'Line2D' object has no property 's'

I want each spot size to be different (ie 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()

You can create a seaborn scatterplot on g.ax_joint .您可以创建一个seaborn散点图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').以下代码已使用 seaborn 0.11.2 进行了测试(旧版本可能存在名为“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)

带有自适应散点图的 sns.jointplot

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM