No img


10. 파이썬 시각화(feat.Matplotlib)

Posted by bonggang | 2019. 10. 16. 02:58

파이썬을 이용해서 기본적인 그래프를 그릴 때 사람들이 많이 사용하는 라이브러리 Matplotlib 사용법을 정리해두기 위해 글을 작성한다.

 

선 그래프 그리기


기본 그래프

from matplotlib import pyplot as plt

plt.plot([1, 2, 3], [100, 200, 300])#plot(x축,y축)
plt.show()

 

그래프 색 변환

from matplotlib import pyplot as plt

plt.plot([1, 2, 3], [100, 200, 300], 'y')#plot(x축,y축, 색)
plt.show()

 

라벨 및 타이틀 표시

from matplotlib import pyplot as plt

plt.plot([1, 2, 3], [100, 200, 300])
plt.xlabel('x label')#x축 라벨
plt.ylabel('y label')#y축 라벨
plt.title('title')#title
plt.show()

 

범례 표시

from matplotlib import pyplot as plt

plt.plot([1, 2, 3], [100, 200, 300])
plt.legend(['first'])#범례
plt.show()

 

어노테이션

from matplotlib import pyplot as plt

plt.plot([1, 2, 3], [100, 200, 300])
plt.annotate('info', xy=(1, 100), xytext=(1.5, 100), arrowprops={'color': 'red'})
plt.show()

 

선 종류

from matplotlib import pyplot as plt

plt.plot([1, 2, 3], [100, 200, 300], 'or') 'or'-> o: 스타일, r: 색상
plt.show()

 

서브 플롯

from matplotlib import pyplot as plt

plt.subplot(2, 1, 1)
plt.plot([1, 2, 3], [100, 200, 300])
plt.subplot(2, 1, 2)
plt.plot([1, 2, 3], [10, 20, 30])
plt.show()

 

막대 그래프 그리기


from matplotlib import pyplot as plt

plt.bar([1, 2, 3], [10, 20, 30], width=0.5) #x축, y축, 막대넓이
plt.show()

 

from matplotlib import pyplot as plt

plt.barh([1, 2, 3], [10, 20, 30]) #x축, y축
plt.show()

 

점 그래프 그리기


from matplotlib import pyplot as plt

plt.scatter([1, 3, 2, 4, 5], [10, 5, 20, 30, 1]) #x축, y축
plt.show()

 

원형 그래프(파이) 그리기


from matplotlib import pyplot as plt

label = ['first', 'second', 'third', 'fourth']
plt.pie([10, 20, 30, 40], labels=label) #퍼센트, 라벨
plt.show()

 

 

박스플롯 그리기


from matplotlib import pyplot as plt

plt.boxplot([1, 2, 2, 3, 3, 4, 3, 3, 2, 2, 2, 2, 1])
plt.show()