Skip to content

Instantly share code, notes, and snippets.

@aryan-f
Created August 2, 2019 18:47
Show Gist options
  • Save aryan-f/0ed49216f73ae6eab6f5f108d3d12b0f to your computer and use it in GitHub Desktop.
Save aryan-f/0ed49216f73ae6eab6f5f108d3d12b0f to your computer and use it in GitHub Desktop.
A little code to plot a histogram of some data in order to investigate its distribution.
import numpy as np
import matplotlib.pyplot as plt
# Store your values inside the 'values' variable as a iteratable (i.e. list)
# I'll just use some random numbers instead
values = np.random.normal(size=10000)
# The line below renders the chart and saves it to memory
# 50 is the number of bins, density scales the chart so the area under the graph would be 1, the other two are just some appearance controls
n, bins, patches = plt.hist(values, 50, density=True, facecolor='g', alpha=0.75)
# The lines below set the labels for the axes (I don't know if it supports Persian :D)
plt.xlabel('Value')
plt.ylabel('Relative Frequency')
# This sets the title of the plot
plt.title('Histogram of values')
# Let's also show the mean and variance of the data
plt.text(-4, .35, r'$\mu={:.2f},\ \sigma={:.2f}$'.format(np.mean(values), np.std(values)))
# This just shows a grid on the chart for more accurate measuring
plt.grid(True)
# This finally shows the chart we've been building up
plt.show()
print('You should see a green histogram which divides your data into 50 bins (columns) which when put together \n'
'looks like the normal distribution. This in fact means that the distribution of the data is normal! I \n'
'actually used numpy to draw some random samples from the normal distribution but when you replace values \n'
'with the one of your own it might not look like this which is absolutely fine! That\'s just the distribution \n'
'of your data which you might need to look online to see if it matches any well known distribution like Poisson \n'
'or something :D')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment