- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to plot a line graph from histogram data in Matplotlib?
To plot a line graph from histogram data in matplotlib, we use numpy histogram method to compute the histogram of a set of data.
Steps
Add a subplot to the current figure, nrows=2, ncols=1 and index=1.
Use numpy histogram method to get the histogram of a set of data.
Plot the histogram using hist() method with edgecolor=black.
At index 2, use the computed data (from numpy histogram). To plot them, we can use plot() method.
To display the figure, use show() method.
Example
import numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True plt.subplot(211) data = np.array(np.random.rand(100)) y, binEdges = np.histogram(data, bins=100) plt.hist(data, bins=100, edgecolor='black') plt.subplot(212) bincenters = 0.5 * (binEdges[1:] + binEdges[:-1]) plt.plot(bincenters, y, '-', c='black') plt.show()
Output
- Related Articles
- How to plot a 2D histogram in Matplotlib?
- How to plot hexbin histogram in Matplotlib?
- How to plot a histogram using Matplotlib in Python with a list of data?
- How to center labels in a Matplotlib histogram plot?
- How to make a histogram from a list of data in Matplotlib?
- How to plot collections.Counter histogram using Matplotlib?
- How to extract data from a Matplotlib plot?
- Plot a Line Graph for Pandas Dataframe with Matplotlib?
- Plot a histogram with colors taken from colormap in Matplotlib
- How to plot a bar graph in Matplotlib from a Pandas series?
- How to show a bar and line graph on the same plot in Matplotlib?
- Plotting a histogram from pre-counted data in Matplotlib
- How to vary the line color with data index for line graph in matplotlib?
- How to plot a high resolution graph in Matplotlib?
- How to animate a line plot in Matplotlib?

Advertisements