How to plot a line graph from histogram data in Matplotlib?

To plot a line graph from histogram data in Matplotlib, we use NumPy's histogram() method to compute the histogram bins and frequencies, then plot them as a line graph.

Steps

  • Generate or prepare your data array

  • Use np.histogram() to compute histogram bins and frequencies

  • Calculate bin centers from bin edges

  • Plot the line graph using plot() with bin centers and frequencies

  • Display the plot using show()

Basic Example

Here's how to create a line graph from histogram data ?

import numpy as np
import matplotlib.pyplot as plt

# Generate sample data
data = np.random.normal(50, 15, 1000)

# Compute histogram
counts, bin_edges = np.histogram(data, bins=30)

# Calculate bin centers
bin_centers = 0.5 * (bin_edges[1:] + bin_edges[:-1])

# Plot line graph
plt.figure(figsize=(10, 6))
plt.plot(bin_centers, counts, '-o', linewidth=2, markersize=4)
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.title('Line Graph from Histogram Data')
plt.grid(True, alpha=0.3)
plt.show()

Comparing Histogram and Line Graph

Let's compare both visualizations side by side ?

import numpy as np
import matplotlib.pyplot as plt

# Generate sample data
np.random.seed(42)
data = np.random.normal(100, 20, 500)

# Create subplots
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(10, 8))

# Plot histogram
ax1.hist(data, bins=25, edgecolor='black', alpha=0.7, color='skyblue')
ax1.set_title('Histogram')
ax1.set_xlabel('Value')
ax1.set_ylabel('Frequency')

# Compute histogram data
counts, bin_edges = np.histogram(data, bins=25)
bin_centers = 0.5 * (bin_edges[1:] + bin_edges[:-1])

# Plot line graph
ax2.plot(bin_centers, counts, '-o', linewidth=2, markersize=5, color='red')
ax2.set_title('Line Graph from Histogram Data')
ax2.set_xlabel('Value')
ax2.set_ylabel('Frequency')
ax2.grid(True, alpha=0.3)

plt.tight_layout()
plt.show()

Customizing the Line Graph

You can customize the appearance with different styles and colors ?

import numpy as np
import matplotlib.pyplot as plt

# Generate sample data
data = np.random.exponential(2, 800)

# Compute histogram
counts, bin_edges = np.histogram(data, bins=40)
bin_centers = 0.5 * (bin_edges[1:] + bin_edges[:-1])

# Create multiple styled line plots
plt.figure(figsize=(12, 8))

plt.subplot(2, 2, 1)
plt.plot(bin_centers, counts, '-', linewidth=3, color='blue')
plt.title('Solid Line')
plt.grid(True, alpha=0.3)

plt.subplot(2, 2, 2)
plt.plot(bin_centers, counts, '--', linewidth=2, color='green')
plt.title('Dashed Line')
plt.grid(True, alpha=0.3)

plt.subplot(2, 2, 3)
plt.plot(bin_centers, counts, '-.', linewidth=2, color='orange')
plt.title('Dash-dot Line')
plt.grid(True, alpha=0.3)

plt.subplot(2, 2, 4)
plt.plot(bin_centers, counts, '-o', linewidth=2, markersize=6, color='purple')
plt.title('Line with Markers')
plt.grid(True, alpha=0.3)

plt.tight_layout()
plt.show()

Key Parameters

Parameter Description Example
bins Number of histogram bins bins=30
linewidth Width of the line linewidth=2
marker Marker style for data points marker='o'
color Line color color='red'

Conclusion

Use np.histogram() to compute bin frequencies and plt.plot() to create a line graph from histogram data. This approach is useful for analyzing data distribution trends and creating smooth visualizations of frequency data.

Updated on: 2026-03-25T19:47:45+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements