Plotting histogram for all features with titles

Viewed 1439

I wrote the following code to plot histograms of all the features in my dataframe dff. My code snippet:

import matplotlib.pyplot as plt
dff.head()
for i in dff.columns:
        plt.figure()
        plt.hist(dff[i])

However the histograms just get plotted without the feature name / column name.Is there a way i could also print the column name below each of distribution charts so that i can relate which distribution corresponds to which column?

1 Answers

this is the first time I actually got chance to answer a question.

if you add this line it will print the column name as the title, you can add other text to it as well. This is a super useful little function

plt.title(f'{i}')

here is it in your code

dff.head()
for i in dff.columns:
    
    plt.figure()
    plt.title(f'{i}')
    plt.hist(dff[i])

*edited this as I put the title in the wrong place originally

Related