在對我的資料框進行了一些處理之后,我得到了以下 df(我做了final_df.to_csv(),這是保存的檔案:
kilometers
1072.1995482049733
2503.2499069986962
354.0418359911766
858.2197121707121
872.7067362778436
1110.8656962113155
1956.1799725414431
298.5388710676759
933.9980032255144
844.663104277453
因此,如果我想獲得概率密度函式 (pdf),我可以使用
并獲取 pdf 并繪制它:
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()

現在我的問題是:有沒有辦法獲得這些圖的最高值(max 在哪里)(假設 x 和 y,盡管 x 很好)?我一直在檢查x 軸上的值和一些 matplotlib 檔案(關于軸在這里)。我嘗試了一些ax.get_children()(ref)但沒有任何用處。資料應該在 x 和 y 軸上(在理論上,據我了解,但它可能只是指繪圖物理上的內容,而不是資料):遵循此處參考的后一個問題:
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'
我想:嘿,也許檢查一下這個物件有什么屬性,所以也許有一些'get_x_value'。但我找不到任何與我正在尋找的東西相關的東西(我可能也缺乏知識):
dir(ploti.get_children()[5])
(我可以發布輸出,但它很長而且很冗長。請隨意詢問!)
除了我認為我可以使用的:numpy 轉換繪圖的所有值并獲取軸的最大值;有沒有一種快速的方法來獲得圖中軸的最大值?
uj5u.com熱心網友回復:
您可以通過以下方式獲取資料:
data = ax.lines[0].get_xydata()
然后用 獲取最大值 y 的坐標np.where:
data[np.where(data[:, 1] == max(data[:, 1]))]
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/508693.html
