Plotting a dataframe as both a 'hist' and 'kde' on the same plot

后端 未结 3 735
心在旅途
心在旅途 2020-12-30 07:08

I have a pandas dataframe with user information. I would like to plot the age of users as both a kind=\'kde\' and on kind=\'hist\' on

3条回答
  •  离开以前
    2020-12-30 07:30

    In case you want it for all the columns of your dataframe:

    fig, ax = plt.subplots(8,3, figsize=(20, 50)) 
    # you can change the distribution, I had 22 columns, so 8x3 is fine to me
    fig.subplots_adjust(hspace = .2, wspace=.2, )
    
    ax = ax.ravel()
    
    for i in range(len(I_df.columns)):
        ax[i] = I_df.iloc[:,i].plot(kind='hist', ax=ax[i])
        ax[i] = I_df.iloc[:,i].plot(kind='kde', ax=ax[i], secondary_y=True)
        plt.title(I_df.columns[i])
    

    I hope it helps :)

提交回复
热议问题