ComputerLanguage_Program/PYTHON
[PYTHON] matplotlib 으로 seaboard scatterplot 구현하기
pbj0812
2021. 12. 14. 00:33
0. 목표
- matplotlib 으로 seaborn scatterplot 구현하기
1. 실습
1) library 호출
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
2) 데이터 호출
df = sns.load_dataset('tips')
3) seaborn 으로 그리기
sns.scatterplot(x = 'total_bill', y = 'tip', hue = 'size', style = 'sex', palette = 'Blues', data = df)
4) matplotlib 으로 그리기
(1) 색깔만 바꾸기
fig, ax = plt.subplots()
scatter = ax.scatter(df['total_bill'], df['tip'], c = df['size'], cmap = 'Blues')
legend1 = ax.legend(*scatter.legend_elements(num = len(df['size'].unique())), loc="upper left", title = "size")
(2) 모양 바꾸기
fig, ax = plt.subplots()
ax.scatter(b[0]['total_bill'], b[0]['tip'], marker = 'x', label = 'F')
ax.scatter(b[1]['total_bill'], b[1]['tip'], marker = 'o', label = 'M')
ax.legend(title = 'sex')
(3) 색 + 모양 바꾸기
fig, ax = plt.subplots()
df2 = df.groupby('sex')
marker = ['x', 'o']
tmp = 0
for name, group in df2:
scatter = ax.scatter(group['total_bill'], group['tip'], marker = marker[tmp], c = group['size'], cmap = 'Blues')
legend1 = ax.legend(*scatter.legend_elements(num = len(group['size'].unique())), loc="upper left", title = "size")
tmp += 1