Plot Histograms in Python using matplotlib

Plot Histograms in Python using matplotlib and seaborn

In this Python tutorial video and notebook, you’ll learn how to create and customize histograms using the powerful libraries matplotlib, seaborn and numpy. Whether you’re a beginner or an experienced data analyst, histograms are a great way to visualize your data and gain insights quickly.

This step-by-step guide will show you how to plot histograms in Python, including how to add useful details to your plots like titles, labels, and legends. You’ll also learn how to adjust the bin size, color, and transparency of your histograms to make them more visually appealing and informative.

By the end of this tutorial, you’ll have a solid understanding of how to create and customize histograms in Python using matplotlib and numpy. This skill is essential for anyone working with data, whether you’re in finance, healthcare, or any other field that requires data analysis.

Tutorial Video

Python Code

Basic Histogram

import matplotlib.pyplot as plt
import numpy as np

# Generate random data for the histogram
data = np.random.normal(loc=50, scale=10, size=1000)  # Generate data from a normal distribution with mean 50 and standard deviation 10

# Plotting a basic histogram
plt.hist( data, bins=30, color='lightgreen', edgecolor='black', alpha=0.7 )  # Adjusting color and transparency

# Adding labels and title
plt.xlabel('Values')
plt.ylabel('Frequency')
plt.title('Customized Histogram')

# Adding grid lines
plt.grid(True, linestyle='--', alpha=0.5)

# Adding text annotation
plt.text( 65, 70, 'Mean: 50\nStd Dev: 10', fontsize=12, bbox=dict(facecolor='white', alpha=0.5))

# Display the plot
plt.show()

Histogram in with Density Plot

iimport matplotlib.pyplot as plt
import numpy as np
import seaborn as sns

# Generate random data for the histogram
data = np.random.normal(loc=50, scale=10, size=1000)  # Generate data from a normal distribution with mean 50 and standard deviation 10

# Creating a customized histogram with a density plot
sns.histplot(data, bins=30, kde=True, color='lightgreen', edgecolor='black', alpha=0.7)

# Adding labels and title
plt.xlabel('Values')
plt.ylabel('Frequency / Density')  # Adjusting ylabel to reflect the addition of KDE
plt.title('Histogram with Kernel Density Estimate')

# Adding grid lines
plt.grid(True, linestyle='--', alpha=0.5)

# Adding text annotation
plt.text(65, 70, 'Mean: 50\nStd Dev: 10', fontsize=12, bbox=dict(facecolor='white', alpha=0.5))

# Display the plot
plt.show()