0

Hi I have drown the distribution graphs for my dataset as shown below. I need to add the density line in the graph. I tried out several ways. Can someone shows me how I can add the density line for each of the following graphs

import numpy as np
import matplotlib.pyplot as plt

n_bins = 10

fig, axes = plt.subplots(nrows=1, ncols=3,figsize=(15,5))
ax0, ax1, ax2 = axes.flatten()

# Intitialise data of lists  
A = [{'Quantity':80995},{'Quantity':76919},{'Quantity':49182},{'Quantity':45632},{'Quantity':41981},{'Quantity':34705},{'Quantity':32727},{'Quantity':24337},{'Quantity':22711},{'Quantity':22465}] 
A = pd.DataFrame(A) 

ax0.hist(A['Quantity'], n_bins)
ax0.legend(prop={'size': 10})
ax0.set_title('Product quantity distribution of UK', fontsize=14)
ax0.set_xlabel("Product quantity", fontsize=12)
ax0.set_ylabel("Product Frequency", fontsize=12)

# Intitialise data of lists  
B = [{'Quantity':1233},{'Quantity':1164},{'Quantity':1114},{'Quantity':1020},{'Quantity':1002},{'Quantity':936},{'Quantity':876},{'Quantity':857},{'Quantity':816},{'Quantity':809}] 
B = pd.DataFrame(B)

ax1.hist(B['Quantity'], n_bins, density=True, histtype='bar')
ax1.set_title('Product quantity distribution of Germany', fontsize=14)
ax1.set_xlabel("Product quantity", fontsize=12)
ax1.set_ylabel("Product Frequency", fontsize=12)

# Intitialise data of lists  
C = [{'Quantity':168},{'Quantity':144},{'Quantity':108},{'Quantity':100},{'Quantity':96},{'Quantity':80},{'Quantity':60},{'Quantity':60},{'Quantity':60},{'Quantity':48}] 
C = pd.DataFrame(C)

ax2.hist(C['Quantity'], n_bins, density=True, histtype='bar')
ax2.set_title('Product quantity distribution of Poland', fontsize=14)
ax2.set_xlabel("Product quantity", fontsize=12)
ax2.set_ylabel("Product Frequency", fontsize=12)

fig.tight_layout()
plt.show()

enter image description here

user3789200
  • 1,166
  • 2
  • 25
  • 45
  • you should share some sample data to make it easier to help you. – Chris Dec 14 '20 at 21:38
  • 1
    Perhaps looking for [this](https://stackoverflow.com/questions/33323432/add-kde-on-to-a-histogram)? Probs a lot easier with seaborn too. – BigBen Dec 14 '20 at 21:38
  • 1
    You seem to only have about 10 values per histogram. That seems way too low to create an insightful density curve. – JohanC Dec 14 '20 at 22:06
  • Hi... :) Yes I have been given only 10 data points. Does this mean that I'm unable to draw this? – user3789200 Dec 14 '20 at 22:14
  • 1
    @user3789200 Yes, you shouldn't try to estimate the density here, it'll lead you nowhere. Or at least it would carry a unreasonably large error with it. Following [this answer](https://stats.stackexchange.com/a/140986/76970), you'd need at least double the amount of data and, _most importantly_, you'd need to know whether you can even expect that the (additional) data follows a distribution! – Asmus Dec 16 '20 at 07:56

0 Answers0