Subplot

출처: https://wikidocs.net/4761

 

Subplot은 말그대로 한 화면에 여러 개의 그래프를 그리는 것이다.

Subplot을 그리는 방법에는 크게 2가지 방식이 있다.

 

# plt.figure(figsize=(8, 8))

plt.subplot(2, 2, 1)
plt.scatter(df1['x'], df1['y'])

plt.subplot(2, 2, 2)
plt.scatter(df2['x'], df2['y'])

plt.subplot(2, 2, 3)
plt.scatter(df3['x'], df3['y'])

plt.subplot(2, 2, 4)
plt.scatter(df4['x'], df4['y'])
# plt.title()

# plt.suptitle()

dd

 

plt.subplots(2, 2)

'''
(<Figure size 640x480 with 4 Axes>,
 array([[<Axes: >, <Axes: >],
        [<Axes: >, <Axes: >]], dtype=object))
'''

dd

 

fig, axes = plt.subplots(2, 2)
# fig, axes = plt.subplots(2, 2, figsize=(5, 5), sharex=True, sharey=True)

axes[0][0].scatter(df1['x'], df1['y'])
# axes[0][0].set_title()
# axes[0][0].set_xlabel()
# axes[0][0].set_xlim()

axes[0][1].scatter(df2['x'], df2['y'])

axes[1][0].scatter(df3['x'], df3['y'])

axes[1][1].scatter(df4['x'], df4['y'])

# fig.suptitle() / plt.suptitle()
# fig.tight_layout() / plt.tight_layout()

dd

 

dd

 

fig, axes = plt.subplots(2, 2)
axes = axes.flatten()

dfs = [df1, df2, df3, df4]
for i, df in zip(range(len(dfs)), dfs):
    axes[i].scatter(df['x'], df['y'])
    axes[i].set_title(f"{chr(i+8544)} subplot")

fig.suptitle("Subplots")
fig.tight_layout()

놀랍게도 axes는 2차원 객체라서 flatten() 함수로 1차원 변환이 가능하다.

이를 활용해 반복문으로 비교적 간단하게 subplot을 그리는 게 가능하다. 

 

seaborn

seaborn 그래프 종류 설명
barplot  
scatterplot  
regplot  
boxplot  
violinplot  
histplot  
countplot  
heatmap  
pairplot  

seaborn 라이브러리의 그래프 종류에 대한 설명이다.

 

그외 데이터 실습

 

 

+ Recent posts