我想在 Seaborn 箱形图中显示一些值,例如每个箱形图系列的观测总数、平均值、混合/最大值。有没有办法在情节中显示这些?
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_theme(style="ticks")
# Initialize the figure with a logarithmic x axis
f, ax = plt.subplots(figsize=(7, 6))
ax.set_xscale("log")
# Load the example planets dataset
planets = sns.load_dataset("planets")
# Plot the orbital period with horizontal boxes
sns.boxplot(
planets, x="distance", y="method", hue="method",
whis=[0, 100], width=.6, palette="vlag"
)
# Add in points to show each observation
sns.stripplot(planets, x="distance", y="method", size=4, color=".3")
# Tweak the visual presentation
ax.xaxis.grid(True)
ax.set(ylabel="")
sns.despine(trim=True, left=True)
只需使用 matplotlib text 函数以及 x 和 y 的“数据坐标”:
mytext = 'Total: XX \n Mean: YY \n Min Value: AA \n Max Value: BB'
ax.text(x=15000, y='Radial Velocity',
s=mytext, style='italic',
color='white', bbox = {'facecolor': 'black'},
fontsize=8, verticalalignment='center')
plt.show()