首页 > 解决方案 > 使用 distplot 获取 (x, y) 对 seaborn FacetGrid

问题描述

我正在按照seaborn 文档中的示例绘制一些数据的累积分布:

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

sns.set(style="ticks", color_codes=True)

bins = np.arange(0, 65, 5)
tips = sns.load_dataset("tips")
g = sns.FacetGrid(tips, col="day", height=4, aspect=.5)
g = g.map(sns.distplot, "total_bill", bins=bins, hist_kws={"histtype":"stepfilled"})

现在我想获取与每个 bin 对应的 y 值。我怎样才能做到这一点?

关于 SO(从 Seaborn distplot 获取数据点)的类似问题建议get_data()从绘图轴线使用。

我试过了

for ax in g.axes[:,0]:
    lines = ax.get_lines()

我明白了lines<a list of 0 Line2D objects>这意味着没有可调用的线路get_data()

另一个问题(对应于给定 X 值的 Python Seaborn Distplot Y 值)建议使用get_height()轴补丁。

我试过了

for ax in g.axes[:,0]:
    values = [h.get_height() for h in ax.patches]

我得到一个AttributeError: 'Polygon' object has no attribute 'get_height'. 事实上,我在matplotlib API中看不到任何get_height方法。Polygon

想法?

更新:

经过一番调查,我发现有论点hist_kws={"histtype":"stepfilled"}会使通过get_height().

标签: pythonmatplotlibseaborn

解决方案


推荐阅读