from matplotlib import rc
rc('font',family='Malgun Gothic')
import matplotlib.pyplot as plt # matlab의 기능을 담아둔 것이 pyplot
%matplotlib inline # 주피터노트북에 포함시켜서 나타나게 하는 옵션
plt.rcParams('axes.unicode_minus'] = False
# 마이너스 부호 때문에 한글이 깨질 수 있어서 설정하는 부분
plt.figure(figsize=(10,6))
plt.plot([0,1,2,3,4,5,6,7,8,9], [1,1,2,3,4,2,3,5,-1,3]) # x축, y축
plt.show()
matplotlib 기본 형태
import numpy as np
t = np.arange(0,12,0.01) # 0부터 12까지 0.01간격으로
y = np.sin(t)
def drawGraph():
plt.figure(figsize=(10,6))
plt.plot(t, np.sin(t), label='sin') #label : 해당 그래프의 이름
plt.plot(t, np.cos(t), label='cos')
plt.grid() # 그래프 배경에 격자 표시
plt.legend() # 범례(plot의 label 표시)
plt.xlabel('time') # x축 제목
plt.ylabel('Amplitude') # y축 제목
plt.title('Example of sinewave') #전체 그래프의 제목
plt.show()
t = np.arange(0,5,0.5)
def drawGraph():
plt.figure(figsize=(10,6))
plt.plot(t,t, 'r--') # r-- : r은 빨간색, --은 점선
plt.plot(t,t**2, 'bs') # bs : b는 파랑색, s는 사각형
plt.plot(t,t**3, 'g^') # g^ : g는 초록색, ^는 위 방향 화살표
plt.show()
t = [0, 1, 2, 3, 4, 5, 6]
y = [1, 4, 5, 8, 9, 5, 3]
def drawGraph():
plt.figure(figsize=(10,6))
plt.plot(
# x축
t,
# y축
y,
# 그래프 색상
color = "green",
# 그래프 스타일
linestyle="dashed",
# 마커 스타일
marker="o",
# 마커 색상
markerfacecolor="blue",
# 마커 사이즈
markersize=12,
)
# x축과 y축의 범위 지정
plt.xlim([-0.5, 6.5])
plt.ylim([0.5,9.5])
plt.show()
t = np.array([0,1,2,3,4,5,6,7,8,9])
y = np.array([9,8,7,9,8,3,2,4,3,4])
def drawGraph():
plt.figure(figsize=(10,6))
plt.scatter(t,y)
plt.show()
colormap = t
def drawGraph():
plt.figure(figsize=(10, 6))
# marker : 점의 모양
plt.scatter(t, y, s=50, c=colormap, marker=">")
# colormap의 색상을 막대 그래프로 출력
plt.colorbar()
plt.show()
표로만 정보를 전달하는 것은 한계가 있다.
이를 보완하기 위해 시각화(Visualization)한다.
matplotlib에서 한글 사용을 위해 폰트 변경
import matplotlib.pyplot as plt
from matplotlib import rc
# 마이너스 부호 때문에 한글이 깨질 수 있기 때문에 설정
plt.rcParams["axes.unicode_minus"] = False
rc('font', family='Malgun Gothic')
data_result['소계'].plot(kind=''barh', grid=True, figsize=(10,6)); # barh: 수평바
def drawGraph():
date_result['소계'].sort_values().plot(
kind='barh',
grid=True,
title='가장 CCTV가 많은 구',
figsize=(10,10)
)
def drawGraph():
date_result['CCTV비율'].sort_values().plot(
kind='barh',
grid=True,
title='가장 CCTV가 많은 구',
figsize=(10,10)
)
def drawGraph():
plt.figure(figsize=(10,6))
plt.scatter(data_result['인구수'], data_result['소계'], s=50)
plt.xlabel('인구수')
plt.ylabel('CCTV')
plt.grid()
plt.show()
fp1 = np.polyfit(data_result['인구수'], data_result['소계'],1)
# 인구수를 x축, CCTV개수를 y축으로 1차식을 만들어달라
fp1
f1 = np.poly1d(fp1)
f1(400000)
fx = np.linspace(100000,700000,100)
def drawGraph():
plt.figure(figsize=(10,6))
plt.scatter(data_result['인구수'], data_result['소계'],s=50)
plt.plot(fx,f1(fx), ls='dashed', lw=3, color='g')
plt.xlabel('인구수')
plt.ylabel('CCTV')
plt.grid()
plt.show()
fp1 = np.polyfit(data_result["인구수"], data_result["소계"], 1)
f1 = np.poly1d(fp1)
fx = np.linspace(100000, 700000, 100)
data_result["오차"] = data_result["소계"] - f1(data_result["인구수"])
# 경향과 비교해서 데이터의 오차가 너무 나는 데이터 계산
df_sort_f = data_result.sort_values(by="오차", ascending=False)
df_sort_t = data_result.sort_values(by="오차", ascending=True)
df_sort_t.head()
from matplotlib.colors import ListedColormap
# color map을 사용자 정의(user define)로 세팅
color_step = ['#e74c3c','#2ecc71','#95a5a6','#2ecc71','#3498db','#3498db']
my_cmap = ListedColormap(color_step)
def drawGraph():
plt.figure(figsize=(14,10))
plt.scatter(data_result['인구수'], data_result['소계'],c=data_result['오차'],s=50, cmap=my_map) # cmap : 사용자 정의한 맵을 적용 / c : color 세팅에 방금 계산한 경향과의 오차를 적용
plt.plot(fx,f1(fx), ls='dashed', lw=3, color='grey')
# 오차가 큰 데이터 아래 위로 5개씩 특별히 마커 옆에 구 이름을 명시
for n in range(5):
plt.text(
df_sort_f['인구수'][n] * 1.02,# 구 이름이 마커에 겹치지 않도록
df_sort_f['소계'][n] * 0.98,# 구 이름이 마커에 겹치지 않도록
df_sort_f.index[n],
fontsixe=15,
)
plt.text(
df_sort_t['인구수'][n] * 1.02,# 구 이름이 마커에 겹치지 않도록
df_sort_t['소계'][n] * 0.98,# 구 이름이 마커에 겹치지 않도록
df_sort_t.index[n],
fontsixe=15,
)
plt.xlabel('인구수')
plt.ylabel('CCTV')
plt.colorbar()
plt.grid()
plt.show()
data_result.to_csv('저장 경로/파일 이름', sep=',',encoding='utf-8')