# Set seed to reproduce the results
np.random.seed(42)
# Generate random data
df = pd.DataFrame(dict(age=(np.random.uniform(-20, 50, 100))))
# KDE plot
ax = df['age'].plot(kind='density')
# Access the child artists and calculate the mean of the resulting array
mean_val = np.mean(ax.get_children()[0]._x)
# Annotate points
ax.annotate('mean', xy=(mean_val, 0.008), xytext=(mean_val+10, 0.010),
arrowprops=dict(facecolor='black', shrink=0.05),
)
# vertical dotted line originating at mean value
plt.axvline(mean_val, linestyle='dashed', linewidth=2)
选择切片0是因为它对应于
^{pr2}$matplotlib.lines.Line2D
轴对象的位置。在相关问题 更多 >
编程相关推荐