티스토리 뷰

산포도 그래프 그리기

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)

⇨그래프 출력

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
글 보관함