Matplotlib.pyplot #3. 기타 plot들

코딩/Backend|2022. 3. 10. 23:00

 

지난 시간 알아본 Line plot 외에

  • 스캐터 플롯(scatter plot)
  • 컨투어 플롯(contour plot)
  • 서피스 플롯(surface plot)
  • 바 차트(bar chart)
  • 히스토그램(histogram)
  • 박스 플롯(box plot)
  • 파이 차트(pie chart)

다른 plot들의 사용법을 간단히 알아보자.

 

Scatter plot

plt.scatter(x, y, s=area, c=colors, alpha=0.5, cmap='Spectral')

x, y : data

s : 원의 size

  plt.plot 에서의 markersize=20과 scatter에서의 s=20**2 이 동일한 size이다.

c : color

cmap : c 값에 숫자를 대입하고 이에 대응하는 cmap에서 색상을 선택한다.

import numpy as np
import matplotlib.pyplot as plt

n = 50
x = np.random.rand(n)
y = np.random.rand(n)
p1 = plt.scatter(x, y, s=5**2, c='red', edgecolor='red', alpha=0.5)

n = 50
x = np.random.rand(n)
y = np.random.rand(n)
color = np.random.rand(n)
p1 = plt.scatter(x, y, s=20**2, c=color, cmap='Spectral', alpha=0.5)
plt.colorbar()

 

 

Bar chart

bar(x, height, width=0.8, bottom=None, *, align='center', data=None)

height가 곧 x에 대응하는 bar의 크기이므로 y나 마찬가지이다. 

또한 막대그래프로 나타내는 data의 경우, x가 간격은 같으나 꼭 scale이 맞을 필요는 없는 경우가 많다. 

ex) 연도별 수확량 

따라서 다음과 같은 구조를 만드는 것이 편하다.

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(3)
years = ['2018', '2019', '2020']
values = [100, 400, 900]
colors = ['y', 'dodgerblue', 'C2']

plt.bar(x, values, color=colors)
plt.xticks(x, years)

plt.show()

수평 막대그래프도 동일하며

plt.barh() method를 사용한다.

 

 

Histogram

histogram은 보통 도수분포 / 확률분포 를 나타낸다.

import matplotlib.pyplot as plt

weight = [68, 81, 64, 56, 78, 74, 61, 77, 66, 68, 59, 71,
          80, 59, 67, 81, 69, 73, 69, 74, 70, 65]

plt.hist(weight, bins=30, label='bins=30', width=0.4, cumulative=True, histtype='barstacked')
plt.legend()
plt.show()

histtype에는

'bar', 'barstacked', 'step', 'stepfilled' 등이 있다.

 

 

Boxplot

통계랑의 분포를 나타내는 boxplot도 간단히 그릴 수 있다. 

 

plt.subplot(1, 3, 1)
plt.boxplot([data.SLl, data.SLs])
plt.title("step length")
plt.xticks([1, 2], ["Long", "Short"])

plt.subplot(1, 3, 2)
plt.boxplot([data.StPDl, data.StPDs])
plt.title("stance phase\nduration")
plt.xticks([1, 2], ["Long", "Short"])

plt.subplot(1, 3, 3)
plt.boxplot([data.SwPDl, data.SwPDs])
plt.title("swing phase\nduration")
plt.xticks([1, 2], ["Long", "Short"])

plt.show()

 

<지속 추가 예정>

'코딩 > Backend' 카테고리의 다른 글

PyQt5 #2. Thread  (0) 2022.05.30
PyQt5 #1. 기본 개념  (0) 2022.05.25
Matplotlib.pyplot #2. Line plot  (0) 2022.03.10
Matplotlib.pyplot #1. 기본 사용법  (0) 2022.03.04
Pandas - Series, DataFrame  (0) 2022.03.04

댓글()