【问题标题】:seaborn cannot find PathCollection containing the dataseaborn 找不到包含数据的 PathCollection
【发布时间】:2016-12-08 09:37:08
【问题描述】:

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 句柄。我查看输出axchildren,我得到以下列表:

[<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 的句柄?

【问题讨论】:

    标签: python matplotlib plot seaborn


    【解决方案1】:

    包含数据点的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 只是为了好玩。

    【讨论】:

    • 我不知道,它似乎与你的目的无关,所以我没有仔细研究它。
    • @HananShteingart 您的实际问题是什么?你想要.get_array() 做什么?你不需要它来绘制带有mplotd3tooltip 插件的swarmplot。
    • 感谢@Goyo 的帮助,我只是想了解幕后发生的事情
    • 这不能正常工作,因为标签与数据框中的行的顺序不同。通过绘制图形和检查标签来检查自己。
    • 我面临同样的问题,无法检索与每个点对应的行索引(这将允许以正确的顺序注释图中的每个点)。特定于上述示例的解决方案是将标签参数替换为labels=ax.get_children()[0].get_offsets()[:, 0])
    猜你喜欢
    • 2018-02-28
    • 2018-06-04
    • 1970-01-01
    • 2018-05-19
    • 2021-12-10
    • 2021-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多