기본 콘텐츠로 건너뛰기

Python Matplotlib pyplot

import matplotlib.pyplot as plt

plt.plot([1,2,3],[1,2,3], 'bo') # 점(1,1), 점(2,2), 점(3,3)을 파란색 동그라미로 표시한다
plt.plot(x, y, 'go--', linewidth=2, markersize=12) # 초록으로 점을 찍고 쇠선으로 연결하고, 선 뚜께는 2, 점의 크기는 12로 한다

예제
plt.plot(y1, c='b', lw=5, ls='--', marker='+', ms=20, mec='m', mew=5, mfc='y') # lw : line width, ls : line style, ms : marker size, mec : marker edge width, mfc : marker face color














색깔
b : blue
g : green
r : red
c : cyan
m : magenta
y : yellow
k : black
w : white

x, y 라벨 이름 정하기
plt.xlabel('Basic Chart Test')
plt.ylabel('Random')

제목 정하기
plt.title('Title')

figure
fig = plt.figure()
ax1 = fig.add_subplot(2,2,1) # 2행, 2열, 1번째 차트
ax2 = fig.add_subplot(2,2,2) # 2행, 2열, 2번째 차트
ax3 = fig.add_subplot(2,2,3) # 2행, 2열, 3번째 차트
ax4 = fig.add_subplot(2,2,4) # 2행, 2열, 4번째 차트

ax1.plot([1,2,3],[1,2,3],'bo')
ax2.plot([1,2,3],[1,2,3],'r+')
ax3.plot([1,2,3],[1,2,3],'g*')
ax4.plot([1,2,3],[1,2,3],'k.')
plt.show()














범례
y1 = np.random.randint(1, 10, 10)
y2 = np.random.randint(1, 10, 10)

plt.plot(y1, 'r', label='Monthly')
plt.plot(y2, 'b', label='Inflation')
plt.legend() # 라벨의 값을 범례로 알려줌, 위치는 자동
plt.xlabel('X Data')
plt.ylabel('Y Data')

최소, 최대 크기 지정
plt.xlim(0,10) # x 좌표의 한계, 최소 0, 최대 10
plt.ylim(0,10) # y 좌표의 한계, 최소 0, 최대 10
plt.show()














막대 그래프(line chart)
# 회귀
plt.bar([1,2,3,4,5,6,7,8,9,10],y1)
plt.show()














산정도(scatter)
# 분류
x = np.arange(1, 11)
plt.scatter(x,y1)
plt.show()














이 블로그의 인기 게시물

Grid 정렬

  .container { display : grid ; gap : 22px ; width : 1000px ; grid-template-columns : repeat ( auto-fit , 150px ); margin : auto ; justify-content : center ; } .container {      display : grid ; gap : 22px ; grid-template-columns : repeat ( auto-fit , minmax ( 250px , 1fr )); }

Python 변수

지역 변수 a 전역 변수 밖에서 선언한 변수를 사용할 때 앞에 global 을 붙여준다     global a