라일락 꽃이 피는 날

[Matplotlib] 다양한 그래프 그리기 2 본문

데이터 분석/Python

[Matplotlib] 다양한 그래프 그리기 2

eunki 2021. 5. 11. 15:25
728x90

6. Pie Chart

explode: 파이에서 툭 튀어져 나온 비율

autopct: 퍼센트 자동으로 표기

shadow: 그림자 표시

startangle: 파이를 그리기 시작할 각도

texts, autotexts 인자를 활용하여 텍스트 스타일링을 적용

texts는 label에 대한 텍스트 효과, autotexts는 파이 위에 그려지는 텍스트 효과를 다룬다.

labels = ['Samsung', 'Huawei', 'Apple', 'Xiaomi', 'Oppo', 'Etc']
sizes = [20.4, 15.8, 10.5, 9, 7.6, 36.7]
explode = (0.3, 0, 0, 0, 0, 0)

 

 

 

 

7. Box Plot

spread = np.random.rand(50) * 100
center = np.ones(25) * 50
flier_high = np.random.rand(10) * 100 + 100
flier_low = np.random.rand(10) * 100 - 100

data = np.concatenate((spread, center, flier_high, flier_low))

 

 

 

다중 박스플롯 생성

다중 그래프 생성을 위해서는 data 자체가 2차원으로 구성되어 있어야 한다.

 

data = np.concatenate((spread, center, flier_high, flier_low))
d2 = np.concatenate((spread, center*0.8, flier_high, flier_low))

data.shape = (-1, 1)
d2.shape = (-1, 1)

data = [data, d2, d2[::2,0]]

 

 

 

Box Plot 축 바꾸기

vert=False 옵션을 통해 표시하고자 하는 축을 바꿀 수 있다.

 

 

 

Outlier 마커 심볼과 컬러 변경

 

 

 

 

8. 3D 그래프 그리기

from mpl_toolkits import mplot3d

 

 

 

3d plot 그리기

x = np.sin(z)
y = np.cos(z)
z = np.linspace(0, 15, 1000)

 

 

 

sample_size = 100

x = np.cumsum(np.random.normal(0, 1, sample_size))
y = np.cumsum(np.random.normal(0, 1, sample_size))
z = np.cumsum(np.random.normal(0, 1, sample_size))

 

 

 

3d-scatter 그리기

sample_size = 500

x = np.cumsum(np.random.normal(0, 5, sample_size))
y = np.cumsum(np.random.normal(0, 5, sample_size))
z = np.cumsum(np.random.normal(0, 5, sample_size))

 

 

 

contour3D 그리기 (등고선)

x = np.linspace(-6, 6, 30)
y = np.linspace(-6, 6, 30)
x, y = np.meshgrid(x, y)
z = np.sin(np.sqrt(x**2 + y**2))

 

 

 

 

9. imshow

이미지 데이터와 유사하게 행과 열을 가진 2차원의 데이터를 시각화할 때 활용한다.

load_digits는 0~16 값을 가지는 array로 이루어져 있다.

1개의 array는 8 X 8 배열 안에 표현되어 있고, 숫자는 0~9로 이루어져 있다.

from sklearn.datasets import load_digits

digits = load_digits()
X = digits.images[:10]
X[0]

 

728x90