Seaborn:如何根据特征更改 JointPlot 中斑点的大小?
Seaborn: How to change size of spots in a JointPlot according to feature?
我有下面的代码。您可以看到绘制了一个 JointPlot。
但我希望点的大小随“大小”列的值而变化。
所以我把最后一行marker='o')
改成了marker='o', s = "size")
。现在我收到错误消息 AttributeError: 'Line2D' object has no property 's'
.
我希望每个点大小都不同(即类似于 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')
更新:
我也试过直接合并两个plot,还是不行。没有报错,只是散点图贴到了右边...
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()
您可以在 g.ax_joint
上创建 seaborn 散点图。以下代码已使用 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(..., scatter=False)
。
g = sns.jointplot(x="total_bill", y="tip", data=tips, kind="reg", scatter=False,
xlim=(0, 60), ylim=(0, 12), color='k')
sns.scatterplot(data=tips, x='total_bill', y='tip',
hue='size', palette='husl',
size='size', sizes=(10, 200), legend='full',
ax=g.ax_joint)
我有下面的代码。您可以看到绘制了一个 JointPlot。
但我希望点的大小随“大小”列的值而变化。
所以我把最后一行marker='o')
改成了marker='o', s = "size")
。现在我收到错误消息 AttributeError: 'Line2D' object has no property 's'
.
我希望每个点大小都不同(即类似于 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')
更新:
我也试过直接合并两个plot,还是不行。没有报错,只是散点图贴到了右边...
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()
您可以在 g.ax_joint
上创建 seaborn 散点图。以下代码已使用 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(..., scatter=False)
。
g = sns.jointplot(x="total_bill", y="tip", data=tips, kind="reg", scatter=False,
xlim=(0, 60), ylim=(0, 12), color='k')
sns.scatterplot(data=tips, x='total_bill', y='tip',
hue='size', palette='husl',
size='size', sizes=(10, 200), legend='full',
ax=g.ax_joint)