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:
How to Show Gridlines on Matplotlib Plots?
Next article icon

How to Show Gridlines on Matplotlib Plots?

Last Updated : 28 Nov, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will see how we can add gridlines to a matplotlib graph and various configurable parameters that can be used in Python.

Example:

 Create a matplotlib plot using sample data.

Python
# Importing the library import matplotlib.pyplot as plt  # Define X and Y data points X = [12, 34, 23, 45, 67, 89] Y = [1, 3, 67, 78, 7, 5]  # Plot the graph using matplotlib plt.plot(X, Y)  # Function to view the plot plt.show() 

Output

grid()

The grid() method is used to define a grid in a matplotlib graph. The syntax is given by -

Syntax:

matplotlib.pyplot.grid(b=None, which='major', axis='both', **kwargs)

Parameters:

  • b: bool or None, optional: Whether to show the grid lines. If any kwargs are supplied, it is assumed you want the grid on and b will be set to True. If b is None and there are no kwargs, this toggles the visibility of the lines.
  • which: {'major', 'minor', 'both'}, optional: The grid lines to apply the changes on.
  • axis: {'both', 'x', 'y'}, optional: The axis to apply the changes on.
  • **kwargs: Line2D properties: Define the line properties of the grid

Example 1: Default Gridlines

Python
# Importing the library import matplotlib.pyplot as plt  # Define X and Y data points X = [12, 34, 23, 45, 67, 89] Y = [1, 3, 67, 78, 7, 5]  # Plot the graph using matplotlib plt.plot(X, Y)  # Add gridlines to the plot plt.grid(b=True) # `plt.grid()` also works  # Function to view the plot plt.show() 

Output

Example 2: Apply Gridlines to  x- axis

In the cases, where we would want to see only the vertical or horizontal gridlines, we can make use of the `axis` parameter. To view only vertical gridlines -

Syntax:

plt.grid(b=True, axis='x')
Python3
# Importing the library import matplotlib.pyplot as plt  # Define X and Y data points X = [12, 34, 23, 45, 67, 89] Y = [1, 3, 67, 78, 7, 5]  # Plot the graph using matplotlib plt.plot(X, Y)  # Add gridlines to the plot plt.grid(b=True, axis='x') # `plt.grid()` also works  # Function to view the plot plt.show() 

Output:

Example 3: Apply Gridlines to y-axis

In the cases, where we would want to see only the vertical or horizontal gridlines, we can make use of the `axis` parameter. To view only horizontal gridlines -

Syntax:

 plt.grid(b=True, axis='y')
Python3
# Importing the library import matplotlib.pyplot as plt  # Define X and Y data points X = [12, 34, 23, 45, 67, 89] Y = [1, 3, 67, 78, 7, 5]  # Plot the graph using matplotlib plt.plot(X, Y)  # Add gridlines to the plot plt.grid(b=True, axis='y') # `plt.grid()` also works  # Function to view the plot plt.show() 

Output:

Example 4: Provide a linestyle and linewidth

We have seen the gridlines being a solid lines. We can change this to view a different line-style like dashes using an argument `linestyle` or `ls`. Similarly, we can change the thickness of the gridlines using the argument `linewidth`. These arguments are provided as part of the keyword arguments and can be viewed in the document link.

To provide a line style -

Syntax:

plt.grid(linestyle='--')
Python3
# Importing the library import matplotlib.pyplot as plt  # Define X and Y data points X = [12, 34, 23, 45, 67, 89] Y = [1, 3, 67, 78, 7, 5]  # Plot the graph using matplotlib plt.plot(X, Y)  # Add gridlines to the plot plt.grid(linestyle='--') # `plt.grid()` also works  # Function to view the plot plt.show() 

Output:

.

Example 5: Change color of the Gridlines

The keyword argument `color` can be used to define a different color to the plot.

Syntax:

plt.grid(linestyle, color)
Python3
# Importing the library import matplotlib.pyplot as plt  # Define X and Y data points X = [12, 34, 23, 45, 67, 89] Y = [1, 3, 67, 78, 7, 5]  # Plot the graph using matplotlib plt.plot(X, Y)  # Add gridlines to the plot plt.grid(linestyle='--', color='pink') # `plt.grid()` also works  # Function to view the plot plt.show() 

Output:

 


Next Article
How to Show Gridlines on Matplotlib Plots?

A

apathak092
Improve
Article Tags :
  • Python
  • Python-matplotlib
Practice Tags :
  • python

Similar Reads

    How to Generate Subplots With Python's Matplotlib
    Data visualization plays a pivotal role in the process of analyzing and interpreting data. The Matplotlib library in Python offers a robust toolkit for crafting diverse plots and charts. One standout feature is its capability to generate subplots within a single figure, providing a valuable tool for
    6 min read
    Matplotlib.pyplot.grid() in Python
    matplotlib.pyplot.grid() function in Matplotlib is used to toggle the visibility of grid lines in a plot. Grid lines help improve readability of a plot by adding reference lines at major and/or minor tick positions along the axes. Example:Pythonimport matplotlib.pyplot as plt fig, ax = plt.subplots(
    3 min read
    Line plot styles in Matplotlib
    Line plots are important data visualization elements that can be used to identify relationships within the data. Using matplotlib.pyplot.plot() function we can plot line plots. Styling tools in this helps us customize line plots according to our requirements which helps in better representations. Li
    3 min read
    How to Create Subplots in Matplotlib with Python?
    Matplotlib is a widely used data visualization library in Python that provides powerful tools for creating a variety of plots. One of the most useful features of Matplotlib is its ability to create multiple subplots within a single figure using the plt.subplots() method. This allows users to display
    6 min read
    Matplotlib.pyplot.hlines() in Python
    Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. Matplotlib.pyplot.hlines() The Matplotlib.pyplot.hlines() is used to draw horizontal lin
    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