Search code examples
pythonplotseabornhistogramdistribution

How to plot histogram and distribution from frequency table?


I have a frequency table

frequency table

and I have been trying to plot this data into something like this,

histogram with distribution curve

so tried this,

to_plot = compare_df[['counts', 'theoritical counts']]
bins=[0,2500,5000,7500,10000,12500,15000,17500,20000]
sns.displot(to_plot,bins=bins)

but, it turned out to be like this, plot

Any idea what I did wrong? Please help.


Solution

  • Couple of things:

    1. when you provide a DataFrame to sns.displot you would need also to specify which column to use for the distribution as the x kwarg.

    2. this leads into the 2nd issue: I don't know of a way to get multiple distributions using sns.displot, but you can use sns.histplot in approximately this way:

    import matplotlib.pyplot as plt
    import seaborn as sns 
    
    titanic = sns.load_dataset('titanic')
    
    ax = sns.histplot(data=titanic,x='age',bins=30,color='r',alpha=.25,
                      label='age')
    sns.histplot(data=titanic,x='fare',ax=ax,bins=30,color='b',alpha=.25,
                 label='fare')         
    ax.legend()
    plt.show()
    

    Result below, and please note that I just used an example dataset to get you a rough image as quickly as possible:

    enter image description here