Draw a 3D scatterplot in Python using Matplotlib

What is Matplotlib?
Matplotlib is a library in Python for creating static and dynamic animations and plotting using its built-in functions. It has many built-in features and built-in analysis tools for analyzing any graph or chart.
If we want to plot any 3D graph then we can use Matplotlib library. When we have a huge dataset of 3D variables and we graph it, it looks very scattered, this is called a 3D scatterplot. We will use Matplotlib's matplot3d toolkit to draw 3D graphics.
There is an axe. function that accepts a dataset of coordinates X, Y, and Z.
Depending on the properties we want to ascribe to 3D graphs, more arguments are needed.

When Matplotlib was first created, only two-dimensional plotting was considered. Around the time of the 1.0 release, a practical (albeit rather limited) 3D data visualization toolset was created by layering some 3D charting tools on top of Matplotlib's 2D display. Three-dimensional charts are made possible by importing the mplot3d toolkit (which is part of the base Matplotlib installation).
The simplest 3D plot is a scatterplot consisting of lines or clusters of (x,y,z) triples. These can be produced with axes. plot3D and ax. The scatter3D function, much like the more typical two-dimensional charts presented earlier. Their calling characteristics are very similar to their two-dimensional counterparts.
To create the illusion of depth on the page, the transparency of the scatter points has been changed.
Example 1:

# importing the necessary libraries  
import numpy as np  
import matplotlib.pyplot as plt  
from mpl_toolkits import mplot3d  
  
# generating  random dataset  
z = np.random.randint(80, size =(55))  
x = np.random.randint(60, size =(55))  
y = np.random.randint(64, size =(55))  
  
# Creating figures for the plot  
fig = plt.figure(figsize = (10, 7))  
ax = plt.axes(projection ="3d")  
  
# Creating a plot using the random datasets   
ax.scatter3D(x, y, z, color = "red")  
plt.title("3D scatter plot")  
  
# display the  plot  
plt.show()  

output:

Explanation:
In the above example, we created a 3D plot using ax. scatter() function. We have initially imported all the required libraries such as numpy, matplotlib and mpl_toolkits. We then created a dataset of x, y, and z coordinates of random numbers using the randInt() function. After that, we used the axe. scatter3D() function, and input x, y and z coordinates, we take red color for points. Finally, we display the plot using the show() function. 

Example 2:

# importing the necessary libraries  
from mpl_toolkits import mplot3d  
import matplotlib.pyplot as plt  
import numpy as np  
  
  
# Creating random 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, 12))  
ax = plt.axes(projection ="3d")  
  
# Add x, and y gridlines for the figure  
ax.grid(b = True, color ='blue',linestyle ='-.', linewidth = 0.5,alpha = 0.3)  
  
  
# Creating the color map for the plot  
my_cmap = plt.get_cmap('hsv')  
  
# Creating the 3D plot  
sctt = ax.scatter3D(x, y, z,alpha = 0.8,c = (x + y + z),cmap = my_cmap,marker ='^')  
  
plt.title("3D scatter plot in Python")  
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.6, aspect = 5)  
  
# display the plot  
plt.show()  

output:

explain:

In the above code, we used the function ax to draw a three-dimensional graph. scatter3D() function. We generated random datasets of x, y and z coordinates and plotted them using the marker "^". We provide labels for the individual axes using the set_label function. 

 

Guess you like

Origin blog.csdn.net/std7879/article/details/127804598