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

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

ํฌ๋ฆฌ๋„ฅ์Šค์„๋ผ์ด์Šค 2023. 1. 29. 21:18

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

 

์ด๋ฒˆ์—๋Š” ๊ฐ„๋‹จํ•˜๊ฒŒ pyecharts ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ํŒŒ์ด ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ ค๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

์‚ฌ์šฉํ•  ๋ฐ์ดํ„ฐ๋Š” ๊ฐ€๋ณ๊ฒŒ ํ†ต์‹ ์‚ฌ๋ณ„ ์ ์œ ์œจ๋กœ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

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

# ํ†ต์‹ ์‚ฌ 
x = ["kt", "skt", "lg", "๊ธฐํƒ€"]
# ์ ์œ ์œจ
y = [35, 25, 25, 15]

ํŒŒ์ด ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ฆฌ๊ธฐ ์ „์— ๋ฐ์ดํ„ฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ๋งŒ๋“ค์–ด ์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

# ํŒŒ์ด ๊ทธ๋ž˜ํ”„์— ์‚ฌ์šฉ๋  ๋ฐ์ดํ„ฐ์…‹
z = [list(i) for i in zip(x, y)]

๋ฐ์ดํ„ฐ์…‹ ํ˜•ํƒœ

๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด ํŒŒ์ด ๊ทธ๋ž˜ํ”„๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

p = (
    Pie()
    .add("", z,)  # ๋ฐ์ดํ„ฐ์…‹
    .set_global_opts(title_opts=opts.TitleOpts(title="ํŒŒ์ด ๊ทธ๋ž˜ํ”„")) # ์ œ๋ชฉ
    .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}")) # ๋ ˆ์ด๋ธ” ํฌ๋งท
    .render_notebook()
)

p

๊ธฐ๋ณธ ํŒŒ์ด ๊ทธ๋ž˜ํ”„

ํŒŒ์ด ๊ทธ๋ž˜ํ”„๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค๋ฉด ํฌ๊ธฐ๋‚˜ ๋ฒ”๋ก€๋ฅผ ์ˆ˜์ •ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

p = (
    Pie()
    .add("", z, radius=["10%", "70%"])  # ๊ฐ€์šด๋ฐ ํฐ ์›์˜ ํฌ๊ธฐ, ์ „์ฒด ๊ทธ๋ž˜ํ”„์˜ ํฌ๊ธฐ
    .set_global_opts(title_opts=opts.TitleOpts(title="ํŒŒ์ด ๊ทธ๋ž˜ํ”„"))
    .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
    .render_notebook()
)

p

ํŒŒ์ด ๊ทธ๋ž˜ํ”„ ํฌ๊ธฐ ์กฐ์ ˆ

ํŒŒ์ด ๊ทธ๋ž˜ํ”„ ์•ˆ์— ์žˆ๋Š” ๋ฒ”๋ก€๋„ ์กฐ์ •์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

 

opts.LegendOpts ํ•จ์ˆ˜์— orient๋ฅผ vertical๋กœ ์ง€์ •ํ•˜๋ฉด ๊ธฐ์กด์— ์ค‘์•™ ์ƒ๋‹จ์— ๊ฐ€๋กœ๋กœ ์žˆ๋˜ ๋ฒ”๋ก€๊ฐ€ ์„ธ๋กœ๋กœ ์กฐ์ •๋ฉ๋‹ˆ๋‹ค.

 

ํ•˜์ง€๋งŒ ์ด๋ ‡๊ฒŒ ๋˜๋ฉด ํŒŒ์ด ๊ทธ๋ž˜ํ”„์™€ ๋ฒ”๋ก€๊ฐ€ ๊ฒน์น˜๊ธฐ ๋•Œ๋ฌธ์— pos_top๊ณผ pos_left๋กœ ์œ„์น˜๋ฅผ ์กฐ์ •ํ•ด ์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

(pos_top๋Š” ์œ„/์•„๋ž˜ ์œ„์น˜๋ฅผ, pos_left๋Š” ์™ผ์ชฝ/์˜ค๋ฅธ์ชฝ ์œ„์น˜๋ฅผ ๋‹ด๋‹นํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.)

 

p = (
    Pie()
    .add("", z, radius=["50%", "80%"])  # ๊ฐ€์šด๋ฐ ๋„๋„›์˜ ํฌ๊ธฐ, ์ „์ฒด ๋„ํ˜•์˜ ํฌ๊ธฐ
    .set_global_opts(title_opts=opts.TitleOpts(title="ํŒŒ์ด ๊ทธ๋ž˜ํ”„"),
                    legend_opts=opts.LegendOpts(orient="vertical", pos_top="10%", pos_left="10%")) # ๋ฒ”๋ก€ ์กฐ์ •
    .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
    .render_notebook()
)

p

๋ฒ”๋ก€ ์ˆ˜์ •

๋ฒ”๋ก€์— ์ด์–ด์„œ ํŒŒ์ด ๋ชจํ˜•๋„ ์ด๋™์‹œํ‚ฌ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

(center ์˜ต์…˜์˜ ๋ฆฌ์ŠคํŠธ ์ฒซ ๋ฒˆ์งธ ๊ฐ’์ด ์ปค์งˆ์ˆ˜๋ก ์˜ค๋ฅธ์ชฝ์œผ๋กœ, ๋‘ ๋ฒˆ์งธ ๊ฐ’์ด ์ปค์งˆ์ˆ˜๋ก ์•„๋ž˜๋กœ ์กฐ์ •๋ฉ๋‹ˆ๋‹ค.)

p = (
    Pie()
    .add("", z, radius=["50%", "80%"], center=["70%", "50%"])   # ์™ผ์ชฝ/์˜ค๋ฅธ์ชฝ, ์œ„/์•„๋ž˜
    .set_global_opts(title_opts=opts.TitleOpts(title="ํŒŒ์ด ๊ทธ๋ž˜ํ”„"),
                    legend_opts=opts.LegendOpts(orient="vertical", pos_top="10%", pos_left="10%")) # ๋ฒ”๋ก€ ์กฐ์ •
    .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
    .render_notebook()
)

p

ํŒŒ์ด ๋ชจํ˜• ์กฐ์ •

ํŒŒ์ด ๊ทธ๋ž˜ํ”„ ์ƒ‰์ƒ์ด ๋ณ„๋กœ๋ผ๊ณ  ์ƒ๊ฐ๋˜์‹ ๋‹ค๋ฉด ์›ํ•˜๋Š” ์ƒ‰์œผ๋กœ ๋ณ€๊ฒฝ๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

p = (
    Pie()
    .add("", z, radius=["50%", "80%"])  # ๊ฐ€์šด๋ฐ ๋„๋„›์˜ ํฌ๊ธฐ, ์ „์ฒด ๋„ํ˜•์˜ ํฌ๊ธฐ
    .set_global_opts(title_opts=opts.TitleOpts(title="ํŒŒ์ด ๊ทธ๋ž˜ํ”„"),
                    legend_opts=opts.LegendOpts(orient="vertical", pos_top="10%", pos_left="10%")) # ๋ฒ”๋ก€ ์กฐ์ •
    .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
    .set_colors(["gold", "steelblue", "limegreen", "plum"]) # ์นดํ…Œ๊ณ ๋ฆฌ๋ณ„ ์ƒ‰์ƒ์„ ๋ฆฌ์ŠคํŠธ๋กœ ์ง€์ •
    .render_notebook()
)

p

์ƒ‰์ƒ ์ง€์ •

์กฐ๊ธˆ ์งง์ง€๋งŒ ๊ฐ„๋‹จํ•œ๊ฒŒ pyecharts ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ํŒŒ์ด ๊ทธ๋ž˜ํ”„๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์ •๋ฆฌํ–ˆ์Šต๋‹ˆ๋‹ค.

 

๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.