Plot Histogram in Python using Matplotlib

You may use the following template to plot a histogram in Python using Matplotlib:

`import matplotlib.pyplot as pltx = [value1, value2, value3,....]plt.hist(x, bins=number of bins)plt.show()`

Steps to plot a histogram in Python using Matplotlib

Step 1: Install the Matplotlib package

If you haven’t already done so, install the Matplotlib package using the following command:

`pip install matplotlib`

Step 2: Collect the data for the histogram

For example, let’s say that you have the following data about the age of 100 individuals:

Step 3: Determine the number of bins

Next, determine the number of bins to be used for the histogram.

For simplicity, set the number of bins to 10.

Step 4: Plot the histogram in Python using matplotlib

Finally, plot the histogram based on the following template:

`import matplotlib.pyplot as pltx = [value1, value2, value3,....]plt.hist(x, bins=number of bins)plt.show()`

For our example:

`import matplotlib.pyplot as pltx = [1, 1, 2, 3, 3, 5, 7, 8, 9, 10,     10, 11, 11, 13, 13, 15, 16, 17, 18, 18,     18, 19, 20, 21, 21, 23, 24, 24, 25, 25,     25, 25, 26, 26, 26, 27, 27, 27, 27, 27,     29, 30, 30, 31, 33, 34, 34, 34, 35, 36,     36, 37, 37, 38, 38, 39, 40, 41, 41, 42,     43, 44, 45, 45, 46, 47, 48, 48, 49, 50,     51, 52, 53, 54, 55, 55, 56, 57, 58, 60,     61, 63, 64, 65, 66, 68, 70, 71, 72, 74,     75, 77, 81, 83, 84, 87, 89, 90, 90, 91     ]plt.hist(x, bins=10)plt.show()`

Run the code, and you’ll get the histogram.

If needed, you can further style your histogram. One way to style your histogram is by adding this syntax towards the end of the code:

`plt.style.use("ggplot")`

For our example:

`import matplotlib.pyplot as pltx = [1, 1, 2, 3, 3, 5, 7, 8, 9, 10,     10, 11, 11, 13, 13, 15, 16, 17, 18, 18,     18, 19, 20, 21, 21, 23, 24, 24, 25, 25,     25, 25, 26, 26, 26, 27, 27, 27, 27, 27,     29, 30, 30, 31, 33, 34, 34, 34, 35, 36,     36, 37, 37, 38, 38, 39, 40, 41, 41, 42,     43, 44, 45, 45, 46, 47, 48, 48, 49, 50,     51, 52, 53, 54, 55, 55, 56, 57, 58, 60,     61, 63, 64, 65, 66, 68, 70, 71, 72, 74,     75, 77, 81, 83, 84, 87, 89, 90, 90, 91     ]plt.style.use("ggplot")plt.hist(x, bins=10)plt.show()`

Run the code, and you’ll get the styled histogram.

Optionally, you can derive the skew in Python using the scipy library:

`import numpy as npfrom scipy.stats import skewx = np.array([1, 1, 2, 3, 3, 5, 7, 8, 9, 10,              10, 11, 11, 13, 13, 15, 16, 17, 18, 18,              18, 19, 20, 21, 21, 23, 24, 24, 25, 25,              25, 25, 26, 26, 26, 27, 27, 27, 27, 27,              29, 30, 30, 31, 33, 34, 34, 34, 35, 36,              36, 37, 37, 38, 38, 39, 40, 41, 41, 42,              43, 44, 45, 45, 46, 47, 48, 48, 49, 50,              51, 52, 53, 54, 55, 55, 56, 57, 58, 60,              61, 63, 64, 65, 66, 68, 70, 71, 72, 74,              75, 77, 81, 83, 84, 87, 89, 90, 90, 91              ])print(skew(x))`

Once you run the code in Python, you’ll get the following skew:

``0.4575278444409153``