 # Three-dimensional Plotting in Python using Matplotlib

## Three-dimensional 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. In this article, we will deal with the 3d plots using matplotlib.

Example:

 `import` `numpy as np` `import` `matplotlib.pyplot as plt` `fig ``=` `plt.figure()` `ax ``=` `plt.axes(projection ``=``'3d'``)`

Output: With the above syntax three -dimensional axes are enabled and data can be plotted in 3 dimensions. 3 dimension graph gives a dynamic approach and makes data more interactive. Like 2-D graphs, we can use different ways to represent 3-D graph. We can make a scatter plot, contour plot, surface plot, etc. Let’s have a look at different 3-D plots.

## Plotting 3-D Lines and Points

Graph with lines and point are the simplest 3 dimensional graph. ax.plot3d and ax.scatter are the function to plot line and point graph respectively.
Example 1: 3 dimensional line graph

 `# importing mplot3d toolkits, numpy and matplotlib` `from` `mpl_toolkits ``import` `mplot3d` `import` `numpy as np` `import` `matplotlib.pyplot as plt` `fig ``=` `plt.figure()` `# syntax for 3-D projection` `ax ``=` `plt.axes(projection ``=``'3d'``)` `# defining all 3 axes` `z ``=` `np.linspace(``0``, ``1``, ``100``)` `x ``=` `z ``*` `np.sin(``25` `*` `z)` `y ``=` `z ``*` `np.cos(``25` `*` `z)` `# plotting` `ax.plot3D(x, y, z, ``'green'``)` `ax.set_title(``'3D line plot geeks for geeks'``)` `plt.show()`

Output: Example 2: 3 dimensional scattered graph

 `# importing mplot3d toolkits` `from` `mpl_toolkits ``import` `mplot3d` `import` `numpy as np` `import` `matplotlib.pyplot as plt` `fig ``=` `plt.figure()` `# syntax for 3-D projection` `ax ``=` `plt.axes(projection ``=``'3d'``)` `# defining axes` `z ``=` `np.linspace(``0``, ``1``, ``100``)` `x ``=` `z ``*` `np.sin(``25` `*` `z)` `y ``=` `z ``*` `np.cos(``25` `*` `z)` `c ``=` `x ``+` `y` `ax.scatter(x, y, z, c ``=` `c)` `# syntax for plotting` `ax.set_title(``'3d Scatter plot geeks for geeks'``)` `plt.show()`

Output: ## Plotting Surface graphs and Wireframes

Surface graph and Wireframes graph work on gridded data. They take grid value and plot it on three-dimensional surface.
Example 1: Surface graph

 `# importing libraries` `from` `mpl_toolkits ``import` `mplot3d` `import` `numpy as np` `import` `matplotlib.pyplot as plt` `# defining surface and axes` `x ``=` `np.outer(np.linspace(``-``2``, ``2``, ``10``), np.ones(``10``))` `y ``=` `x.copy().T` `z ``=` `np.cos(x ``*``*` `2` `+` `y ``*``*` `3``)` `fig ``=` `plt.figure()` `# syntax for 3-D plotting` `ax ``=` `plt.axes(projection ``=``'3d'``)` `# syntax for plotting` `ax.plot_surface(x, y, z, cmap ``=``'viridis'``, edgecolor ``=``'green'``)` `ax.set_title(``'Surface plot geeks for geeks'``)` `plt.show()`

Output: Example 2: Wireframes

 `from` `mpl_toolkits ``import` `mplot3d` `import` `numpy as np` `import` `matplotlib.pyplot as plt` `# function for z axea` `def` `f(x, y):` `    ``return` `np.sin(np.sqrt(x ``*``*` `2` `+` `y ``*``*` `2``))` `# x and y axis` `x ``=` `np.linspace(``-``1``, ``5``, ``10``)` `y ``=` `np.linspace(``-``1``, ``5``, ``10``)` ` ` `X, Y ``=` `np.meshgrid(x, y)` `Z ``=` `f(X, Y)` `fig ``=` `plt.figure()` `ax ``=` `plt.axes(projection ``=``'3d'``)` `ax.plot_wireframe(X, Y, Z, color ``=``'green'``)` `ax.set_title(``'wireframe geeks for geeks'``);`

Output: ## Plotting Contour Graphs

Contour graph takes all the input data in two-dimensional regular grids, and the Z data is evaluated at every point.We use ax.contour3D function to plot a contour graph.
Example:

 `from` `mpl_toolkits ``import` `mplot3d` `import` `numpy as np` `import` `matplotlib.pyplot as plt` `# function for z axis` `def` `f(x, y):` `    ``return` `np.sin(np.sqrt(x ``*``*` `2` `+` `y ``*``*` `3``))` `# x and y axis` `x ``=` `np.linspace(``-``1``, ``5``, ``10``)` `y ``=` `np.linspace(``-``1``, ``5``, ``10``)` `X, Y ``=` `np.meshgrid(x, y)` `Z ``=` `f(X, Y)` `fig ``=` `plt.figure()` `ax ``=` `plt.axes(projection ``=``'3d'``)` `# ax.contour3D is used plot a contour graph` `ax.contour3D(X, Y, Z)`

Output: ## Plotting Surface Triangulations

The above graph is sometimes overly restricted and inconvenient. So by this method, we use a set of random draws. The function ax.plot_trisurf is used to draw this graph. It is not that clear but more flexible.
Example:

 `from` `mpl_toolkits ``import` `mplot3d` `import` `numpy as np` `import` `matplotlib.pyplot as plt` `# angle and radius` `theta ``=` `2` `*` `np.pi ``*` `np.random.random(``100``)` `r ``=` `6` `*` `np.random.random(``100``)` `# all three axes` `x ``=` `np.ravel(r ``*` `np.sin(theta))` `y ``=` `np.ravel(r ``*` `np.cos(theta))` `z ``=` `f(x, y)` `ax ``=` `plt.axes(projection ``=``'3d'``)` `ax.scatter(x, y, z, c ``=` `z, cmap ``=``'viridis'``, linewidth ``=` `0.25``);` `ax ``=` `plt.axes(projection ``=``'3d'``)` `ax.plot_trisurf(x, y, z, cmap ``=``'viridis'``, edgecolor ``=``'green'``);`

Output: ## Plotting Möbius strip

Möbius strip also called the twisted cylinder, is a one-sided surface without boundaries. To create the Möbius strip think about its parameterization, it’s a two-dimensional strip, and we need two intrinsic dimensions. Its angle range from 0 to 2 pie around the loop and width ranges from -1 to 1.
Example:

 `from` `mpl_toolkits ``import` `mplot3d` `import` `numpy as np` `import` `matplotlib.pyplot as plt` `from` `matplotlib.tri ``import` `Triangulation` `theta ``=` `np.linspace(``0``, ``2` `*` `np.pi, ``10``)` `w ``=` `np.linspace(``-``1``, ``5``, ``8``)` `w, theta ``=` `np.meshgrid(w, theta)` `phi ``=` `0.5` `*` `theta` `# radius in x-y plane` `r ``=` `1` `+` `w ``*` `np.cos(phi)` `# all three axes` `x ``=` `np.ravel(r ``*` `np.cos(theta))` `y ``=` `np.ravel(r ``*` `np.sin(theta))` `z ``=` `np.ravel(w ``*` `np.sin(phi))` `# triangulate in the underlying` `# parameterization` `tri ``=` `Triangulation(np.ravel(w), np.ravel(theta))` ` ` `ax ``=` `plt.axes(projection ``=``'3d'``)` `ax.plot_trisurf(x, y, z, triangles ``=` `tri.triangles,` `                ``cmap ``=``'viridis'``, linewidths ``=` `0.2``);`

Output: Last Updated on March 1, 2022 by admin

## Normal Equation in Linear RegressionNormal Equation in Linear Regression

ML | Normal Equation in Linear Regression Normal Equation is an analytical approach to Linear Regression with a Least Square Cost Function. We can directly find out the value of θ without using Gradient Descent. Following this approach is an effective and

## Python Program to convert List of Integer to List of StringPython Program to convert List of Integer to List of String

Python Program to convert List of Integer to List of String Given a List of Integers. The task is to convert them to List of Strings. Examples:   Input: [1, 12, 15, 21, 131] Output: ['1', '12', '15', '21', '131']

## Python program to find the gcd of two numbersPython program to find the gcd of two numbers

Python program to find the gcd of two numbers Given two numbers. The task is to find the GCD of the two numbers. Using STL :   In Python, the math module contains a number of mathematical operations, which can

## Find the k most frequent words from data set in PythonFind the k most frequent words from data set in Python

Find the k most frequent words from data set in Python Given the data set, we can find k number of most frequent words. In order to do this, we’ll use a high performance data type module, which is collections. This

## Python | Linear Regression using sklearnPython | Linear Regression using sklearn

Python | Linear Regression using sklearn Linear Regression is a machine learning algorithm based on supervised learning. It performs a regression task. Regression models a target prediction value based on independent variables. It is mostly used for finding out the

## randrange() in Pythonrandrange() in Python

randrange() in Python Generating a random number has always been an important application and having many uses in daily life. Python offers a function that can generate random numbers from a specified range and also allowing rooms for steps to

## How to Plot Mean and Standard Deviation in Pandas?How to Plot Mean and Standard Deviation in Pandas?

How to Plot Mean and Standard Deviation in Pandas? Errorbar is the plotted chart that refers to the errors contained in the data frame, which shows the confidence & precision in a set of measurements or calculated values. Error bars help

## Python – Get list of running processesPython – Get list of running processes

Python – Get list of running processes A Process is a program that is being executed (processed). A process may not have to be one run explicitly by the user, it could be a system process spawned by the operating system. Any