【发布时间】:2021-12-18 05:44:16
【问题描述】:
我有下面的代码。您可以看到绘制了一个 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')
更新:
我也试过直接合并两个情节,但还是不行。没有报错,只是散点图只是贴到右边了……
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()
【问题讨论】:
-
@JohanC 谢谢。在最后一行,
g.ax_joint.scatter(row[0], row[1], color="blue", marker='o', s=7)有效,但g.ax_joint.scatter(row[0], row[1], color="blue", marker='o', s="size")仍然无效,这是我的主要意图...我希望点具有不同的大小... -
fig, ax = plt.subplots()在这里不起作用,因为jointplot是一个创建新图形的图形级函数。你需要sns.scatterplot(..., ax=g.ax_joint)
标签: python plot seaborn data-visualization visualization