简体   繁体   中英

How Can i Get the X axis Value for the Distributions peak y value in a seaborn distplot?

So i have a frequency distribution plot that looks like this:

image of the Desired point

an i need the x value corresponding to the peak of the y value. How can i get it for the plotting code?

seaborn.distplot('TheSeries',bins = 30, ax=axes[0][1])

Can someone explain how i can get that corresponding value for this and similar cases?

You can extract the coordinates of the kde curve from ax.lines[-1] and use np.argmax() to find the mode of the curve.

Note the in the latest seaborn version distplot has been deprecated. Here histplot with kde=True would be its replacement.

from matplotlib import pyplot as plt
import numpy as np
import seaborn as sns

samples = np.random.randn(300) ** 2 * 50
ax = sns.histplot(samples, bins=30, kde=True, color='skyblue')
kdeline = ax.lines[0]
xs = kdeline.get_xdata()
ys = kdeline.get_ydata()
mode_idx = np.argmax(ys)
ax.vlines(xs[mode_idx], 0, ys[mode_idx], color='tomato', ls='--', lw=2)
plt.show()

示例图

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM