티스토리 뷰
산포도 그래프 그리기
step 1. 데이터 생성과 그래프를 그리기 위한 라이브러리 불러오기, ggplot형태 그래프 이용
import maplotlib.pyplot as plt
import numpy as np
plt.style.use('ggplot')
step2. 샘플 데이터 생성
np.random.seed(2)
x = np.arange(1, 201)
#총 200개 데이터 생성
y = 2 * x * np.random.rand(200)
step3. 산포도 그래프 그리기
fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(x,y)
plt.show()
⇨그래프 결과
bokeh chart 활용하기
1. 다중 선 그래프 그리기
step1. bokeh 라이브러리 불러오기 및 csv 데이터 불러오기
from bokeh.plotting import figure, output_file, show
import pandas as pd
df = pd.read_csv('경로/파일이름.csv')
df
⇨데이터 프레임 출력
선수명 | 경기수 | 타수 | 득점 | 안타 |
홍길동 | 137 | 476 | 84 | 176 |
일지매 | 131 | 483 | 91 | 177 |
전우치 | 106 | 388 | 84 | 141 |
강감찬 | 125 | 498 | 103 | 173 |
step2. 데이터 지정
data1 = df['경기수']
data2 = df['타수']
data3 = df['득점']
name = df['선수명']
step3. 그래프 그리기
from bokeh.io import show, output_notebook
#그래프 크기 지정
p = figure(plot_width = 400, plot_height = 400)
output_notebook()
#다중선 그래프 그리기
p.multi_line([[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]], [data1, data2, data3], color = ['firebrick', 'navy', 'red'], alpha=[0.8, 0.8, 0.8], line_width=4)
show(p)
⇨그래프 출력
2. 막대그래프 그리기
step1, step2 위와 동일
step3. 막대 그래프 그리기
from bokeh.io import show, output_notebook
output_file('vbar.html')
p2 = figure(plot_width=400, plot_height=400)
p2.vbar(x=[1, 2, 3, 4], width=0.5, bottom=0, top=data1, color = 'firebrick')
show(p2)
⇨그래프 출력
'파이썬' 카테고리의 다른 글
결측치 처리 (0) | 2023.03.26 |
---|---|
파이썬 활용하여 자동으로 이메일 보내기 (0) | 2021.06.13 |
여러가지 통계분석 (0) | 2021.06.13 |
nltk 패키지 / 영문 텍스트 분석하기 / 워드클라우드 (0) | 2021.06.13 |
(MAC OS) matplotlib를 이용한 그래프 그리기 (0) | 2021.06.13 |