Plot 2-D Histogram in Python using Matplotlib
Last Updated :
28 Mar, 2023
2D Histogram is used to analyse the relationship among two data variables which has wide range of values.A 2D histogram is very similar like 1D histogram.The class intervals of the data set are plotted on both x and y axis.Unlike 1D histogram, it drawn by including the total number of combinations of the values which occurs in intervals of x and y, and marking the densities.It is useful when there is a large amount of data in a discrete distribution, and simplifies it by visualizing the points where the frequencies if variables is dense.
Creating a 2D Histogram
Matplotlib library provides an inbuilt function
matplotlib.pyplot.hist2d()
which is used to create 2D histogram.Below is the syntax of the function:
matplotlib.pyplot.hist2d(x, y, bins=(nx,ny), range=None, density=False, weights=None, cmin=None, cmax=None, cmap=value)
Here
(x,y)
specify the coordinates of the data variables, the length of the X data and Y variables should be same.The number of bins can be specified by the attribute
bins=(nx,ny)
where
nx
and
ny
is the number of bins to be used in the horizontal and vertical directions respectively.
cmap=value
is used to set the color scale.The
range=None
is an optional parameter used to set rectangular area in which data values are counted for plot.
density=value
is optional parameter accepting boolean values used to normalize histogram.
The code below code creates a simple 2D histogram using
matplotlib.pyplot.hist2d()
function having some random values of x and y:
[sourcecode language="Python3"]
# Import libraries
import numpy as np
import matplotlib.pyplot as plt
import random
# Creating dataset
n = 100
x = np.random.standard_normal(n)
y = 3.0 * x
fig = plt.subplots(figsize=(10,7))
# Creating plot
plot.hist2d(x, y)
plot.title("Simple 2D Histogram")
# show plot
plot.show()
[/sourcecode]
Output:
Customizing 2D Histogram
The
matplotlib.pyplot.hist2d()
function has a wide range of methods which we can use to customize and create the plot for better view and understanding.
[sourcecode language="Python3"]
# Import libraries
import numpy as np
import matplotlib.pyplot as plt
import random
# Creating dataset
x = np.random.normal(size=500000)
y = x * 3 + 4*np.random.normal(size=500000)
fig = plt.subplots(figsize=(10,7))
# Creating plot
plot.hist2d(x, y)
plot.title("Simple 2D Histogram")
# show plot
plot.show()
[/sourcecode]
Output:
Some of the customization of the above graph are listed below:
[sourcecode language="Python3"]
# Import libraries
import numpy as np
import matplotlib.pyplot as plt
import random
# Creating dataset
x = np.random.normal(size=500000)
y = x * 3 + 4*np.random.normal(size=500000)
# Creating bins
x_min = np.min(x)
x_max = np.max(x)
y_min = np.min(y)
y_max = np.max(y)
x_bins = np.linspace(x_min,x_max,50)
y_bins = np.linspace(y_min,y_max,20)
fig,ax = plt.subplots(figsize=(10,7))
# Creating plot
plt.hist2d(x,y,bins=[x_bins,y_bins])
plt.title("Changing the bin scale")
ax.set_xlabel('X-axis')
ax.set_ylabel('X-axis')
# show plot
plt.tight_layout()
plot.show()
[/sourcecode]
Output:
Changing the color scale and adding color bar:-
[sourcecode language="Python3"]
# Import libraries
import numpy as np
import matplotlib.pyplot as plt
import random
# Creating dataset
x = np.random.normal(size=500000)
y = x * 3 + 4*np.random.normal(size=500000)
# Creating bins
x_min = np.min(x)
x_max = np.max(x)
y_min = np.min(y)
y_max = np.max(y)
x_bins = np.linspace(x_min,x_max,50)
y_bins = np.linspace(y_min,y_max,20)
fig,ax = plt.subplots(figsize=(10,7))
# Creating plot
plt.hist2d(x,y,bins=[x_bins,y_bins], cmap=plt.cm.nipy_spectral)
plt.title("Changing the color scale and adding color bar")
# Adding color bar
plt.colorbar()
ax.set_xlabel('X-axis')
ax.set_ylabel('X-axis')
# show plot
plt.tight_layout()
plot.show()
[/sourcecode]
Output:
[sourcecode language="Python3"]
# Import libraries
import numpy as np
import matplotlib.pyplot as plt
import random
# Creating dataset
x = np.random.normal(size=500000)
y = x * 3 + 4*np.random.normal(size=500000)
# Creating bins
x_min = np.min(x)
x_max = np.max(x)
y_min = np.min(y)
y_max = np.max(y)
x_bins = np.linspace(x_min,x_max,50)
y_bins = np.linspace(y_min,y_max,20)
# Creating data filter
data = np.c_[x,y]
for i in range(10000):
x_idx = random.randint(0,500000)
data[x_idx,0] = -9999
data = data[data[:,0]!=-9999]
fig,ax = plt.subplots(figsize=(10,7))
# Creating plot
plt.hist2d(data[:,0],data[:,1],bins=[x_bins,y_bins])
plt.title("Filtering data")
ax.set_xlabel('X-axis')
ax.set_ylabel('X-axis')
# show plot
plt.tight_layout()
plot.show()
[/sourcecode]
Output:
Using matplotlib hexbin function:-
[sourcecode language="Python3"]
# Import libraries
import numpy as np
import matplotlib.pyplot as plt
import random
# Creating dataset
x = np.random.normal(size=500000)
y = x * 3 + 4*np.random.normal(size=500000)
fig,ax = plt.subplots(figsize=(10,7))
# Creating plot
plt.title("Using matplotlib hexbin function")
plt.hexbin(x,y,bins=50)
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
# show plot
plt.tight_layout()
plot.show()
[/sourcecode]
Output:
Similar Reads
Python Tutorial | Learn Python Programming Language Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read