database

fitting with legend

작성자
cfdkim
작성일
2025-08-17 07:48
조회
122
import matplotlib.pyplot as plt
import numpy as np

xx = [0, 5, 10, 15, 20, 25, 30]
yy = [30, 26, 23, 22, 21, 22, 23]
yy2 = [30, 25, 22, 20, 19, 18, 17.5]

# 항상 figure(1)만 사용
fig = plt.figure(1)
plt.clf() # figure(1) 전체 지우기
ax = fig.add_subplot(111)

# 원 데이터 점 (open symbol)
ax.plot(xx, yy, "ro", markerfacecolor='none')
ax.plot(xx, yy2, "bo", markerfacecolor='none')

# 다항식 피팅
YY = np.polyfit(xx, yy, 2)
YY2 = np.polyfit(xx, yy2, 2)

XX = np.linspace(xx[0], xx[-1], 20 * len(xx))

# 피팅 곡선
ax.plot(XX, np.polyval(YY, XX), 'r-')
ax.plot(XX, np.polyval(YY2, XX), 'b-')

# 범례, 레이블, 격자
ax.legend(['One ice cube', 'Three ice cubes'], fontsize=15)
ax.set_xlabel('time', fontsize=15)
ax.set_ylabel('Temperature', fontsize=15)
ax.grid()

plt.show()
전체 0