[英]How to set multiple mean values in subplotted histograms with pandas Dataframe
如果我使用具有多個標頭的DataFrame的pd.DataFrame.hist()
函數,則python將繪制多個直方圖。
我想用plt.axvline
函數用數據幀的平均值繪制平均值。 這不行
我已經嘗試過只使用一個數據幀頭,並且它可以工作。
def plot_HistOfDailyReturn(p_df, p_bins=10):
"""plots the histogram of the daily returns"""
df1 = pd.DataFrame(p_df['HCP'])
df1.hist(bins=p_bins)
plt.axvline(df1['HCP'].mean(), color='w', linestyle='dashed', linewidth=2)
plt.show()
我現在如何將其應用於幾個? 但我不想讓他們每個人都處於單獨的情節中。
DataFrame.hist()返回其中的matplotlib.AxesSubplot
或numpy.ndarray
。 如果您有多列,則很可能會返回matplotlib.AxesSubplot
的2D numpy.ndarray
。 您可以使用這些AxesSubplot來在每個子圖中繪制線條。
演示:
In [199]: data = pd.DataFrame(np.random.randint(100, size=(1000,4)), columns=list("abcd"))
In [200]: data.shape
Out[200]: (1000, 4)
In [201]: (a, b), (c, d) = data.hist(bins=100, alpha=0.8, figsize=(12, 8))
In [202]: a.axvline(data["a"].mean(), color='orange', linestyle='dashed', linewidth=2)
Out[202]: <matplotlib.lines.Line2D at 0x149c220a4e0>
In [203]: b.axvline(data["b"].mean(), color='orange', linestyle='dashed', linewidth=2)
Out[203]: <matplotlib.lines.Line2D at 0x149c220aa58>
In [204]: c.axvline(data["c"].mean(), color='orange', linestyle='dashed', linewidth=2)
Out[204]: <matplotlib.lines.Line2D at 0x149c098f5f8>
In [205]: d.axvline(data["d"].mean(), color='orange', linestyle='dashed', linewidth=2)
Out[205]: <matplotlib.lines.Line2D at 0x149c222d550>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.