If I use the pd.DataFrame.hist()
func of a DataFrame with multiple headers, then python will plot multiple histograms.
I wanted to plot the mean with the plt.axvline
function with the mean of the dataframe. This is not working
I have already tried it with only one header of a dataframe and it worked.
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()
How can I now apply this to several ones? But I don't want want them to be each in a seperate plot.
DataFrame.hist() returns a matplotlib.AxesSubplot
or numpy.ndarray
of them. If you have multiple columns, then most probably it'll return a 2D numpy.ndarray
of matplotlib.AxesSubplot
. You can use those AxesSubplot's in order to draw lines in each subplot.
Demo:
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>
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.