Python

[Python] Scatterplot

SangRok Jung 2022. 9. 30. 23:14
반응형

Scatterplot


  • 서로 다른 두 변수 사이의 관계를 표시합니다.
  • 각 변수는 연속되는 값, 일반적으로 정수형 도는 실수형의 데이터 입니다.
  • 2개의 연속 변수를 각각 x, y축에 하나씩 놓아, 데이터 값이 위치하는 좌표를 찾아서 점으로 표시합니다.
  • 옵션
    • c : 점의 색상
    • s : 점의 크기
    • alpha : 투명도

 

 

▷ 생성문

DataFrame.plot(kind='scatter')

 

 

 

 

▶ mpg, weight의 축을 가진 히스토그램을 생성합니다.

df_auto.plot(kind='scatter',x='weight', y ='mpg', s=10,
             c='FireBrick', figsize=(6,3))

plt.title("mpg VS weight")

 

 

▶ Cylinder의 값을 점의 사이즈로 설정합니다.

cylinders_size = df_auto.cylinders / df_auto.cylinders.max() * 300

df_auto.plot(kind='scatter',x='weight', y ='mpg', s=cylinders_size,
             c='FireBrick', figsize=(10,5), alpha=0.3)

plt.title("Scatter plot : mpg-weight-cylinders")

 

 

 

▶ 여러개의 색을 모아놓은 cmap을 설정하여 생성합니다.

# marker='+', cmap='viridis' 활용
df_auto.plot(kind='scatter',x='weight', y ='mpg', c=cylinders_size,
             figsize=(10,5), alpha=0.7, marker='+', s=50, cmap='viridis')

plt.title("Scatter plot : mpg-weight-cylinders")

# 현재 폴더에 png 파일 저장 
plt.savefig('./sactter.png')
plt.savefig('./sactter_transparent.png', transparent=True)

 

 

 

 

▶ 반복문으로 각각의 그래프들을 저장합니다.

for i in ['cylinders', 'displacement', 'horsepower', 'weight', 'acceleration',
       'model year']:
    df_auto.plot(kind='scatter', x=i, y='mpg', figsize=(10,5))
    plt.title(f'mpg vs {i}')
    plt.savefig(f'scatter_mpg vs {i}.png')

 

 

 

 

반응형

'Python' 카테고리의 다른 글

[Python] Boxplot  (0) 2022.09.30
[Python] Pie  (0) 2022.09.30
[Python] Histogram  (1) 2022.09.30
[Python] 시각화 도구  (0) 2022.09.28
[Python] pandas 내장 그래프 도구  (0) 2022.09.27