# 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

## Matplotlib.pyplot.xlim() in PythonMatplotlib.pyplot.xlim() in Python

Matplotlib.pyplot.xlim() in Python There are various plots which can be used in Pyplot are Line

## Python – Get key from value in DictionaryPython – Get key from value in Dictionary

Python | Get key from value in Dictionary Let’s see how to get the key

## Python – Display images with PyGamePython – Display images with PyGame

Python | Display images with PyGame Pygame is a cross-platform set of Python modules designed for

## Oracle Database Connection in PythonOracle Database Connection in Python

Oracle Database Connection in Python Sometimes as part of programming, we required to work with

## Setup OpenCV With PyCharm EnvironmentSetup OpenCV With PyCharm Environment

Setup OpenCV With PyCharm Environment Introduction: If you love working on image processing and video

## How to read multiple text files from folder in Python?How to read multiple text files from folder in Python?

How to read multiple text files from folder in Python? Python is a strong language

## Matplotlib.pyplot.savefig() in PythonMatplotlib.pyplot.savefig() in Python

Matplotlib.pyplot.savefig() in Python Matplotlib is highly useful visualization library in Python. It is a multi-platform data

## numpy.sign() in Pythonnumpy.sign() in Python

numpy.sign() in Python numpy.sign(array [, out]) function is used to indicate the sign of a number