Home > front end >  Get the max value of axis in a KDE plot from pandas dataframe or matplotlib
Get the max value of axis in a KDE plot from pandas dataframe or matplotlib

Time:09-15

After some processing done to my dataframe, I ended up with the following df (I did final_df.to_csv() and this is that saved file:

kilometers
1072.1995482049733
2503.2499069986962
354.0418359911766
858.2197121707121
872.7067362778436
1110.8656962113155
1956.1799725414431
298.5388710676759
933.9980032255144
844.663104277453

Therefore, if I want to get the probability density function (pdf) I use kde plot

And getting the pdf and plotting it:

from scipy import stats
import seaborn as sns

data = final_df['kilometers']
loc = data.mean()
scale = data.std()
pdf = stats.norm.pdf(data, loc=loc, scale=scale)


fig, ax = plt.subplots()
ax = sns.lineplot(x=data, y=pdf, ax=ax)
plt.show()

PDF

Now to my question: Is there a way to obtain the highest value (where max is) (let's say x and y, although just x is fine) of these plots? I've been checking in getting values on x axis and some matplotlib documentation (about axis in here). I tried some ax.get_children() (ref) but without any use. The data should be in x and y axis (IN THEORY, to my understanding, but it might just refer to what the plot phisically has, not the data): Following the latter question referenced in here:

ploti.get_children()

[<matplotlib.lines.Line2D at 0x7fd294de1c60>,
 <matplotlib.spines.Spine at 0x7fd294b18040>,
 <matplotlib.spines.Spine at 0x7fd294b18400>,
 <matplotlib.spines.Spine at 0x7fd294b18460>,
 <matplotlib.spines.Spine at 0x7fd294b18490>,
 <matplotlib.axis.XAxis at 0x7fd294b18070>,
 <matplotlib.axis.YAxis at 0x7fd294b18bb0>,
 Text(0.5, 1.0, ''),
 Text(0.0, 1.0, ''),
 Text(1.0, 1.0, ''),
 <matplotlib.legend.Legend at 0x7fd294de16f0>,
 <matplotlib.patches.Rectangle at 0x7fd294b1b280>]

ploti.get_children()[5] #this one should have what I'm looking for

<matplotlib.axis.XAxis at 0x7fd294b18070>

ploti.get_children()[5]._x

AttributeError: 'XAxis' object has no attribute '_x'

I thought: hey, maybe check what attributes this object has, so maybe there is some 'get_x_value'. But I coulnd't find anything relevant to what I'm looking for (I might lack knowledge also):

dir(ploti.get_children()[5])

(I could post the output but it's quite long and verbose. Feel free to ask for it! )

Apart from what I think I could be using: numpy converting all values of the plot and getting the max value of an axis; Is there a quick way to obtain the max value of an axis in a plot?

CodePudding user response:

You can get data with:

data = ax.lines[0].get_xydata()

Then get the coordinates for the maximum y with np.where:

data[np.where(data[:, 1] == max(data[:, 1]))]
  • Related