import matplotlib.pyplot as plt
from matplotlib import rc
rc("font", family="Malgun Gothic")
# %matplotlib inline (밑에 꺼랑 같은 기능)
get_ipython().run_line_magic("matplotlib", "inline")
plt.rcParams['axes.unicode_minus'] = False # 폰트 설정 이후 마이너스 기호 문제 해결하기 위한 코드
데이터를 그래프로 그리기 위한 겉에 배경인 도화지를 설정해 주는 것
plt.figure(figsize=(10, 6)) # 배경을 가로축은 10, 세로축은 6으로 설정
plt.plot()
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])
plt.show()
import numpy as np
t = np.arange(0, 12, 0.01) # 0부터 12까지 0.01 간격으로
y = np.sin(t)
plt.figure(figsize=(10, 6))
plt.plot(t, np.sin(t))
plt.plot(t, np.cos(t))
plt.show()
plt.figure(figsize=(10, 6))
plt.plot(t, np.sin(t))
plt.plot(t, np.cos(t))
plt.grid(True) # 격자무늬 추가
plt.show()
plt.figure(figsize=(10, 6))
plt.plot(t, np.sin(t))
plt.plot(t, np.cos(t))
plt.grid(False) # 격자무늬 추가 X
plt.show()
plt.figure(figsize=(10, 6))
plt.plot(t, np.sin(t))
plt.plot(t, np.cos(t))
plt.grid(True)
plt.title("Example of sinewave") # 그래프 제목 추가
plt.show()
plt.figure(figsize=(10, 6))
plt.plot(t, np.sin(t))
plt.plot(t, np.cos(t))
plt.grid(True)
plt.title("Example of sinewave")
plt.xlabel("time") # x축, y축 제목추가
plt.ylabel("Amplitude") # 진폭
plt.show()
plt.figure(figsize=(10, 6))
plt.plot(t, np.sin(t))
plt.plot(t, np.cos(t))
plt.grid(True)
# 주황색, 파란색 선 데이터 의미 구분
plt.legend(labels=["sin", "cos"]) # 범례
plt.title("Example of sinewave")
plt.xlabel("time")
plt.ylabel("Amplitude") # 진폭
plt.show()
주황색, 파란색 선 데이터 의미 구분 2
plt.figure(figsize=(10, 6))
plt.plot(t, np.sin(t), label="sin")
plt.plot(t, np.cos(t), label="cos")
plt.grid(True)
plt.legend() #범례 (컴퓨터가 자동으로 빈 공간이라고 인식하는 곳에 넣어줌)
plt.title("Example of sinewave")
plt.xlabel("time")
plt.ylabel("Amplitude") # 진폭
plt.show()
주황색, 파란색 선 데이터 의미 구분 (범례 위치 변경)
plt.figure(figsize=(10, 6))
plt.plot(t, np.sin(t), label="sin")
plt.plot(t, np.cos(t), label="cos")
plt.grid(True)
plt.legend(loc="upper right") #범례를 오른쪽 상단에 넣음
plt.title("Example of sinewave")
plt.xlabel("time")
plt.ylabel("Amplitude") # 진폭
plt.show()
주황색, 파란색 선 데이터 의미 구분 (범례 위치 변경)2
plt.figure(figsize=(10, 6))
plt.plot(t, np.sin(t), label="sin")
plt.plot(t, np.cos(t), label="cos")
plt.grid(True)
plt.legend(loc=2) #범례를 오른쪽 상단에 넣음
plt.title("Example of sinewave")
plt.xlabel("time")
plt.ylabel("Amplitude") # 진폭
plt.show()
함수에 넣어서 깔끔하게 표현
def drawGraph():
plt.figure(figsize=(10, 6))
plt.plot(t, np.sin(t), label="sin")
plt.plot(t, np.cos(t), label="cos")
plt.grid(True)
plt.legend(loc=2) #범례를 오른쪽 상단에 넣음
plt.title("Example of sinewave")
plt.xlabel("time")
plt.ylabel("Amplitude") # 진폭
plt.show()
drawGraph()
t = np.arange(0, 5, 0.5)
t
r--: 빨간색 점선 형태의 그래프
cf) - : 직선 형태의 그래프
bs: 파란색 사각형 형태의 그래프
g>: 초록색 삼각형 형태의 그래프
plt.figure(figsize=(10, 6))
plt.plot(t, t, "r--") # red ----
plt.plot(t, t ** 2, "bs")
plt.plot(t, t ** 3, "g>")
plt.show()
# t = [0, 1, 2, 3, 4, 5, 6]
t = list(range(0, 7))
y = [1, 4, 5, 8, 9, 5, 3]
plt.figure(figsize=(10, 6))
plt.plot(
t,
y,
color="green",
linestyle="dashed",
marker="o",
markerfacecolor="blue",
markersize=15,
)
plt.xlim([-0.5, 6.5])
plt.ylim([0.5, 9.5])
plt.show()
색깔 변경
plt.figure(figsize=(10, 6))
plt.plot(
t,
y,
color="red", # 색깔 변경
linestyle="dashed",
marker="o",
markerfacecolor="blue",
markersize=15,
)
plt.xlim([-0.5, 6.5])
plt.ylim([0.5, 9.5])
plt.show()
그래프 모양 변경
plt.figure(figsize=(10, 6))
plt.plot(
t,
y,
color="red",
linestyle="--", # -(대쉬)로 넣어도 표현됨
marker="o",
markerfacecolor="blue",
markersize=15,
)
plt.xlim([-0.5, 6.5])
plt.ylim([0.5, 9.5])
plt.show()
직선 그래프 모양
plt.figure(figsize=(10, 6))
plt.plot(
t,
y,
color="red",
linestyle="-", # -(대쉬)를 하나만 넣으면 실선으로 표현됨
marker="o",
markerfacecolor="blue",
markersize=15,
)
plt.xlim([-0.5, 6.5])
plt.ylim([0.5, 9.5])
plt.show()
마크 색깔 변경
plt.figure(figsize=(10, 6))
plt.plot(
t,
y,
color="red",
linestyle="-",
marker="o",
markerfacecolor="green", # 마크 색깔 변경
markersize=15,
)
plt.xlim([-0.5, 6.5])
plt.ylim([0.5, 9.5])
plt.show()
마크 크기 변경
plt.figure(figsize=(10, 6))
plt.plot(
t,
y,
color="red",
linestyle="-",
marker="o",
markerfacecolor="green",
markersize=30, # 마크 크기 변경
)
plt.xlim([-0.5, 6.5])
plt.ylim([0.5, 9.5])
plt.show()
x축 범위 변경
plt.figure(figsize=(10, 6))
plt.plot(
t,
y,
color="red",
linestyle="-",
marker="o",
markerfacecolor="green",
markersize=30,
)
plt.xlim([-0.5, 10]) # x축 범위 변경
plt.ylim([0.5, 9.5])
plt.show()
그래프 함수에 담아두기
def drawGraph():
plt.figure(figsize=(10, 6))
plt.plot(
t,
y,
color="red",
linestyle="-",
marker="o",
markerfacecolor="green",
markersize=30, # 마크 크기 변경
)
plt.xlim([-0.5, 6.5])
plt.ylim([0.5, 9.5])
plt.show()
drawGraph()
scatter는 점을 뿌리듯이 그리는 그림
cf) plot으로 그릴 때는 선으로 이어진 형태의 그래프로 그려지는데, scatter로 그리면 점의 형태인 그래프로 그려진다.
t = np.array(range(0, 10))
y = np.array([9, 8, 7, 9, 8, 3, 2, 4, 3, 4])
plt.figure(figsize=(10, 6))
plt.scatter(t, y)
plt.show()
그래프를 함수로 표현하기
def drawGraph():
plt.figure(figsize=(20, 6)) # 가로축 크기 변경
plt.scatter(t, y)
plt.show()
drawGraph()
colormap 사용
colormap이란 마커 안을 채우는데, 내가 잡은 숫자의 색을 지정한다.
colormap = t
def drawGraph():
plt.figure(figsize=(20, 6))
plt.scatter(t, y, s=50, c=colormap, marker=">") # s는 마커의 사이즈
plt.colorbar()
plt.show()
drawGraph()
data_result.head()
data_result["인구수"].plot(kind="bar", figsize=(10, 6))
가로형태의 그래프, kind="barh"이용
data_result["인구수"].plot(kind="barh", figsize=(10, 6))
# kind="barh는 그래프 가로형태