Seaborn boxplot 中的框坐标 [英] Coordinates of boxes in Seaborn boxplot
问题描述
这个例子,扩展了此处,显示了如何标记条形图在Matplotlib中;类似的想法可以用来标记箱形图.它依赖于知道由 barplot 函数返回的条形的 x 和 y 坐标.我怎样才能对 Seaborn 箱线图做同样的事情?不幸的是,Seaborn 不会返回这些坐标.
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? Unfortunately Seaborn does not return these coordinates.
推荐答案
你可以稍微修改一下来找到它们,但它并不漂亮.
You can hack around a bit to find them, but its not pretty.
sns.boxplot
返回 matplotlib
绘制框的轴实例.
sns.boxplot
returns the matplotlib
Axes instance the boxes are drawn on.
框被创建为 matplotlib.patches.PathPatch
实例.
我们可以找到这样的实例:
We can find those instances like so:
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
,在此示例中:
This will print the BBox
of the boxes, in this example:
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)
这篇关于Seaborn boxplot 中的框坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!