Seaborn 箱线图中箱子的坐标

Coordinates of boxes in Seaborn boxplot

This example, extended here, shows how to label bar plots in Matplotlib; a similar idea can be used to label box plots. It relies on knowing the x and y coordinates of the bars, which are returned by the barplot function. How can I do the same thing for Seaborn box plots?不幸的是,Seaborn 没有 return 这些坐标。

你可以四处寻找它们,但它并不漂亮。

sns.boxplot returns matplotlib 绘制框的轴实例。

盒子被创建为 matplotlib.patches.PathPatch 个实例。

我们可以像这样找到那些实例:

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

tips = sns.load_dataset("tips")

ax = sns.boxplot(x="day", y="total_bill", data=tips)

for c in ax.get_children():
    if type(c) == matplotlib.patches.PathPatch:
        print(c.get_extents())

这将打印框的 BBox,在本例中:

Bbox(x0=92.4, y0=116.996, x1=191.6, y1=162.242666667)
Bbox(x0=216.4, y0=114.957333333, x1=315.6, y1=171.6)
Bbox(x0=340.4, y0=125.576, x1=439.6, y1=189.141333333)
Bbox(x0=464.4, y0=131.926666667, x1=563.6, y1=194.172)