In this article, we will explore the creation of a simple 3D scatter plot using the popular Python library, Matplotlib. The data used in this example is from a trainset.csv file, which contains information on takeoff speed, weight, and distance.

Prerequisites

Before we begin, make sure you have installed the necessary libraries:

  • Matplotlib
  • NumPy

If you don't have these libraries installed, you can do so by running the following commands:

pip install matplotlib numpy

Code Example

Here is the code used to create the 3D scatter plot:

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.gca(projection='3d')

# Load data from trainset.csv file
data = np.loadtxt('./trainset.csv', unpack=True, delimiter=',', skiprows=1)
X = data[0]
Y = data[1]
Z = data[2]

# Plot the scatter plot
ax.scatter(X, Y, Z)

# Customize the axes
ax.set_xlabel('Speed (km/h)')
ax.set_ylabel('Weight (ton)')
ax.set_zlabel('Distance (m)')

# Add title and show plot
plt.suptitle('Takeoff distance prediction', fontsize=16)
plt.show()

Explanation

In this code, we first import the necessary libraries: Matplotlib (matplotlib.pyplot) and NumPy (numpy). We then create a figure object using fig = plt.figure(), which is used to store the plot.

Next, we create an axes object using ax = fig.gca(projection='3d'), which specifies that we want to create a 3D plot. The projection='3d' parameter tells Matplotlib to create a 3D projection.

We then load the data from the trainset.csv file using np.loadtxt(). The data is stored in three arrays: X, Y, and Z, which represent the takeoff speed, weight, and distance, respectively.

Next, we plot the scatter plot using ax.scatter(X, Y, Z), which creates a 3D scatter plot with the data points.

Finally, we customize the axes labels using ax.set_xlabel(), ax.set_ylabel(), and ax.set_zlabel(), and add a title to the plot using plt.suptitle().


In this article, we have created a simple 3D scatter plot using Matplotlib. This plot is used to visualize the relationship between takeoff speed, weight, and distance for predicting takeoff distances. In the next posting, we will explore how to use a neural network (Multi-Layer Perceptron) to predict takeoff distances.

[Data Visualization] Matplotlib로 3D scatter plot 그리기

이전 포스팅에서는 데이터 시각화의 기본을 다루었습니다. 이제는 3D scatter plot을 그려보겠습니다. 간단한 3D scatter plot을 그려보겠습니다.

trainset.csv에서 데이터를 로드하여 비행기 이륙속도, 짐의 무게를 가지고 이륙에 필요한 거리를 계산하는 예제의 시각화 부분입니다.

이 코드에서는 Matplotlib와 NumPy 라이브러리를 사용합니다. Matplotlib는 2D 및 3D 그래프를 그리는 데 사용되는 Python 라이브러리입니다.

코드:

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.gca(projection='3d')

# 데이터 로드
data = np.loadtxt('./trainset.csv', unpack=True, delimiter=',', skiprows=1)
X = data[0]
Y = data[1]
Z = data[2]

# 3D scatter plot 그리기
ax.scatter(X, Y, Z)

# 축 레이블 설정
ax.set_xlabel('Speed(km/h)')
ax.set_ylabel('Weight(ton)')
ax.set_zlabel('Distance(m)')

# 제목 추가 및 표시
plt.suptitle('Takeoff distance prediction', fontsize=16)
plt.show()

이 코드에서는 3D scatter plot을 그리는 데 사용되는 Matplotlib 라이브러리를 사용합니다. 데이터는 우측 상단 링크에서 받으실 수 있습니다.

다음 포스팅에서는 신경망(Multi-Layer Perceptron)을 이용하여 비행기 이륙거리 예측을 해보겠습니다.