• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Data Viz with Python and R

Learn to Make Plots in Python and R

  • Home
  • Python Viz
  • Seaborn
  • Altair
  • R Viz
  • ggplot2
  • About
    • Privacy Policy
  • Show Search
Hide Search

How To Make Histograms with Matplotlib in Python?

datavizpyr · February 27, 2020 ·

In this post, we will see an example of how to make histogram with Matplotlib library in Python. Matplotlib’s pyplot module is designed to make a number of types of plots with Matplotlib. We will use pyplot module’s function hist() to make histograms with matplotlib.

Let us load Matplotlib.pyplot and Numpy library.

import matplotlib.pyplot as plt
import numpy as np

We will generate data to make a histogram using NumPy’s random module. Here were using NumPy.random’s normal distribution function to generate random numbers for our data.

# set seed for reproducing
np.random.seed(42)
# specify mean
mean_mu = 60
specify Standard deviation
sd_sigma = 15
# number of data points
n = 5000
# generate random numbers from normal distribution
data = np.random.normal(mean_mu, sd_sigma, n)

Matplotlib’ Pyplot has hist() function that takes the data as input and makes histogram. In addition to data, hist() function can take a number of arguments to customize the histogram.

Here we specify the number of bins with “bins=100″ argument and specify we want frequency histogram with ” density=False” argument. We also specify transparency levels for the fill color with alpha=0.75.

# the histogram of the data
n, bins, patches = plt.hist(data, bins=100, density=False, alpha=0.75)
plt.xlabel('data',size=16)
plt.ylabel('Counts',size=16)
plt.title('Histogram with Matplotlib',size=18)

pyplot’s hist function also returns three tuples with all the information needed for histogram. And be default, pyplot’s hist() function colors the histogram in blue as shown below.

How To Make Histogram with Matplotllib?
How To Make Histogram with Matplotllib?

Related posts:

How To Adjust Axes Label Position in Matplotlib?How To Adjust Positions of Axis Labels in Matplotlib? Add Rectangle with text annotation MatplotlibHow To Draw a Rectangle on a Plot in Matplotlib? Connect Paired Points with Lines in MatplotlibHow To Connect Paired Data Points with Lines in Scatter Plot with Matplotlib Matplotlib Scatter plot with legendHow to Add Legend to Scatterplot Colored by a Variable with Matplotlib in Python

Filed Under: Histogram Matplotlib, Matplotlib, Python Tagged With: Histogram, Matplotlib, Python

Primary Sidebar

Tags

Altair barplot Boxplot boxplot python boxplot with jiitered text labels Bubble Plot Color Palette Countplot Density Plot Facet Plot gganimate ggplot2 ggplot2 Boxplot ggplot2 error ggplot boxplot ggridges ggtext element_markdown() Grouped Barplot R heatmap heatmaps Histogram Histograms Horizontal boxplot Python lollipop plot Maps Matplotlib Pandas patchwork pheatmap Pyhon Python R RColorBrewer reorder boxplot ggplot Ridgeline plot Scatter Plot Scatter Plot Altair Seaborn Seaborn Boxplot Stock Price Over Time Stripplot UpSetR Violinplot Violin Plot World Map ggplot2

Buy Me a Coffee

Copyright © 2025 · Daily Dish Pro on Genesis Framework · WordPress · Log in

Go to mobile version