๋ฐ์ดํ„ฐ ์‹œ๊ฐํ™”

pyecharts๋กœ ์„  ๊ทธ๋ž˜ํ”„ ๊ทธ๋ฆฌ๊ธฐ

ํฌ๋ฆฌ๋„ฅ์Šค์„๋ผ์ด์Šค 2023. 1. 27. 14:27

์•ˆ๋…•ํ•˜์„ธ์š” ํฌ๋ฆฌ๋„ฅ์Šค์„๋ผ์Šค์ž…๋‹ˆ๋‹ค.

 

์ €๋ฒˆ์—๋Š” pyecharts๋ฅผ ์ด์šฉํ•ด ๋ง‰๋Œ€ ๊ทธ๋ž˜ํ”„๋ฅผ ๋งŒ๋“œ๋Š” ๋‚ด์šฉ์„ ๋‹ค๋ค˜์Šต๋‹ˆ๋‹ค.

 

์ด๋ฒˆ์—๋Š” ๋ง‰๋Œ€ ๊ทธ๋ž˜ํ”„๋งŒํผ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ์„  ๊ทธ๋ž˜ํ”„์— ๋Œ€ํ•ด ๋‹ค๋ค„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

๋จผ์ € ์ œ๊ฐ€ ์‚ฌ์šฉํ•  ๋ฐ์ดํ„ฐ๋Š” 2013๋…„๋ถ€ํ„ฐ 2023๋…„๊นŒ์ง€ ์ถ•๊ตฌ์„ ์ˆ˜ ๋ฆฌ์˜ค๋„ฌ ๋ฉ”์‹œ์˜ ๋ฆฌ๊ทธ ๋“์  ๊ธฐ๋ก๊ณผ ๋„์›€ ๊ธฐ๋ก์ž…๋‹ˆ๋‹ค.

(์œ„ํ‚ค๋ฐฑ๊ณผ์—์„œ ์ฐธ๊ณ ํ–ˆ์Šต๋‹ˆ๋‹ค.)

# ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
import pyecharts.options as opts
from pyecharts.charts import Line

# ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ
years = ['2013', '2014', '2015', '2016', '2017', '2018', '2019', '2020', '2021', '2022', '2023']
goals = [46, 28, 43, 26, 37, 34, 36, 25, 30, 6, 7]
assists = [12, 11, 18, 16, 9, 12, 13, 21, 9, 0, 0]

# ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„์œผ๋กœ ์ƒ์„ฑ
messi = pd.DataFrame({"์—ฐ๋„": years, "๊ณจ": goals, "๋„์›€": assists, "๊ฒฝ๊ธฐ": games})

messi

๋ฆฌ์˜ค๋„ฌ ๋ฉ”์‹œ์˜ ๋ฆฌ๊ทธ ๊ธฐ๋ก์„ ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„์œผ๋กœ ์‚ดํŽด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

๋ฆฌ์˜ค๋„ฌ ๋ฉ”์‹œ ๋ฆฌ๊ทธ ๊ธฐ๋ก

ํ•ด๋‹น ๋ฐ์ดํ„ฐ ์ค‘ ๋“์  ๊ธฐ๋ก์— ๋Œ€ํ•œ ์„  ๊ทธ๋ž˜ํ”„๋ฅผ ๊ฐ„๋‹จํžˆ ๊ทธ๋ ค๋ณด๋ฉด ๋‹ค์Œ ์ด๋ฏธ์ง€์ฒ˜๋Ÿผ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.

(์ €๋Š” x์ถ•์— intํ˜• ๊ฐ’์„ ๋„ฃ์—ˆ๋”๋‹ˆ ๊ทธ๋ž˜ํ”„๊ฐ€ ๊ทธ๋ ค์ง€์ง€ ์•Š์•„์„œ ๋ฌธ์žํ˜•์œผ๋กœ ๋œ years ๋ฆฌ์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.) 

c = (
    Line()
    .add_xaxis(years)         # x์ถ•
    .add_yaxis("๋“์ ", goals)  # y์ถ•  
    .set_global_opts(title_opts=opts.TitleOpts(title="๋ฆฌ์˜ค๋„ฌ ๋ฉ”์‹œ ์—ญ๋Œ€ ๊ธฐ๋ก"))  # ์ œ๋ชฉ
    .render_notebook()        # ์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ์— ์ถœ๋ ฅ
)

c

๋“์  ๊ธฐ๋ก

์ด๋ฒˆ์—๋Š” ๋“์ ๊ณผ ๋„์›€์„ ํ•˜๋‚˜์˜ ๊ทธ๋ž˜ํ”„ ์•ˆ์— ๊ทธ๋ ค๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

c = (
    Line()
    .add_xaxis(years)           # x์ถ•
    .add_yaxis("๋“์ ", goals)    # y์ถ• 1
    .add_yaxis("๋„์›€", assists)  # y์ถ• 2
    .set_global_opts(title_opts=opts.TitleOpts(title="๋ฆฌ์˜ค๋„ฌ ๋ฉ”์‹œ ์—ญ๋Œ€ ๊ธฐ๋ก"))  # ์ œ๋ชฉ
    .render_notebook()        # ์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ์— ์ถœ๋ ฅ
)

c

๋ง‰๋Œ€ ๊ทธ๋ž˜ํ”„์—์„œ ์„ค๋ช…๋“œ๋ฆฐ ๊ฒƒ์ฒ˜๋Ÿผ ์ค‘์•™ ์ƒ๋‹จ์˜ ๋“์ /๋„์›€์„ ํด๋ฆญํ•˜๋ฉด ํ•ด๋‹น ๋ณ€์ˆ˜๋ฅผ ์„ ํƒ/ํ•ด์ฒดํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

๋“์ ๊ณผ ๋„์›€

์ €๋Š” ์„  ๊ทธ๋ž˜ํ”„๊ฐ€ ์ข€ ๋ฐ‹๋ฐ‹ํ•˜๊ฒŒ ๋Š๊ปด์ ธ ์  ๋ชจ์–‘์ด๋‚˜ ์ƒ‰์ƒ์„ ๋ณ€๊ฒฝํ•ด ๋ดค์Šต๋‹ˆ๋‹ค.

c = (
    Line()
    .add_xaxis(years)           # x์ถ•
    .add_yaxis("๋“์ ", goals, symbol="diamond", symbol_size=10, color="tomato")  # ์  ๋ชจ์–‘๊ณผ ์ƒ‰์ƒ ๋ณ€๊ฒฝ 1
    .add_yaxis("๋„์›€", assists, symbol="square", symbol_size=8, color="orange")  # ์  ๋ชจ์–‘๊ณผ ์ƒ‰์ƒ ๋ณ€๊ฒฝ 2
    .set_global_opts(title_opts=opts.TitleOpts(title="๋ฆฌ์˜ค๋„ฌ ๋ฉ”์‹œ ์—ญ๋Œ€ ๊ธฐ๋ก"))  # ์ œ๋ชฉ
    .render_notebook()        # ์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ์— ์ถœ๋ ฅ
)

c

์  ๋ชจ์–‘๊ณผ ์ƒ‰์ƒ ๋ณ€๊ฒฝ

๊ทธ๋ž˜๋„ ๊ทธ๋ž˜ํ”„๊ฐ€ ๋ฐ‹๋ฐ‹ํ•˜๊ฒŒ ๋Š๊ปด์ ธ์„œ ์„  ๊ทธ๋ž˜ํ”„์˜ ์Šคํƒ€์ผ์„ ๋‹ค๋ฅธ ๋Š๋‚Œ์œผ๋กœ ๋ณ€๊ฒฝํ–ˆ์Šต๋‹ˆ๋‹ค.

c = (
    Line()
    .add_xaxis(years)           # x์ถ•
    .add_yaxis("๋“์ ", goals, symbol="diamond", symbol_size=10, color="tomato", is_smooth=True)  # ๋ถ€๋“œ๋Ÿฝ๊ฒŒ
    .add_yaxis("๋„์›€", assists, symbol="square", symbol_size=8, color="orange", is_smooth=True)  # ๋ถ€๋“œ๋Ÿฝ๊ฒŒ
    .set_global_opts(title_opts=opts.TitleOpts(title="๋ฆฌ์˜ค๋„ฌ ๋ฉ”์‹œ ์—ญ๋Œ€ ๊ธฐ๋ก"))  # ์ œ๋ชฉ
    .render_notebook()        # ์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ์— ์ถœ๋ ฅ
)

c

๊ทธ๋ž˜ํ”„๋ฅผ ๋ถ€๋“œ๋Ÿฝ๊ฒŒ ๋ณ€๊ฒฝ

is_smooth = True ๋Œ€์‹  is_step = True๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ณ„๋‹จ ๋ชจ์–‘์œผ๋กœ ๋ณ€๊ฒฝ๋ฉ๋‹ˆ๋‹ค.

๊ทธ๋ž˜ํ”„๋ฅผ ๊ณ„๋‹จํ˜•์œผ๋กœ ๋ณ€๊ฒฝ

๋งŒ๋“  ์„  ๊ทธ๋ž˜ํ”„์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ‰๊ท ์„ ์ถ”๊ฐ€ํ•ด ์ข€ ๋” ์˜๋ฏธ๋ฅผ ๋ถ€์—ฌํ•ด ์ค„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

c = (
    Line()
    .add_xaxis(years)           # x์ถ•
    .add_yaxis("๋“์ ", goals, symbol="diamond", symbol_size=10, color="tomato", is_smooth=True,
              markline_opts=opts.MarkLineOpts(data=[opts.MarkLineItem(type_="average")]))  # ํ‰๊ท  ์ถ”๊ฐ€
    .add_yaxis("๋„์›€", assists, symbol="square", symbol_size=8, color="orange", is_smooth=True,
              markline_opts=opts.MarkLineOpts(data=[opts.MarkLineItem(type_="average")]))  # ํ‰๊ท  ์ถ”๊ฐ€
    .set_global_opts(title_opts=opts.TitleOpts(title="๋ฆฌ์˜ค๋„ฌ ๋ฉ”์‹œ ์—ญ๋Œ€ ๊ธฐ๋ก"))  # ์ œ๋ชฉ
    .render_notebook()        # ์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ์— ์ถœ๋ ฅ
)

c

ํ‰๊ท ์— ๋Œ€ํ•œ ์ •๋ณด ๋„ฃ๊ธฐ

๋งŒ์•ฝ ์„  ๊ทธ๋ž˜ํ”„์—์„œ ์ตœ๋Œ“๊ฐ’์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๋ณด์—ฌ ์ฃผ๊ณ  ์‹ถ๋‹ค๋ฉด ์ตœ๋Œ“๊ฐ’ ๋งˆ์ปค๋ฅผ ์ถ”๊ฐ€ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

c = (
    Line()
    .add_xaxis(years)           # x์ถ•
    .add_yaxis("๋“์ ", goals, symbol="diamond", symbol_size=10, color="tomato", is_smooth=True,
              markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_="max")]))  # ๋งˆ์ปค ์ถ”๊ฐ€
    .add_yaxis("๋„์›€", assists, symbol="square", symbol_size=8, color="orange", is_smooth=True,
              markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_="max")]))  # ๋งˆ์ปค ์ถ”๊ฐ€
    .set_global_opts(title_opts=opts.TitleOpts(title="๋ฆฌ์˜ค๋„ฌ ๋ฉ”์‹œ ์—ญ๋Œ€ ๊ธฐ๋ก"))  # ์ œ๋ชฉ
    .render_notebook()        # ์ฃผํ”ผํ„ฐ ๋…ธํŠธ๋ถ์— ์ถœ๋ ฅ
)

c

๋งˆ์ปค ์ถ”๊ฐ€

์˜ค๋Š˜ ์ค€๋น„ํ•ด ๋ณธ ๋‚ด์šฉ์€ ์—ฌ๊ธฐ๊นŒ์ง€ ์ž…๋‹ˆ๋‹ค.

 

์ €๋Š” ๋‚˜๋ฆ„ ๋ฐ์ดํ„ฐ ์‹œ๊ฐํ™”์— ํฅ๋ฏธ๊ฐ€ ์žˆ์–ด์„œ, ์ด๋ ‡๊ฒŒ ๋ฐ˜์‘ํ˜•์œผ๋กœ ๊ทธ๋ž˜ํ”„๋ฅผ ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด ๋งค๋ ฅ์ ์ด์—ˆ์Šต๋‹ˆ๋‹ค.