본문 바로가기

Python/시각화

시각화_03

반응형
day4_시각화2

시각화 셋팅

In [2]:
import numpy as np
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt

# 한글 폰트 패스로 지정 
import matplotlib.font_manager as fm
path = 'C:\Windows\Fonts\malgunbd.ttf'
font_name = fm.FontProperties(fname=path).get_name()
plt.rc('font', family=font_name)

# 자동 사이즈 조정 
%matplotlib inline

Scatter Plot

  • fiqure 생성
  • plt.sctter(xData,yData,s=Size, c=colorName, marker=markerName)
  • makerName : X / O / D / s / *
In [11]:
names = ['a', 'b', 'c', 'd', 'e']
values = np.random.randint(30,50,5)
values
Out[11]:
array([37, 49, 38, 45, 35])
In [12]:
plt.scatter(names, values, s=200, c='blue', marker='s')
plt.suptitle('산포도')
plt.show()

scatter plot - value 표시

  • 총 갯수 산출 : len(xData)
  • for 문 이용
  • value 표시 : plt.text(xPos, yPos, value)
In [4]:
names = ['group_a', 'group_b', 'group_c', 'group_d', 'group_e']
values = np.random.randint(30,50,5)

num = len(names)

fig_s2 = plt.figure()
fig_s2, plt.scatter(names, values, color='red')

# value 표시 for문
for i in np.arange(num):
    fig_s2, plt.text(names[i], values[i], values[i])
    
fig_s2, plt.suptitle('Scatter Plot Sample')
fig_s2, plt.show()
[35 43 36 41 33]
Out[4]:
(<Figure size 432x288 with 1 Axes>, None)

Scatter Plot

- colorRandom, size는Y데이타와상관

  • Dot의 크기와 색상 변화
    colorValue = np.random.rand(x데이타총갯수)
    fig, 변수 = plt.subplots()
    변수.scatter(xData, yData, s=크기, c=colorValue )
In [16]:
x = ['a','b','c','d','e','f', 'g']
y = np.random.rand(len(x))
colorValue =  np.random.rand(len(x))

fig, ax = plt.subplots()
ax.scatter(x, y, s=y*1000+70, c=colorValue )

plt.show()

Pie Plot

  • figure 생성
  • plt.pie( values, labels=labelList, autopct='%digit.pointNum%Unit')
In [21]:
# Label + Value 
labelList = ['Frogs', 'Hogs', 'Dogs', 'Logs']
sizes = [15, 30, 45, 10]
fig_p1 = plt.figure()
fig_p1, plt.pie(sizes, labels=labelList, autopct='%.1f%%')
plt.show()

Pie Plot - Explode

  • plt.pie(dataValue, explode=explodeList, labels=labels, autopct='%digit.pointNum%Unit', shadow=True, startangle=angleValue)
In [35]:
# label, value 생성 
labelList = 'Frogs', 'Hogs', 'Dogs', 'Logs'
sizes = [15, 30, 45, 10]

# 파이 조각에 대한 색상 리스트 
colorList=('b', 'g', 'r', 'c')

# 슬라이싱되는 값은 각각의 labelData와 연계
# explode = (0,0,0,0) 
explode = (0.05, 0.1, 0.25, 0.05)

# 그림자 옵션 shadow=True
# 시작 각도 지정 startangle=angleValue


fig_p2 = plt.figure()
fig_p2, plt.pie(sizes, explode=explode, labels=labelList, 
                autopct='%.1f%%', shadow=True, startangle=180, 
                textprops={'fontsize': 14}, colors=colorList)

plt.show()

Histogram plot

  • figure 생성
  • plt.hist(dataVales, bins=number, color='colorName')
In [41]:
points = np.random.randn(1000)

fig_h1 = plt.figure()
fig_h1, plt.hist(points, bins=20, color='hotpink')
Out[41]:
(<Figure size 432x288 with 1 Axes>,
 (array([  1.,   0.,   0.,   3.,  14.,  28.,  45.,  75.,  93., 105., 154.,
         161., 112.,  79.,  68.,  39.,  10.,   7.,   3.,   3.]),
  array([-3.93745407, -3.57706974, -3.21668542, -2.85630109, -2.49591677,
         -2.13553245, -1.77514812, -1.4147638 , -1.05437947, -0.69399515,
         -0.33361083,  0.0267735 ,  0.38715782,  0.74754215,  1.10792647,
          1.46831079,  1.82869512,  2.18907944,  2.54946377,  2.90984809,
          3.27023241]),
  <a list of 20 Patch objects>))

Histogram + Subplot

  • figure 생성
  • figureVar, subplotVar = plt.subplots(row,col, sharey=True)
    sharey=True : 간격 자동 지정
  • subplotVar[index].hist(dataVales, bins=number, color='colorName')
In [39]:
n_bins = 20

x = np.random.randn(100000)
y = np.random.randn(100000)

fig_h2 = plt.figure(figsize=(12,12))
fig_h2, axs = plt.subplots(1, 2, sharey=True)

fig_h2, axs[0].hist(x, bins=n_bins, color='yellowgreen')
fig_h2, axs[1].hist(y, bins=n_bins, color='brown')
Out[39]:
(<Figure size 432x288 with 2 Axes>,
 (array([2.0000e+00, 8.0000e+00, 3.8000e+01, 1.7100e+02, 7.1200e+02,
         1.8930e+03, 4.4310e+03, 8.6280e+03, 1.3489e+04, 1.7183e+04,
         1.7967e+04, 1.5191e+04, 1.0400e+04, 5.9470e+03, 2.5670e+03,
         9.7700e+02, 2.9200e+02, 8.6000e+01, 1.3000e+01, 5.0000e+00]),
  array([-4.65398683, -4.19705066, -3.74011449, -3.28317832, -2.82624215,
         -2.36930597, -1.9123698 , -1.45543363, -0.99849746, -0.54156129,
         -0.08462512,  0.37231105,  0.82924722,  1.28618339,  1.74311956,
          2.20005573,  2.6569919 ,  3.11392807,  3.57086425,  4.02780042,
          4.48473659]),
  <a list of 20 Patch objects>))
<Figure size 864x864 with 0 Axes>

데이타프레임 그래프화

In [ ]:
 
반응형

'Python > 시각화' 카테고리의 다른 글

시각화_05. 워드클라우드02  (0) 2019.08.26
시각화_04. 워드클라우드01  (0) 2019.08.26
시각화_02. matplotlib  (0) 2019.08.26
시각화_01. seaborn  (0) 2019.08.26