-
[Data Viz] 1. Matplotlib인공지능/부스트캠프 Ai Tech 2022. 2. 4. 21:17728x90
[Data Viz] 1. Matplotlib In [1]:from IPython.core.display import display, HTML display(HTML("<style>.container { width:90% !important; }</style>"))
In [3]:import numpy as np import matplotlib as mpl print(f'numpy version : {np.__version__}') # version check print(f'matplotlib version : {mpl.__version__}') # version check # pylab은 pyplot으로 대체됨 import matplotlib.pyplot as plt
numpy version : 1.21.2 matplotlib version : 3.5.0
In [4]:fig = plt.figure() plt.show() # 그림을 그려달라.
<Figure size 432x288 with 0 Axes>
Figure에 서브플롯을 최소 1개 이상 추가해야 한다.(천개 만개도 가능..)
In [5]:fig = plt.figure() # fig라는 객체에 subplot을 추가한다. ax = fig.add_subplot() plt.show()
그래프의 사이즈는 figure의 사이즈, 서브플롯 ax의 사이즈로 조정한다.
가로, 세로(inch 단위)를 튜플 형태로
figsize
에 전달하여 조절한다.In [8]:fig = plt.figure(figsize=(12, 7)) # fig의 색을 변경하는 코드, fig라는 팔레트에 ax라는 subplot을 그린 형태 #fig.set_facecolor('black') ax = fig.add_subplot() plt.show()
세로로 쪼개서 사용
In [14]:fig = plt.figure() # 가로 1 세로 2로 나눴을때 첫번째에 추가 # ax = fig.add_subplot(1,2,1) ax = fig.add_subplot(121) ax = fig.add_subplot(122) plt.show()
가로로 쪼개서 사용
In [17]:fig = plt.figure() # 가로 1 세로 2로 나눴을때 첫번째에 추가 # ax = fig.add_subplot(1,2,1) ax = fig.add_subplot(211) ax = fig.add_subplot(212) plt.show()
In [18]:fig = plt.figure() # 가로 1 세로 2로 나눴을때 첫번째에 추가 # ax = fig.add_subplot(1,2,1) ax = fig.add_subplot(221) ax = fig.add_subplot(224) plt.show()
그래프 그리기¶
plot
을 사용하여 선그래프를 그릴 수 있다.In [19]:fig = plt.figure() ax = fig.add_subplot() x = np.array([1, 2, 3]) plt.plot(x) plt.show()
plt를 사용하여 그래프를 순차적으로 그릴 수 있다.
서브플롯 객체 ax에 직접 그리기¶
ax
객체에 직접 그리면 pythonic하게 그릴 수 있다.- pyplot API : 순차적 방법
- 객체지향 API : 그래프에서 각 객체에 대해 직접 수정
In [23]:# pyplot API fig = plt.figure() x1 = [1,2,3] x2 = [3,2,1] ax = fig.add_subplot(211) plt.plot(x1) ax2 = fig.add_subplot(212) plt.plot(x2) plt.show()
In [22]:# 객체지향 API fig = plt.figure() x1 = [1,2,3] x2 = [3,2,1] ax1 = fig.add_subplot(211) ax2 = fig.add_subplot(212) ax1.plot(x1) ax2.plot(x2) plt.show()
한 서브플롯에 여러 그래프 그리기
In [24]:fig = plt.figure() ax = fig.add_subplot(111) # 3개 plot 동시에 그리기 ax.plot([1, 1, 1]) ax.plot([1, 2, 3]) ax.plot([3, 3, 3]) plt.show()
같은 종류의 그래프는 자동으로 색상이 구분된다.
In [25]:fig = plt.figure() ax = fig.add_subplot(111) # 서로 다른 종류의 그래프를 그릴시 색상 구분이 안됨 ax.plot([1,2,3],[1,2,3]) ax.bar([1,2,3],[1,2,3]) plt.show()
In [26]:fig = plt.figure() ax = fig.add_subplot(111) # 3개 plot 동시에 그리기 ax.plot([1, 1, 1], color='r')# 한 글자로 지정 ax.plot([1, 2, 3], color='forestgreen') # color name ax.plot([3, 3, 3], color='#000000') # hex code, 가장 추천 plt.show()
In [28]:fig = plt.figure() ax = fig.add_subplot(111) ax.plot([1, 1, 1], label='1') ax.plot([1, 2, 3], label='2') ax.plot([3, 3, 3], label='3') ax.legend() # 범례를 쓰지 않으면 그래프에 나타나지 않는다 ax.set_title("Basic Plot") # 제목을 추가 plt.show()
In [30]:fig = plt.figure() ax1 = fig.add_subplot(1,2,1) ax2 = fig.add_subplot(1,2,2) # subplot 각각에 타이틀 지정 가능 ax1.set_title('ax1') ax2.set_title('ax2') fig.suptitle('fig') # sup(super)title plt.show()
📢
ax에서 특정 데이터를 변경하는 경우 :
.set_{}()
형태의 메서드가 많다.set
으로 세팅하는 정보들은 정보를 받아올때는.get_{}()
형태의 메서드를 사용한다.ax.get_title()
In [31]:fig = plt.figure() ax = fig.add_subplot(111) ax.plot([1, 1, 1], label='1') ax.plot([1, 2, 3], label='2') ax.plot([3, 3, 3], label='3') ax.legend() ax.set_title("Basic Plot") print(ax.get_title()) # ax의 title을 가져옴 plt.show()
Basic Plot
축¶
축은
ticks
와ticklabels
로 구분됨In [32]:fig = plt.figure() ax = fig.add_subplot(111) ax.plot([1, 1, 1], label='1') ax.plot([1, 2, 3], label='2') ax.plot([3, 3, 3], label='3') ax.legend() ax.set_title("Basic Plot") ax.set_xticks([0, 1, 2]) plt.show()
In [34]:fig = plt.figure() ax = fig.add_subplot(111) ax.plot([1, 1, 1], label='1') ax.plot([1, 2, 3], label='2') ax.plot([3, 3, 3], label='3') ax.legend() ax.set_xticks([0, 1, 2]) ax.set_xticklabels(['zero', 'one', 'two']) # 라벨을 붙여줌 plt.show()
그래프에 텍스트를 추가하는 방법
ax.text(x,y,s=문자열)
: 원하는 위치에 적어줌ax.annotate(text,xy=(x,y))
: 원하는 위치에 텍스트를 지정함xytext=(x,y), arrowprop=
: 화살표까지 추가 가능
In [35]:fig = plt.figure() ax = fig.add_subplot(111) ax.plot([1, 1, 1], label='1') ax.plot([1, 2, 3], label='2') ax.plot([3, 3, 3], label='3') ax.legend() ax.set_xticks([0, 1, 2]) ax.set_xticklabels(['zero', 'one', 'two']) # 라벨을 붙여줌 ax.text(x=1, y=2, s="Text") plt.show()
In [37]:fig = plt.figure() ax = fig.add_subplot(111) ax.plot([1, 1, 1], label='1') ax.plot([1, 2, 3], label='2') ax.plot([3, 3, 3], label='3') ax.legend() ax.set_xticks([0, 1, 2]) ax.set_xticklabels(['zero', 'one', 'two']) # 라벨을 붙여줌 ax.annotate(text="Annotate", xy=(1,2), xytext=(1.2, 2.2), arrowprops=dict(facecolor='black')) plt.show()
'인공지능 > 부스트캠프 Ai Tech' 카테고리의 다른 글
[Data Viz] 2-2. Line Plot (0) 2022.02.04 [Data Viz] 2-1. Bar Plot (0) 2022.02.04 [Data Viz] 0. 시각화 (0) 2022.02.04 [Pytorch] hook&apply (0) 2022.02.01 [Pytorch]5. Model Load (0) 2022.01.28