
데이터 시각화


실습을 위한 데이터 준비
np.random.seed(0)
df1 = pd.DataFrame(np.random.randn(100, 3), # 2018.1.1 부터 100일간의 랜덤 숫자 세개 지정
index=pd.date_range('1/1/2018', periods=100),
columns=['A', 'B', 'C']).cumsum()
df1.tail()
iris = sns.load_dataset("iris") # 붓꽃 데이터
titanic = sns.load_dataset("titanic") # 타이타닉호 데이터
Pandas 기본 Plot() 함수
Line Chart
df1.plot() # 기본 선차트
plt.title("Pandas의 Plot메소드 사용 예")
plt.xlabel("시간")
plt.ylabel("Data")
plt.show()
Bar Chart
iris.sepal_length[:20].plot(kind='bar', rot=0)
plt.title("꽃받침의 길이 시각화")
plt.xlabel("Data")
plt.ylabel("꽃받침의 길이")
plt.show()
iris[:5].plot.bar(rot=0)
plt.title("Iris 데이터의 Bar Plot")
plt.xlabel("Data")
plt.ylabel("각 Feature의 값")
plt.ylim(0, 7)
plt.show()
Barh Chart
iris[:5].plot.barh(rot=0) # 수평 방향 막대그래프
plt.title("Iris 데이터의 Bar Plot")
plt.xlabel("Data")
plt.ylabel("각 Feature의 값")
plt.ylim(0, 7)
plt.show()
Bar Chart
df2 = iris.groupby(iris.species).mean() # species 에 다른 각 수치별 평균값의 차이
df2.columns.name = "feature"
df2
Bar Chart
df2.plot.bar(rot=0) # 새롭게 정의된 데이터에 대해 바 그래프
plt.title("각 종의 Feature별 평균")
plt.xlabel("평균")
plt.ylabel("종")
plt.ylim(0, 8)
plt.show()
Pie Chart
df3 = titanic.pclass.value_counts() # value_counts 를 통해 pclass 마다의 인원수
df3.plot(kind='pie',autopct='%.2f%%')
plt.title("선실별 승객 수 비율")
plt.axis('equal')
plt.show()
Histogram
iris.plot.hist() # plot.hist()로 히스토그램 생성
plt.title("각 Feature 값들의 빈도수 Histogram")
plt.xlabel("데이터 값")
plt.show()
커널 밀도 함수(KDE)
iris.plot.kde() # 커널 밀도 함수 생성
plt.title("각 Feature 값들의 빈도수에 대한 Kernel Density Plot")
plt.xlabel("데이터 값")
plt.show()
Box Chart
iris.plot.box()
plt.title("각 Feature 값들의 빈도수에 대한 Box Plot")
plt.xlabel("Feature")
plt.ylabel("데이터 값")
plt.show()
Titanic 데이터에서 확인했던 차이들을 Pandas plot으로 표현해보자
Scatter Chart (산점도)
iris.plot.scatter(x='sepal_length',y='sepal_width') # sepal_length와 width의 관계를 산점도로 표현
plt.title("각 Feature의 종 별 데이터에 대한 SCatter Plot")
plt.show()