# 3D Scatter Plotting in Python using Matplotlib

## 3D Scatter Plotting in Python using Matplotlib

3D Scatter Plot is a mathematical diagram, the most basic version of three-dimensional plotting used to display the properties of data as three variables of a dataset using the cartesian coordinates.To create a 3D Scatter plot, Matplotlib’s mplot3d toolkit is used to enable three dimensional plotting.Generally 3D scatter plot is created by using ax.scatter3D() the function of the matplotlib library which accepts a data sets of X, Y and Z to create the plot while the rest of the attributes of the function are the same as that of two dimensional scatter plot.

Example 1: Let’s create a basic 3D scatter plot using the ax.scatter3D() function.

 `# Import libraries` `from` `mpl_toolkits ``import` `mplot3d` `import` `numpy as np` `import` `matplotlib.pyplot as plt` `# Creating dataset` `z ``=` `np.random.randint(``100``, size ``=``(``50``))` `x ``=` `np.random.randint(``80``, size ``=``(``50``))` `y ``=` `np.random.randint(``60``, size ``=``(``50``))` `# Creating figure` `fig ``=` `plt.figure(figsize ``=` `(``10``, ``7``))` `ax ``=` `plt.axes(projection ``=``"3d"``)` `# Creating plot` `ax.scatter3D(x, y, z, color ``=` `"green"``)` `plt.title(``"simple 3D scatter plot"``)` `# show plot` `plt.show()`

Output :

Example 2 : For better understanding Let’s take another example.

 `# Import libraries` `from` `mpl_toolkits ``import` `mplot3d` `import` `numpy as np` `import` `matplotlib.pyplot as plt` `# Creating dataset` `z ``=` `4` `*` `np.tan(np.random.randint(``10``, size ``=``(``500``))) ``+` `np.random.randint(``100``, size ``=``(``500``))` `x ``=` `4` `*` `np.cos(z) ``+` `np.random.normal(size ``=` `500``)` `y ``=` `4` `*` `np.sin(z) ``+` `4` `*` `np.random.normal(size ``=` `500``)` `# Creating figure` `fig ``=` `plt.figure(figsize ``=` `(``16``, ``9``))` `ax ``=` `plt.axes(projection ``=``"3d"``)` `  ` `# Add x, y gridlines` `ax.grid(b ``=` `True``, color ``=``'grey'``,` `        ``linestyle ``=``'-.'``, linewidth ``=` `0.3``,` `        ``alpha ``=` `0.2``)` `# Creating color map` `my_cmap ``=` `plt.get_cmap(``'hsv'``)` `# Creating plot` `sctt ``=` `ax.scatter3D(x, y, z,` `                    ``alpha ``=` `0.8``,` `                    ``c ``=` `(x ``+` `y ``+` `z),` `                    ``cmap ``=` `my_cmap,` `                    ``marker ``=``'^'``)` `plt.title(``"simple 3D scatter plot"``)` `ax.set_xlabel(``'X-axis'``, fontweight ``=``'bold'``)` `ax.set_ylabel(``'Y-axis'``, fontweight ``=``'bold'``)` `ax.set_zlabel(``'Z-axis'``, fontweight ``=``'bold'``)` `fig.colorbar(sctt, ax ``=` `ax, shrink ``=` `0.5``, aspect ``=` `5``)` `# show plot` `plt.show()`

Output :

Last Updated on November 13, 2021 by admin

## Writing to an excel sheet using PythonWriting to an excel sheet using Python

Writing to an excel sheet using Python Using xlwt module, one can perform multiple operations on spreadsheet.

## Mouse and keyboard automation using PythonMouse and keyboard automation using Python

Mouse and keyboard automation using Python This article illustrates how to automate movements of mouse

## Pandas Extracting rows using .loc[]Pandas Extracting rows using .loc[]

Pandas is a popular data manipulation library in Python. It provides many useful methods to

## Difference between loc() and iloc() in Pandas DataFrameDifference between loc() and iloc() in Pandas DataFrame

Difference between loc() and iloc() in Pandas DataFrame   Pandas library of python is very

## Python sep parameter in print()Python sep parameter in print()

Python | sep parameter in print() The separator between the arguments to print() function in

## Types of inheritance PythonTypes of inheritance Python

Types of inheritance Python Inheritance is defined as the capability of one class to derive

## os.walk() in Pythonos.walk() in Python

os.walk() in Python How to traverse file system in Python ? Suppose we have given

## Multi-dimensional lists in PythonMulti-dimensional lists in Python

Multi-dimensional lists in Python There can be more than one additional dimension to lists in Python.