Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Pandas
  • Numpy
  • Seaborn
  • Ploty
  • Data visualization
  • Data Analysis
  • Power BI
  • Tableau
  • Machine Learning
  • Deep Learning
  • NLP
  • Computer Vision
  • Data Science for Beginners
  • Data Science interview questions
  • Data analysis interview questions
  • NLP Interview questions
Open In App
Next Article:
Box Plot in Python using Matplotlib
Next article icon

Box Plot in Python using Matplotlib

Last Updated : 14 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A Box Plot (or Whisker plot) display the summary of a data set, including minimum, first quartile, median, third quartile and maximum. it consists of a box from the first quartile to the third quartile, with a vertical line at the median. the x-axis denotes the data to be plotted while the y-axis shows the frequency distribution. The matplotlib.pyplot module of matplotlib library provides boxplot() function with the help of which we can create box plots.

Syntax

matplotlib.pyplot.boxplot(data)

The data values given to the ax.boxplot() method can be a Numpy array or Python list or Tuple of arrays. Let us create the box plot by using numpy.random.normal() to create some random data, it takes mean, standard deviation, and the desired number of values as arguments.

Example:  

Python
import matplotlib.pyplot as plt import numpy as np  np.random.seed(10) d = np.random.normal(100, 20, 200)  fig = plt.figure(figsize =(10, 7))  plt.boxplot(d) plt.show() 

Output: 

box-plot-python

The basic box plot that displays the distribution of the randomly generated data.

Customizing Box Plot

The matplotlib.pyplot.boxplot() provides endless customization possibilities to the box plot. some of the key customization parameters include:

  • The notch: True attribute creates the notch format to the box plot
  • patch_artist: True fills the boxplot with colors, we can set different colors to different boxes.
  • vert: 0 attribute creates horizontal box plot.
  • labels: specifies custom labels for the boxes.

Example 1: Multiple Datasets Box Plot

Python
import matplotlib.pyplot as plt import numpy as np  np.random.seed(10)  d_1 = np.random.normal(100, 10, 200) d_2 = np.random.normal(90, 20, 200) d_3 = np.random.normal(80, 30, 200) d_4 = np.random.normal(70, 40, 200) d = [d_1, d_2, d_3, d_4]  fig = plt.figure(figsize =(10, 7)) ax = fig.add_axes([0, 0, 1, 1]) bp = ax.boxplot(d)  plt.show() 

Output: 

box-plot-python

Example 2: Let's try to modify the above plot with some of the customizations: 

We will customize the plot by adding a notch, filling the boxes with colors, and modifying the whisker and median styles.

Python
import matplotlib.pyplot as plt import numpy as np  np.random.seed(10) d_1 = np.random.normal(100, 10, 200) d_2 = np.random.normal(90, 20, 200) d_3 = np.random.normal(80, 30, 200) d_4 = np.random.normal(70, 40, 200) d = [d_1, d_2, d_3, d_4]  fig = plt.figure(figsize =(10, 7)) ax = fig.add_subplot(111)  bp = ax.boxplot(d, patch_artist = True,                 notch ='True', vert = 0)  colors = ['#0000FF', '#00FF00',            '#FFFF00', '#FF00FF']  for patch, color in zip(bp['boxes'], colors):     patch.set_facecolor(color)  for whisker in bp['whiskers']:     whisker.set(color ='#8B008B',                 linewidth = 1.5,                 linestyle =":")  # changing color and linewidth of for cap in bp['caps']:     cap.set(color ='#8B008B',             linewidth = 2)  for median in bp['medians']:     median.set(color ='red',                linewidth = 3)  # changing style of fliers for flier in bp['fliers']:     flier.set(marker ='D',               color ='#e7298a',               alpha = 0.5)  ax.set_yticklabels(['d_1', 'd_2',                      'd_3', 'd_4'])   plt.title("Customized box plot")  ax.get_xaxis().tick_bottom() ax.get_yaxis().tick_left()  plt.show() 

Output: 

customizes_box_plot
customizes box plot

A highly customized box plot with different colors for each dataset, enhanced whiskers, and a styled median.

Related Article:

  • Matplotlib Pyplot
  • Matplotlib.pyplot.plot() function in Python

Next Article
Box Plot in Python using Matplotlib

J

jeeteshgavande30
Improve
Article Tags :
  • Data Visualization
  • AI-ML-DS
  • Python-matplotlib

Similar Reads

    Simple Plot in Python using Matplotlib
    Creating simple plots is a common step in data visualization. These visual representations help us to understand trends, patterns and relationships within data. Matplotlib is one of the most popular plotting libraries in Python which makes it easy to generate high-quality graphs with just a few line
    4 min read
    Matplotlib.pyplot.sci() in Python
    Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. There are various plots which can be used in Pyplot are Line Plot, Contour, Histogram, Scatter, 3D Plot,
    2 min read
    Matplotlib.pyplot.sca() in Python
    Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. There are various plots which can be used in Pyplot are Line Plot, Contour, Histogram, Scatter, 3D Plot,
    1 min read
    Matplotlib.pyplot.axes() in Python
    axes() method in Matplotlib is used to create a new Axes instance (i.e., a plot area) within a figure. This allows you to specify the location and size of the plot within the figure, providing more control over subplot layout compared to plt.subplot(). It's key features include:Creates a new Axes at
    3 min read
    Matplotlib.pyplot.show() in Python
    Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. Sample Code - Python3 1== # sample code import matplotlib.pyplot as plt plt.plot([1, 2, 3, 4], [16, 4, 1,
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences