Data Visualization: Drawing 3D Scatter Plots using Matplotlib
In this article, we will create a simple 3D scatter plot using Matplotlib to visualize the takeoff distance prediction data. This example is based on the scenario where we need to calculate the takeoff distance required for an airplane given its speed and weight.
Importing Modules
To draw a 3D scatter plot, we need to import some modules from matplotlib. Specifically, we need to import pyplot as plt, which provides functions for creating plots, and numpy as np, which provides functions for numerical operations.
import matplotlib.pyplot as plt
import numpy as np
Loading Data
Next, we load the data from a CSV file called trainset.csv. This file contains three columns: speed (in km/h), weight (in tons), and takeoff distance (in meters).
data = np.loadtxt('./trainset.csv', unpack=True, delimiter=',', skiprows=1)
X = data[0]
Y = data[1]
Z = data[2]
Creating the Plot
To create a 3D scatter plot, we need to specify the projection type as '3d'. We then use the scatter function to plot the points in 3D space.
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.scatter(X, Y, Z)
Customizing the Plot
We can customize the plot by setting labels for the x, y, and z axes using the set_xlabel, set_ylabel, and set_zlabel functions.
ax.set_xlabel('Speed (km/h)')
ax.set_ylabel('Weight (ton)')
ax.set_zlabel('Takeoff Distance (m)')
Adding a Title
Finally, we add a title to the plot using the suptitle function.
plt.suptitle('Takeoff Distance Prediction', fontsize=16)
Displaying the Plot
To display the plot, we use the show function.
plt.show()
And that's it! We have created a simple 3D scatter plot to visualize the takeoff distance prediction data.
In the next posting, we will explore how to use a Multi-Layer Perceptron (MLP) to predict the takeoff distance based on the speed and weight of an airplane.