问题
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