Coordinates of boxes in Seaborn boxplot

谁说我不能喝 提交于 2019-12-13 06:09:33

问题


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.


回答1:


You can hack around a bit to find them, but its not pretty.

sns.boxplot returns the matplotlib Axes instance the boxes are drawn on.

The boxes are created as matplotlib.patches.PathPatch instances.

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()

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)


来源:https://stackoverflow.com/questions/37369632/coordinates-of-boxes-in-seaborn-boxplot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!