본문 바로가기
IT 이야기/Python

[Python] Matplotlib

by Dblog 2020. 12. 7.
728x90

https://matplotlib.org/

 


간단한 cmd 명령어를 통해 다운로드 가능합니다.

 

pip install matplotlib 

 

Matplotlib는 데이터를 시각화 하는데 사용하는 라이브러리 입니다.

bar, scatter, line, histogram 등 다양한 feature를 보유하고 있으며 게시글 에서는 bar, plot 두개의 예시를 보이겠습니다.


Bar

막대 그래프를 나타냅니다. 
사용자는 x, y축을 custom할 수 있고 막대 마다색을 지정할 수 있습니다.

아래와 같은 예시로 데이터를 custom 할 수 있습니다.

import matplotlib.pyplot as plt


labels = ['G1', 'G2', 'G3', 'G4', 'G5']
men_means = [20, 35, 30, 35, 27]
women_means = [25, 32, 34, 20, 25]
men_std = [2, 3, 4, 1, 2]
women_std = [3, 5, 2, 3, 3]
width = 0.35       # the width of the bars: can also be len(x) sequence

fig, ax = plt.subplots()

ax.bar(labels, men_means, width, yerr=men_std, label='Men')
ax.bar(labels, women_means, width, yerr=women_std, bottom=men_means,
       label='Women')

ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.legend()

plt.show()

 

 

Simple Plot

가장 기본이 되는 두번째 plot 입니다. 값에 따라 유연한 곡선 그래프 혹은 일자 선으로 나타날 수 있습니다.

import matplotlib
import matplotlib.pyplot as plt
import numpy as np

# Data for plotting
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)

fig, ax = plt.subplots()
ax.plot(t, s)

ax.set(xlabel='time (s)', ylabel='voltage (mV)',
       title='About as simple as it gets, folks')
ax.grid()

fig.savefig("test.png")
plt.show()

 


 

Example

링크에서 다양한 예시를 찾을 수 있습니다.

https://matplotlib.org/

 

Matplotlib: Python plotting — Matplotlib 3.3.3 documentation

 

matplotlib.org

 

 

  • Line, bar and marker

 

  • Images, contours and fields

 

  • Subplots, axes and figures

 

  • Statistics

 

아래는 과거 데이터 분석 프로젝트 도중 서울시 지하철, 버스 정류소 위치를 scatter plot에 나타낸 결과 입니다.

 

 

 

728x90

'IT 이야기 > Python' 카테고리의 다른 글

[Python] sklearn  (0) 2020.12.08

댓글