# Histograms Plots

First, we need to know what frequency tables are. Suppose you have a set of members with various values. We can create a table that has various buckets of ranges of values in a column. Each bucket must have at least one value. Then we can count the number of members that fall into that bucket and note those counts against the buckets.

If bins is an integer, it defines the number of equal-width bins in the range.

If bins is a sequence, it defines the bin edges, including the left edge of the first bin and the right edge of the last bin; in this case, bins may be unequally spaced.

bins is interval. (default: 10)

In [1]:
import numpy as np
import matplotlib.pyplot as plt

In [2]:
scores = [1, 3, 5, 1, 2, 4, 4, 2, 5, 4, 3, 1, 2]

In [3]:
plt.hist(scores)
plt.show()

In [4]:
plt.hist(scores, bins = 5)
plt.show()


The histogram of one-dimensional data is a 2D figure.

In [5]:
plt.hist(scores, bins = 5, edgecolor="red")
plt.show()

In [6]:
plt.hist(scores, bins = 15, edgecolor="red")
plt.show()


### Generate data from numpy

In [7]:
x = np.random.randn(1000)
x[:20]

Out[7]:
array([-0.29370312,  0.59815106, -2.28036393, -0.04559174, -1.5445421 ,
0.43417658,  1.19630526, -0.08432642, -0.25696322,  0.14830154,
-1.87736922,  1.21561781, -1.00686897, -1.47097589, -1.46002049,
1.20130276,  0.19590106, -1.21030835,  0.48481554, -0.11487742])
In [8]:
plt.hist(x)
plt.show()

In [9]:
plt.hist(x, bins=30)
plt.show()

In [10]:
plt.hist(x, bins=30, density=True, histtype='step')
plt.show()


## Multiple histogram

In [11]:
x1 = np.random.normal(0, 0.8, 1000)
x2 = np.random.normal(-2, 1, 1000)
x3 = np.random.normal(3, 2, 1000)

In [12]:
print(x1[:10])
print(x2[:10])
print(x3[:10])

[-0.17466897 -0.00390251 -0.36487302 -0.60805626  0.95551434 -0.49293387
0.24564901  0.45859673 -0.48883817 -0.00886988]
[-3.63540801 -1.91342141 -2.05799681 -2.13110339 -1.53395593 -1.66502518
-2.01662326 -0.6786185  -2.24570154 -2.86612388]
[3.14102811 4.13972813 5.83451644 2.35987244 2.99004212 2.67943733
3.09266463 0.7885563  1.10564708 0.99033539]

In [13]:
plt.hist(x1)
plt.hist(x2)
plt.hist(x3);

In [14]:
kwargs = dict(alpha=0.3, histtype='stepfilled', bins=40)

plt.hist(x1, **kwargs)
plt.hist(x2, **kwargs)
plt.hist(x3, **kwargs)
plt.show()

In [15]:
kwargs = dict(alpha=0.3, histtype='stepfilled', bins=40, facecolor = 'g')

plt.hist(x1, **kwargs)
plt.hist(x2, **kwargs)
plt.hist(x3, **kwargs)
plt.show()


### Title, legend , x-axis and y-axis label

In [16]:
kwargs = dict(alpha=0.3, histtype='stepfilled', bins=40)

plt.hist(x1, **kwargs, label = 'x1')
plt.hist(x2, **kwargs, label = 'x2')
plt.hist(x3, **kwargs, label = 'x3')

plt.title('Histogram')
plt.xlabel("X-axis")
plt.ylabel("y-axis")
plt.legend()

plt.show()


## Two-Dimensional Histograms

In [17]:
n_points = 10000
bins = 50

In [18]:
x = np.random.randn(n_points)
y = np.random.randn(n_points)

In [19]:
print(x[:10])
print(y[:10])

[ 1.13593219  0.43899878  0.13246305  1.93051206 -1.16784114  0.56138205
0.25602927  0.39125038 -0.08377408  1.00217832]
[-0.28071078 -0.70062961  0.32065733  0.94901202 -0.20692571 -0.36116829
0.75868523 -1.06383209 -0.36806428  1.34531627]

In [20]:
plt.hist2d(x, y, bins=50)
plt.show()