w3resource

NumPy: Compute the histogram of a set of data

NumPy: Array Object Exercise-116 with Solution

Write a NumPy program to compute the histogram of a set of data.

Sample Solution:

Python Code:

import numpy as np    
import matplotlib.pyplot as plt
plt.hist([1, 2, 1], bins=[0, 1, 2, 3, 5])
plt.show()

Sample Output:

Histogram image

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a NumPy program to find indices of elements equal to zero in a numpy array.
Next: Write a NumPy program to compute the line graph of a set of data.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Set comprehension:

>>> m = {x ** 2 for x in range(5)}
>>> m
{0, 1, 4, 9, 16}