Change grid line thickness in 3D surface plot in Python – Matplotlib
Last Updated :
14 Sep, 2021
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
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
def data_creation():
x = np.outer(np.linspace( - 3 , 3 , 30 ), np.ones( 30 ))
y = x.copy().T
z = np.cos(x * * 2 + y * * 2 )
return (x,y,z)
if __name__ = = '__main__' :
ax = plt.gca(projection = '3d' )
data_x,data_y,data_z = data_creation()
ax.xaxis._axinfo[ "grid" ].update({ "linewidth" : 3 })
ax.plot_surface(data_x,data_y,data_z)
ax.set_xlabel( "X axis" )
ax.set_ylabel( "Y axis" )
ax.set_zlabel( "Z axis" )
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
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
def data_creation():
x = np.outer(np.linspace( - 3 , 3 , 30 ), np.ones( 30 ))
y = x.copy().T
z = np.cos(x * * 2 + y * * 2 )
return (x,y,z)
if __name__ = = '__main__' :
ax = plt.gca(projection = '3d' )
data_x,data_y,data_z = data_creation()
ax.yaxis._axinfo[ "grid" ].update({ "linewidth" : 3 })
ax.plot_surface(data_x,data_y,data_z)
ax.set_xlabel( "X axis" )
ax.set_ylabel( "Y axis" )
ax.set_zlabel( "Z axis" )
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
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
def data_creation():
x = np.outer(np.linspace( - 3 , 3 , 30 ), np.ones( 30 ))
y = x.copy().T
z = np.cos(x * * 2 + y * * 2 )
return (x,y,z)
if __name__ = = '__main__' :
ax = plt.gca(projection = '3d' )
data_x,data_y,data_z = data_creation()
ax.zaxis._axinfo[ "grid" ].update({ "linewidth" : 3 })
ax.plot_surface(data_x,data_y,data_z)
ax.set_xlabel( "X axis" )
ax.set_ylabel( "Y axis" )
ax.set_zlabel( "Z axis" )
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
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
def data_creation():
x = np.outer(np.linspace( - 3 , 3 , 30 ), np.ones( 30 ))
y = x.copy().T
z = np.cos(x * * 2 + y * * 2 )
return (x,y,z)
if __name__ = = '__main__' :
ax = plt.gca(projection = '3d' )
data_x,data_y,data_z = data_creation()
ax.xaxis._axinfo[ "grid" ].update({ "linewidth" : 1 })
ax.yaxis._axinfo[ "grid" ].update({ "linewidth" : 1 , 'color' : 'red' })
ax.zaxis._axinfo[ "grid" ].update({ "linewidth" : 1 , 'color' : 'green' })
ax.plot_surface(data_x,data_y,data_z)
ax.set_xlabel( "X axis" )
ax.set_ylabel( "Y axis" )
ax.set_zlabel( "Z axis" )
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.
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 m
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
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
How to Change the Color of a Graph Plot in Matplotlib with Python?
Prerequisite: Matplotlib Python offers a wide range of libraries for plotting graphs and Matplotlib is one of them. Matplotlib is simple and easy to use a library that is used to create quality graphs. The pyplot library of matplotlib comprises commands and methods that makes matplotlib work like ma
2 min read