Skip to content
geeksforgeeks
  • 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
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • 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:
3D Surface plotting in Python using Matplotlib
Next article icon

Change grid line thickness in 3D surface plot in Python - Matplotlib

Last Updated : 14 Sep, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

Prerequisites: Matplotlib

Using Matplotlib library we can plot the three-dimensional plot by importing the mplot3d toolkit. In this plot, we are going the change the thickness of the gridline in a three-dimensional surface plot. Surface Plot is the diagram of 3D data it shows the functional relationship between the dependent variable (Y), and two independent variables (X and Y) rather than showing the individual data points.

Gridlines are the lines that cross the plot to show the axis divisions. Gridlines of the plot help the viewer of the chart to see what value is represented by the particular unlabeled data point of the plot. It helps especially when the plot is too complicated to analyze, so according to the need, we can adjust or change the thickness of the grid lines or the styles of the grid lines.

Approach

  • Import the necessary libraries.
  • Create the 3D data for plotting.
  • Create the three-dimensional co-ordinate system using ax = matplotlib.pyplot.gca('projection=3d') here gca stands for get current grid and in that pass the parameter projection=3d it will generate three-dimensional co-ordinate system.
  • Now for changing the gridline thickness we have to update the axes info of respective axes by using ax.xaxis.update({'linewidth':3}) or whatever grid width you want to set this is for x-axis in the same way you can set for Y axis and Z axis just writing yaxis, zaxis in place of xaxis respectively.
  • If want to change the color of the respective axis just pass 'color':'red' in the dictionary ax.xaxis.update({'linewidth':3,'color':'red'}) or whatever color you want to set.
  • Now after making changes plot the surface plot using ax.plot_surface(x,y,z) in that pass 3d data.
  • Set X, Y and Z label using ax.set_xlabel(), ax.set_ylabel(), ax.set_zlabel() in parameter pass the string.
  • Now visualize the plot using matplotlib.pyplot.show() function.

Example 1: Changing X-axis grid lines thickness in the 3D surface plot using Matplotlib.

Python
# importing necessary libraries import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D import numpy as np  # function to create data for plotting def data_creation():        # creating 3d data     x = np.outer(np.linspace(-3, 3, 30), np.ones(30))     y = x.copy().T # transpose     z = np.cos(x ** 2 + y ** 2)     return (x,y,z)  # main function if __name__ == '__main__':        # creating three dimensional co-ordinate system     ax = plt.gca(projection='3d')      # calling data creation function and storing in      # the variables     data_x,data_y,data_z = data_creation()      # changing grid lines thickness of x axis to 3     ax.xaxis._axinfo["grid"].update({"linewidth":3})      # plotting surface plot     ax.plot_surface(data_x,data_y,data_z)      # giving label name to x,y and z axis     ax.set_xlabel("X axis")     ax.set_ylabel("Y axis")     ax.set_zlabel("Z axis")          # visualizing the plot     plt.show() 

  

Output:


 


 

In the above example, we had changed the gridline thickness of X-axis, as you can see in the above figure X-axis gridlines has a thicker line with gray color. It can be done by updating the _axinfo dictionary of the respective axis in the above example code our respective axis is X axis.


 

Example 2: Changing X-axis grid lines thickness in the 3D surface plot using Matplotlib.


 

Python
# importing necessary libraries import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D import numpy as np  # function to create data for plotting def data_creation():        # creating 3d data     x = np.outer(np.linspace(-3, 3, 30), np.ones(30))     y = x.copy().T # transpose     z = np.cos(x ** 2 + y ** 2)     return (x,y,z)  # main function if __name__ == '__main__':     # creating three dimensional co-ordinate system     ax = plt.gca(projection='3d')      # calling data creation function and storing in the variables     data_x,data_y,data_z = data_creation()      # changing grid lines thickness of Y axis to 3     ax.yaxis._axinfo["grid"].update({"linewidth":3})      # plotting surface plot     ax.plot_surface(data_x,data_y,data_z)      # giving label name to x,y and z axis     ax.set_xlabel("X axis")     ax.set_ylabel("Y axis")     ax.set_zlabel("Z axis")          # visualizing the plot     plt.show() 

  

Output:


 


 

In the above example, we had changed the gridline thickness of Y axis, as you can see in the above figure Y-axis gridlines has a thicker line with gray color. We had set the gridline thickness of Y-axis to 3.


 

Example 3: Changing Z axis grid lines thickness in 3D surface plot using Matplotlib.


 

Python
# importing necessary libraries import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D import numpy as np  # function to create data for plotting def data_creation():     # creating 3d data     x = np.outer(np.linspace(-3, 3, 30), np.ones(30))     y = x.copy().T # transpose     z = np.cos(x ** 2 + y ** 2)     return (x,y,z)  # main function if __name__ == '__main__':     # creating three dimensional co-ordinate system     ax = plt.gca(projection='3d')      # calling data creation function and storing in the variables     data_x,data_y,data_z = data_creation()      # changing grid lines thickness of Z axis to 3     ax.zaxis._axinfo["grid"].update({"linewidth":3})      # plotting surface plot     ax.plot_surface(data_x,data_y,data_z)      # giving label name to x,y and z axis     ax.set_xlabel("X axis")     ax.set_ylabel("Y axis")     ax.set_zlabel("Z axis")          # visualizing the plot     plt.show() 

  

Output:


 


 

In the above example, we had changed the gridline thickness of the Z-axis, as you can see in the above figure Z-axis gridlines have a thicker line with gray color. We had set the gridline thickness of the Z-axis to 3.


 

Example 4: Changing gridline thickness and color of all three axis using Matplotlib.


 

Python
# importing necessary libraries import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D import numpy as np  # function to create data for plotting def data_creation():        # creating 3d data     x = np.outer(np.linspace(-3, 3, 30), np.ones(30))     y = x.copy().T # transpose     z = np.cos(x ** 2 + y ** 2)     return (x,y,z)  # main function if __name__ == '__main__':     # creating three dimensional co-ordinate system     ax = plt.gca(projection='3d')      # calling data creation function and storing in the variables     data_x,data_y,data_z = data_creation()      # changing grid lines thickness of x axis to 1     ax.xaxis._axinfo["grid"].update({"linewidth":1})      # changing grid lines thickness of Y axis to 1 and giving color to red     ax.yaxis._axinfo["grid"].update({"linewidth":1,'color':'red'})      # changing grid lines thickness of Z axis to 1 and giving color to green     ax.zaxis._axinfo["grid"].update({"linewidth":1,'color':'green'})      # plotting surface plot     ax.plot_surface(data_x,data_y,data_z)      # giving label name to x,y and z axis     ax.set_xlabel("X axis")     ax.set_ylabel("Y axis")     ax.set_zlabel("Z axis")          # visualizing the plot     plt.show() 

  

Output:


 


 

In the above example, we had set the gridline thickness of X, Y, and Z axis to 1 and change the color of the Y-axis to red and Z-axis to green, by updating the _axinfo and updating the dictionary we had set line width and color of the 3D plot.


 


Next Article
3D Surface plotting in Python using Matplotlib
author
srishivansh5404
Improve
Article Tags :
  • Python
  • Python-matplotlib
Practice Tags :
  • python

Similar Reads

  • How to Change the Line Width of a Graph Plot in Matplotlib with Python?
    Prerequisite : Matplotlib  In this article we will learn how to Change the Line Width of a Graph Plot in Matplotlib with Python. For that one must be familiar with the given concepts: Matplotlib : Matplotlib is a tremendous visualization library in Python for 2D plots of arrays. Matplotlib may be a
    2 min read
  • 3D Surface plotting in Python using Matplotlib
    A Surface Plot is a representation of three-dimensional dataset. It describes a functional relationship between two independent variables X and Z and a designated dependent variable Y, rather than showing the individual data points. It is a companion plot of the contour plot. It is similar to the wi
    4 min read
  • Change plot size in Matplotlib - Python
    Plots are an effective way of visually representing data and summarizing it beautifully. However, if not plotted efficiently it seems appears complicated. Python's Matplotlib provides several libraries for data representation. While making a plot we need to optimize its size. In this article, we wil
    3 min read
  • Tri-Surface Plot in Python using Matplotlib
    A Tri-Surface Plot is a type of surface plot, created by triangulation of compact surfaces of finite number of triangles which cover the whole surface in a manner that each and every point on the surface is in triangle. The intersection of any two triangles results in void or a common edge or vertex
    2 min read
  • Change the error bar thickness in Matplotlib
    Matplotlib is a Python library which is widely used by data analytics. Matplotlib.pyplot.errorbar()  is a pyplot module consisting of a function which provides a MATLAB like interface. Changing Error Bar Thickness Before changing the thickness of error bar let us see what error bar is and how we can
    2 min read
  • Change the line opacity in Matplotlib
    Changing Line Opacity in Matplotlib means adjusting the transparency level of a line in a plot. Opacity controls how much the background or overlapping elements are visible through the line. A fully opaque line is solid, while a more transparent line allows other elements behind it to be seen. Let's
    2 min read
  • Matplotlib.axis.Tick.set_clip_path() function in Python
    Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. It is an amazing visualization library in Python for 2D plots of arrays and used for working with the broader SciPy stack. Matplotlib.axis.Tick.set_clip_path() Function The Tick.set_clip_path() function
    2 min read
  • 3D Surface Plots using Plotly in Python
    Plotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization library
    2 min read
  • 3D Contour Plotting in Python using Matplotlib
    Matplotlib was introduced keeping in mind, only two-dimensional plotting. But at the time when the release of 1.0 occurred, the 3d utilities were developed upon the 2d and thus, we have 3d implementation of data available today! The 3d plots are enabled by importing the mplot3d toolkit. Let's look a
    2 min read
  • How to Change the Transparency of a Graph Plot in Matplotlib with 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 that can be used in Pyplot are Line Plot, Contour, Histogram, Scatter, 3D Plot, e
    3 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