matplotlib 사용 방법

1. 우선 기본형


드로잉 데이터의 x, y를 목록에 표시합니다.
fig = plt.figure()
ax = fig.add_subplot()

x = [1, 2, 3, 4]
y = [1, 4, 9, 16]

ax.plot(x, y)


2. x축을 시간으로 설정


네!!:tada:

3. 데이터를 그려본다.


3.1 Sample의 데이터 프레임을 만든다.
4
from datetime import datetime, timedelta
x_t =  [datetime(2021, 9, 1) + timedelta(hours=i) for i in range(4)]
'''
x_t
[datetime.datetime(2021, 9, 1, 0, 0),
datetime.datetime(2021, 9, 1, 1, 0),
datetime.datetime(2021, 9, 1, 2, 0),
datetime.datetime(2021, 9, 1, 3, 0)]
'''

fig = plt.figure()
ax = fig.add_subplot()
ax.plot(x_t, y)
ax.scatter(x_t, y) # 点もプロットしてみる

2.2 목록은 열을 목록으로 변환하면 이미지를 그릴 수 있습니다.
이런 느낌.
4
import pandas as pd
df = pd.DataFrame(
    {
    'x': x,
    'y': y,
    'x_t': x_t
    }
)
print(df)
'''
   x   y                 x_t
0  1   1 2021-09-01 00:00:00
1  2   4 2021-09-01 01:00:00
2  3   9 2021-09-01 02:00:00
3  4  16 2021-09-01 03:00:00
'''
3 이후 동일하게 그리기
x_df = df['x'].to_list()
y_df = df['y'].to_list()

도중에 약간 걸려 넘어졌다.
리스트에 쓸 때 이쪽이 틀렸어!!
4
fig = plt.figure()
ax = fig.add_subplot()

ax.plot(x_df, y_df)
※ 추출열은 ['x'일 경우 시리즈, ['x'일 경우 데이터 프레임입니다.이번에는 Series to.list().
matplotlib과 데이터 프레임에 익숙하지 않기 때문에 이것만으로도 상당히 깊다.

좋은 웹페이지 즐겨찾기