Back to Browse

2#How to Construct a Histogram in Python | Class Intervals & Frequencies using on Google Colab

59 views
Aug 20, 2025
6:51

Welcome to this detailed tutorial by Prof. Jasbir Singh, HoD Computer Science in series for COSM Practals 2 : Construct a histogram for a given dataset: Accept numerical data, define class intervals, and count frequencies. Use matplotlib.pyplot.hist() to plot a histogram using the same intervals and frequency. . In this video, you will learn step by step how to construct a histogram in Python by: Accepting numerical data Defining class intervals (bins) Counting frequencies Plotting a histogram using matplotlib Creating a frequency distribution table using Pandas We will use Google Colab for implementation, which makes it easy for anyone to run the code without installation issues. 🖥️ Code Explanation (Line by Line) import matplotlib.pyplot as plt import numpy as np import pandas as pd 👉 We import three important Python libraries: matplotlib.pyplot → for plotting histograms and other graphs. numpy (np) → for handling numerical arrays and datasets. pandas (pd) → for creating tables (frequency distribution). # Sample numerical data (replace with your actual data) data = np.array([1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 0, 6, 7, 8, 9, 10, 12, 13, 11, 4, 12, 6]) 👉 We create a NumPy array called data that stores our sample numerical dataset. You can replace this with any dataset you want to analyze. # Define class intervals bins = [0, 5, 10, 15] # Intervals: 0-5, 5-10, 10-15 👉 We define class intervals (bins). Here, the data will be grouped into: 0–5 5–10 10–15 By default, matplotlib considers the left edge inclusive and the right edge exclusive (except the last bin). # Calculate frequencies and plot the histogram hist, bin_edges, patches = plt.hist(data, bins=bins, edgecolor='black') 👉 This line does two things: Calculates frequencies of values in each interval. Plots a histogram with black edges for clarity. The result is stored in: hist → frequencies for each interval bin_edges → edges of the bins (class limits) patches → the rectangular bars of the histogram # Display class intervals and frequencies in a table intervals = [f'{int(bin_edges[i])}-{int(bin_edges[i+1])}' for i in range(len(bin_edges)-1)] 👉 We create a list of interval labels like "0-5", "5-10", "10-15" using the bin_edges. freq_table = pd.DataFrame({'Class Interval': intervals, 'Frequency': hist.astype(int)}) display(freq_table) 👉 Using Pandas DataFrame, we create a frequency distribution table that shows: Class Interval Frequency of values in each interval We also convert frequencies into integers for a clean table. # Add labels and title plt.xlabel('Data Values') plt.ylabel('Frequency') plt.title('Histogram with Defined Intervals') 👉 We label the x-axis, y-axis, and give the histogram a title for better readability. # Set x-axis ticks plt.xticks(bins) 👉 This sets the x-axis ticks at the class boundaries (0, 5, 10, 15) for clarity. # Show the plot plt.show() 👉 Finally, we display the histogram on the screen. 📊 Output of the Program: A histogram graph with data distributed across the defined intervals. A frequency distribution table showing class intervals and corresponding frequencies. 🔑 Key Learning Points: How to group data into intervals (bins) How to count frequencies automatically How to plot a histogram using Matplotlib How to create frequency tables using Pandas Practical use of Google Colab for data visualization

Download

0 formats

No download links available.

2#How to Construct a Histogram in Python | Class Intervals & Frequencies using on Google Colab | NatokHD