seaborn 找不到包含数据的 PathCollection

seaborn cannot find PathCollection containing the data

seaborn tutorial我策划了一个情节:

import seaborn as sns
sns.set_style("whitegrid")
tips = sns.load_dataset("tips")
ax = sns.swarmplot(x=tips["total_bill"])

现在我想要包含 make in interactive using mpld3 数据的 PathCollection 的句柄。我查看输出 ax children 并得到以下列表:

[<matplotlib.collections.PathCollection at 0x7828d325f8>,
 <matplotlib.spines.Spine at 0x782824a518>,
 <matplotlib.spines.Spine at 0x782824ab38>,
 <matplotlib.spines.Spine at 0x782824a748>,
 <matplotlib.spines.Spine at 0x782824a940>,
 <matplotlib.axis.XAxis at 0x782824acf8>,
 <matplotlib.axis.YAxis at 0x78282e5518>,
 <matplotlib.text.Text at 0x78282fb710>,
 <matplotlib.text.Text at 0x78282fb0b8>,
 <matplotlib.text.Text at 0x78282fb208>,
 <matplotlib.patches.Rectangle at 0x78282fb0f0>]

然而,当我在 PathCollection 中查找数据时,它是空的 ax.get_children()[0].get_array()

如何找到包含数据点的 PathCollection 的句柄?

包含数据点的 PathCollectionax.get_children()[0]。调用 .get_array() 的结果是 None(非空),但这不会阻止您使用 PathCollection 使用 mplot3d。此代码对我有用:

tips = sns.load_dataset("tips")
ax = sns.swarmplot(x=tips["total_bill"])
tooltip = mpld3.plugins.PointLabelTooltip(ax.get_children()[0],
                                          labels=tips.total_bill.tolist())
mpld3.plugins.connect(plt.gcf(), tooltip)
mpld3.show()

做了一个 Jupiter notebook 只是为了好玩。