Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs

Python matplotlib Histogram

by suresh

The Python matplotlib histogram looks similar to the bar chart. However, the data will equally distribute into bins. Each bin represents data intervals, and the matplotlib histogram shows the comparison of the frequency of numeric data against the bins. In Python, you can use the Matplotlib library to plot histogram with the help of pyplot hist function.

The hist syntax to draw matplotlib pyplot histogram is

matplotlib.pyplot.pie(x, bins)

In the above Python histogram syntax, x represents the numeric data that you want to use in the Y-Axis, and bins will use in the X-Axis.

Simple matplotlib Histogram Example

In this pyplot histogram example, we were generating a random array and assigned it to x. Next, we are drawing a python histogram using the hist function. Notice that we haven’t used the bins argument.

import matplotlib.pyplot as plt
import numpy as np

x = np.random.randn(1000)
print(x)

plt.hist(x)

plt.show()

OUTPUT

Python matplotlib Histogram 1

Since we are using the random array, the above image or screenshot might not be the same for you.

The first step to plot a histogram is creating bins using a range of values. However, in the above Python example, we haven’t used the bins argument so that the hist function will automatically create and used default bins. In this example, we used the bins number explicitly by assigning 20 to it. It means, below code will draw a histogram of random numbers, and the data will equally distribute into 20 bins.

import matplotlib.pyplot as plt
import numpy as np

x = np.random.randn(1000)
print(x)

plt.hist(x, bins = 20)

plt.show()

OUTPUT

Python matplotlib Histogram 2

It is another example of the pyplot histogram.

import matplotlib.pyplot as plt
import numpy as np

x = np.random.normal(0, 1, 1000)
print(x)

plt.hist(x, bins = 50)

plt.show()

OUTPUT

Python matplotlib Histogram 3

Python matplotlib Histogram using CSV File

In this matplotlib example, we are using the CSV file to plot a histogram. As you can see from the below code, we are using the Orders quantity as the Y-Axis values.

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt

df = pd.read_excel('/Users/suresh/Downloads/Global_Superstore.xls')

data = df['Quantity']
bins=np.arange(min(data), max(data) + 1, 1)

print(df['Quantity'].count())
plt.hist(df['Quantity'], bins)

plt.show()

OUTPUT

Python matplotlib Histogram 4

Python matplotlib Histogram titles

This pyplot histogram example shows how to add the title to the histogram, X-Axis, and Y-Axis labels.

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt

df = pd.read_excel('/Users/suresh/Downloads/Global_Superstore.xls')

data = df['Quantity']
bins=np.arange(min(data), max(data) + 1, 1)

plt.hist(df['Quantity'], bins)

plt.title('Python Matplotlib Histogram Example')
plt.xlabel('Bins')
plt.ylabel('Order Quatity')
plt.show()

OUTPUT

Python matplotlib Histogram 5

Format pyplot histogram labels

In this pyplot histogram example, we are formatting the font size and color of the X and Y labels, X, and Y ticks. If you notice the below code, we are using the data whose Segment is equal to Consumer.

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt

df = pd.read_excel('/Users/suresh/Downloads/Global_Superstore.xls')

x = df['Segment'] == 'Consumer'
df.where(x, inplace = True)
print(df)

data = df['Quantity']
bins=np.arange(min(data), max(data) + 1, 1)

print(df['Quantity'].count())
plt.hist(df['Quantity'], bins)

plt.title('Python Matplotlib Histogram Example')
plt.xlabel('Bins', fontsize = 15, color = 'b')
plt.ylabel('Order Quatity', fontsize = 15, color = 'b')
plt.xticks(fontsize = 12)
plt.yticks(fontsize = 12)

plt.show()

OUTPUT

Python matplotlib Histogram 6

Multiple Histograms in Python

In this Python example, we are trying to plot multiple histograms using the matplotlib library.

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt

df = pd.read_excel('/Users/suresh/Downloads/Global_Superstore.xls')

data = df['Quantity']
bins = np.arange(min(data), max(data) + 1, 1)

x = df.loc[df['Segment'] == 'Consumer']
y = df.loc[df['Segment'] == 'Corporate']
z = df.loc[df['Segment'] == 'Home Office']

plt.hist(x['Quantity'], bins)
plt.hist(y['Quantity'], bins)
plt.hist(z['Quantity'], bins)

plt.show()

OUTPUT

Python matplotlib Histogram 20

Controlling pyplot histogram bin size

It is the same example that we shown above. However, this time, we changed the size of the bins to static value 5. It means the whole Quantity data distributed into five bins.

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt

df = pd.read_excel('/Users/suresh/Downloads/Global_Superstore.xls')

data = df['Quantity']
bins = 5

x = df.loc[df.Segment == 'Consumer', 'Quantity']
y = df.loc[df.Segment == 'Corporate', 'Quantity']
z = df.loc[df.Segment == 'Home Office', 'Quantity']

plt.hist(x,bins)
plt.hist(y, bins)
plt.hist(z, bins)

plt.show()

OUTPUT

Python matplotlib Histogram 7

If you observe the above two screenshot, you can notice there is a difference in both the histograms. It is because of their change in the bins.

Python matplotlib Histogram legend

While working with multiple values or histograms, it is necessary to identify which one belongs to which category. Otherwise, users will get confused. To solve these issues, you have to enable the legend by using the pyplot legend function. Next, use labels argument of the Python hist function to add labels to each histogram.

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt

df = pd.read_excel('/Users/suresh/Downloads/Global_Superstore.xls')

data = df['Quantity']
bins = np.arange(min(data), max(data) + 1, 1)

x = df.loc[df['Segment'] == 'Consumer']
y = df.loc[df['Segment'] == 'Corporate']
z = df.loc[df['Segment'] == 'Home Office']

plt.hist(x['Quantity'], bins, label = 'Consumer')
plt.hist(y['Quantity'], bins, label = 'Corporate')
plt.hist(z['Quantity'], bins, label = 'Home Office')

plt.legend()
plt.show()

OUTPUT

Python matplotlib Histogram 8

Format Python matplotlib Histogram Colors

Whether it is one or more, Python matplotlib will automatically assign the default colors to the histogram. However, you can use the color argument of the pyplot hist function to alter the color. In this example, we are assigning maroon to the first histogram, blue to second, and green to the third histogram.

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt

df = pd.read_excel('/Users/suresh/Downloads/Global_Superstore.xls')

data = df['Quantity']
bins = np.arange(min(data), max(data) + 1, 1)

x = df.loc[df['Segment'] == 'Consumer']
y = df.loc[df['Segment'] == 'Corporate']
z = df.loc[df['Segment'] == 'Home Office']

plt.hist(x['Quantity'], bins, label = 'Consumer', color = 'maroon')
plt.hist(y['Quantity'], bins, label = 'Corporate', color = 'blue')
plt.hist(z['Quantity'], bins, label = 'Home Office', color = 'green')

plt.legend()
plt.show()

OUTPUT

Python matplotlib Histogram 9

Similarly, you can alter the edge colors and opacity using alpha and edgecolor argument.

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt

df = pd.read_excel('/Users/suresh/Downloads/Global_Superstore.xls'

data = df['Quantity']
bins=np.arange(min(data), max(data) + 1, 1)

plt.hist(df['Quantity'], bins, color = 'red', alpha = 0.8, 
         edgecolor = 'g')

plt.title('Python Matplotlib Histogram Example')
plt.xlabel('Bins', fontsize = 15, color = 'b')
plt.ylabel('Order Quatity', fontsize = 15, color = 'b')
plt.xticks(fontsize = 12)
plt.yticks(fontsize = 12)

plt.show()

OUTPUT

Python matplotlib Histogram 10

Python matplotlib Horizontal Histogram

The pyplot hist function has an orientation argument with two options, and they are horizontal and vertical (default). If you use this orientation argument as the horizontal, then the histogram will be drawn horizontally.

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt

df = pd.read_excel('/Users/suresh/Downloads/Global_Superstore.xls')

data = df['Quantity']
bins=np.arange(min(data), max(data) + 1, 1)

plt.hist(df['Quantity'], bins, color = 'red',
         alpha = 0.8, orientation = 'horizontal')

plt.title('Python Matplotlib Horizontal Histogram Example')
plt.show()

OUTPUT

Python matplotlib Histogram 11

matplotlib Histogram histtype

The pyplot histogram has a histtype argument, which is useful to change the histogram type from one type to another. There are four types of histograms available in matplotlib, and they are

  • bar: This is the traditional bar-type histogram. If you use multiple data along with histtype as a bar, then those values are arranged side by side.
  • barstacked: When you use the multiple data, those values stacked on top of each other.
  • step: Histogram without filling the bars. Something like a line chart or Waterfall chart.
  • stepfilled: Same as above, but the empty space filled with the default color.
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt

df = pd.read_excel('/Users/suresh/Downloads/Global_Superstore.xls')

data = df['Quantity']
bins=np.arange(min(data), max(data) + 1, 1)

fix, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize = (8, 4))

x = df.loc[df['Segment'] == 'Consumer']
y = df.loc[df['Segment'] == 'Corporate']
z = df.loc[df['Segment'] == 'Home Office']

ax1.hist(x['Quantity'], bins, histtype = 'bar',
         color = 'red', alpha = 0.8, edgecolor = 'g')
ax2.hist(y['Quantity'], bins, color = 'blue', histtype = 'step')
ax3.hist(z['Quantity'], bins, color = 'green',  histtype = 'stepfilled')

plt.show()

OUTPUT

Python matplotlib Histogram 12

matplotlib Cumulative Histogram

The Python histogram log argument value accepts a boolean value, and its default is False. If you set this True, then the Matplotlib histogram axis will be set on a log scale. Apart from this, there is one more argument called cumulative, which helps display the cumulative histogram.

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt

df = pd.read_excel('/Users/suresh/Downloads/Global_Superstore.xls')

data = df['Quantity']
bins=np.arange(min(data), max(data) + 1, 1)

plt.hist(df['Quantity'], bins, color = 'red', alpha = 0.8,
         cumulative = True, log = True)

plt.title('Python Matplotlib Horizontal Histogram Example')

plt.show()

OUTPUT

Python matplotlib Histogram 13

Python seaborn Histogram

In Python, we have a seaborn module, which helps to draw a histogram along with a density curve. It is very simple and straightforward.

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

x = np.random.randn(1000)
print(x)

sns.distplot(x)

plt.show()

OUTPUT

Python matplotlib Histogram 14

Python matplotlib 2d Histogram

The Python pyplot has a hist2d function to draw a two dimensional or 2D histogram. And to draw matplotlib 2D histogram, you need two numerical arrays or array-like values.

import matplotlib.pyplot as plt
import numpy as np

x = np.random.randn(100)
print(x)

y = 2 * np.random.randn(100)
print(y)

plt.hist2d(x, y)

plt.show()

OUTPUT

Python matplotlib Histogram 15

In this example, we were using two subplots and changed the bin size.

import matplotlib.pyplot as plt
import numpy as np

x = np.random.randn(100)
print(x)

y = 2 * np.random.randn(100)
print(y)

fig, (ax1, ax2) = plt.subplots(1, 2)

ax1.hist2d(x, y, bins = 5)

ax2.hist2d(x, y, bins = 10)
plt.show()

OUTPUT

Python matplotlib Histogram 16

It is another example of the Python Matplotlib 2D histogram.

import matplotlib.pyplot as plt
import numpy as np

x = np.random.randn(10000)
print(x)

y = 2 * np.random.randn(10000)
print(y)

fig, (ax1, ax2) = plt.subplots(1, 2)

ax1.hist2d(x, y, bins = (10, 10))

ax2.hist2d(x, y, bins = (200, 200))
plt.show()

OUTPUT

Python matplotlib Histogram 17

Let me change the colors of a Python histogram using the cmap argument.

import matplotlib.pyplot as plt
import numpy as np

x = np.random.randn(10000)
print(x)

y = 2 * np.random.randn(10000)
print(y)

fig, (ax1, ax2) = plt.subplots(1, 2)

ax1.hist2d(x, y, bins = (10, 10), cmap = 'cubehelix')

ax2.hist2d(x, y, bins = (200, 200), cmap = 'rainbow')
plt.show()

OUTPUT

Python matplotlib Histogram 18

Python matplotlib Histogram of an Image

Apart from the above-specified ones, you can use the Python Matplotlib histogram to analyze the colors in an image. In this example, we use the histogram to show the RGB colors In an image.

import cv2
from matplotlib import pyplot as plt

img = cv2.imread('/Users/suresh/Downloads/IMG_2065.JPG', 0)

plt.hist(img.ravel(), bins = 256, range = [0, 256])
plt.show()

OUTPUT

Python matplotlib Histogram 19

Placed Under: Python

  • Download and Install Python
  • Python Arithmetic Operators
  • Python Assignment Operators
  • Python Bitwise Operators
  • Python Comparison Operators
  • Python Logical Operators
  • Python If Statement
  • Python If Else
  • Python Elif Statement
  • Python Nested If
  • Python For Loop
  • Python While Loop
  • Python Break
  • Python Continue
  • Python Dictionary
  • Python datetime
  • Python String
  • Python Set
  • Python Tuple
  • Python List
  • Python List Comprehensions
  • Python Lambda Function
  • Python Functions
  • Python Types of Functions
  • Python Iterator
  • Python File
  • Python Directory
  • Python Class
  • Python classmethod
  • Python Inheritance
  • Python Method Overriding
  • Python Static Method
  • Connect Python and SQL Server
  • Python SQL Create DB
  • Python SQL Select Top
  • Python SQL Where Clause
  • Python SQL Order By
  • Python SQL Select Statement
  • Python len Function
  • Python max Function
  • Python map Function
  • Python print Function
  • Python sort Function
  • Python range Function
  • Python zip Function
  • Python Math Functions
  • Python String Functions
  • Python List Functions
  • Python NumPy Array
  • NumPy Aggregate Functions
  • NumPy Arithmetic Operations
  • Python Numpy Bitwise operators
  • Numpy Comparison Operators
  • Numpy Exponential Functions
  • Python Numpy logical operators
  • Python Numpy String Functions
  • NumPy Trigonometric Functions
  • Python random Array
  • Python Numpy concatenate
  • Python Numpy Array shape
  • Python pandas DataFrame
  • Pandas DataFrame plot
  • Python Series
  • Python matplotlib Histogram
  • Python matplotlib Scatter Plot
  • Python matplotlib Pie Chart
  • Python matplotlib Bar Chart
  • Python List Length
  • Python sort List Function
  • Python String Concatenation
  • Python String Length
  • Python Substring
  • Python Programming Examples
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy