๐Ÿ“Š ๊ฐœ๊ฐ•ํ–ˆ๋”๋‹ˆ ์‚ผํ•™๋…„์ด๋ผ๊ณ  ํ•™๊ธฐ ์ดˆ๋ถ€ํ„ฐ ์•„์ฃผ ๋ฐ”์ฉ๋‹ˆ๋‹ค!! ์ „๊ณต๊ณผ๋ชฉ์˜ ๋‚œ์ด๋„๊ฐ€ ์ •๋ง 2ํ•™๋…„๋•Œ์™€๋Š” ๋น„๊ตํ•  ์ˆ˜ ์—†๊ฒŒ ์˜ฌ๋ผ๊ฐ”์Šต๋‹ˆ๋‹ค(๋ฌผ๋ก  2๋…„๋™์•ˆ ๊ตณ์€ ์ œ ๋จธ๋ฆฌ๋„ ํ•œ๋ชซํ•˜๊ฒ ์ง€๋งŒโ€ฆ๐Ÿ™„). ๋ฐฐ์šด ๊ฑด ๊ทธ๋‚ ๊ทธ๋‚  ๋ณต์Šตํ•˜๋ ค๋Š” ์Šต๊ด€์„ ๋“ค์ด๊ณ  ์žˆ๋Š”๋ฐ ๊ฐœ๊ฐ• ์ฒซ์ฃผ๋ถ€ํ„ฐ ์•„์ฃผ ์•„์Šฌ์•„์Šฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜๋„ ์งฌ์งฌ์ด ๊ณต๋ถ€ํ•œ ๋‚ด์šฉ๋“ค์€ ์ตœ๋Œ€ํ•œ ๋ธ”๋กœ๊ทธ์— ์˜ฌ๋ ค๋ณผ ์ƒ๊ฐ์ž…๋‹ˆ๋‹ค!!

๐Ÿ“Š ์˜ค๋Š˜์€ plotly์—์„œ for loop๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฒ•์„ ์•Œ์•„๋ด…์‹œ๋‹ค.

๐Ÿ“Š ๋‹จ์ˆœํžˆ ์—ฌ๋Ÿฌ๊ฐœ์˜ ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ฆฌ๋Š” ๊ฒƒ์ด ์•„๋‹Œ ๊ฐ๊ฐ์˜ ๋ฐ์ดํ„ฐ ์š”์†Œ๋“ค์„ ๋–ผ์–ด์™€์„œ ๊ทธ๋ฆด๋•Œ, ์ผ์ผ์ด figure๋“ค์„ ์ •์˜ํ•ด์ค˜์•ผํ•˜๋Š” ๋ฒˆ๊ฑฐ๋กœ์›€์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์–ด๋–จ๊ฒŒ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์„๊นŒ ๊ณ ๋ฏผํ•˜๋‹ค๊ฐ€ for๋ฌธ์„ ์‚ฌ์šฉํ•˜๊ธฐ๋กœ ๊ฒฐ์ •ํ–ˆ๋Š”๋ฐ, ์ƒ๊ฐ๋ณด๋‹ค ์–ด๋ ค์› ๋˜ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํ•œ๋ฒˆ ์‚ดํŽด๋ด…์‹œ๋‹ค๐Ÿ™‚!!


3.1 for๋ฌธ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์€ plotly

๐Ÿ“Š ๋จผ์ € ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„์„ ํ•˜๋‚˜ ์ •์˜ํ•ด๋ด…์‹œ๋‹ค.

#pandas ์ž„ํฌํŠธ
import pandas as pd

#plotly ์ž„ํฌํŠธ
import plotly.graph_objects as go
import plotly.offline as pyo
pyo.init_notebook_mode()
score_df = pd.DataFrame({'test' : ['score1','score2','score3','score4','score5','score6'],
                         'A' : [95, 100, 90, 88, 92, 94],
                         'B' : [87, 92, 95, 93, 88, 86],
                         'C' : [92, 92, 86, 95, 90, 84],
                         'D' : [78, 80, 82, 86, 80, 82],
                         'E' : [80, 76, 84, 80, 78, 84]})
score_df = score_df.set_index('test')
score_df
>>
        A	B	C	D	E
test					
score1	95	87	92	78	80
score2	100	92	92	80	76
score3	90	95	86	82	84
score4	88	93	95	86	80
score5	92	88	90	80	78
score6	94	86	84	82	84

ํ•™์ƒ A,B,C,D,E ์˜ 6๊ฐœ์˜ ์‹œํ—˜ ์„ฑ์ ์„ ๋‚˜ํƒ€๋‚ธ ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„์ž…๋‹ˆ๋‹ค.

๐Ÿ“Š ์ผ๋‹จ for ๋ฌธ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ์ƒํƒœ์—์„œ ์‹œ๊ฐํ™”๋ฅผ ์ง„ํ–‰ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

fig = go.Figure()
fig.add_trace(
    go.Scatter(
        x = score_df.index, y = score_df['A'], name = 'A'))

fig.add_trace(
    go.Scatter(
        x = score_df.index, y = score_df['B'], name = 'B'))

fig.add_trace(
    go.Scatter(
        x = score_df.index, y = score_df['C'], name = 'C'))

fig.add_trace(
    go.Scatter(
        x = score_df.index, y = score_df['D'], name = 'D'))

fig.add_trace(
    go.Scatter(
        x = score_df.index, y = score_df['E'], name = 'E'))

fig.show()


๐Ÿ“Š for๋ฌธ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์—๋Š” fig . add_trace( ) ๋ฅผ ๋‹ค์„ฏ๋ฒˆ ๋ชจ๋‘ ํ˜ธ์ถœํ•ด์„œ ๊ฐ ํ•™์ƒ์˜ ์‹œํ—˜ ์„ฑ์ ๋“ค์„ ๊ทธ๋ฆฝ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋ฐ์ดํ„ฐ์˜ ์ˆ˜๊ฐ€ ๋งŽ์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋Š” ๊ตณ์ด ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ํฐ ๋ถ€๋‹ด์ด ์—†์ง€๋งŒ, ๋งŽ์€ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃจ๋ ค๋ฉด ์œ„์˜ ๋ฐฉ๋ฒ•๋งŒ์œผ๋กœ๋Š” ์ข€ ๋ฌด๋ฆฌ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ์ด ํฌ์ŠคํŒ…์„ ์ž‘์„ฑํ•œ ์ด์œ ์ด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค!!

๊ทธ๋Ÿฌ๋ฉด ์ด๋ฒˆ์—๋Š” for ๋ฌธ์„ ์‚ฌ์šฉํ•ด๋ด…์‹œ๋‹ค.


3.2 for๋ฌธ์œผ๋กœ plotly ๊ทธ๋ ค๋ณด๊ธฐ

๐Ÿ“Š ๋ฐ์ดํ„ฐ๋Š” ์œ„์—์„œ ๋งŒ๋“  score_df ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„์„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

score_df
>>
        A	B	C	D	E
test					
score1	95	87	92	78	80
score2	100	92	92	80	76
score3	90	95	86	82	84
score4	88	93	95	86	80
score5	92	88	90	80	78
score6	94	86	84	82	84

๐Ÿ“Š ์ด๋ฒˆ์—๋Š” for ๋ฌธ์„ ๊ฐ€์ง€๊ณ  ์‹œ๊ฐํ™”ํ•ด๋ด…์‹œ๋‹ค!!

col = len(score_df.columns)

fig = go.Figure()
for i in range(col):
    fig.add_trace(
        go.Scatter(
            x = score_df.index, y = score_df[score_df.columns[i]], name = score_df.columns[i]))

fig.show()

๐Ÿ“Š ๋ณ€์ˆ˜ col ์— ๋ฐ์ดํ„ฐ์˜ ์—ด ๊ฐœ์ˆ˜๋ฅผ ๋„ฃ์–ด์ฃผ๊ณ , ๊ทธ ๊ฐœ์ˆ˜๋งŒํผ for๋ฌธ ์„ ๋Œ๋ฆฌ๋ฉด์„œ fig . add_trace( ) ๋ฅผ ํ˜ธ์ถœํ•ด์ค๋‹ˆ๋‹ค. ๊ทธ ๋’ค๋ถ€ํ„ฐ๋Š” ๋ฐ์ดํ„ฐ์˜ ์—ด์— ์ธ๋ฑ์Šค๋กœ ์ ‘๊ทผํ•ด์คŒ์œผ๋กœ์จ x์ถ•๊ณผ y์ถ•์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ •ํ•ด์ค๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๊ณ  ๋‚˜๋ฉด ์‹œ๊ฐํ™”๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค.


for ๋ฌธ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค.


๐Ÿ“Š ๋‹น์—ฐํžˆ for ๋ฌธ์„ ์‚ฌ์šฉํ•˜๋ฉด ์ผ์ผ์ด ํ˜ธ์ถœํ•ด์ฃผ๋Š” ๊ฒƒ๋ณด๋‹ค ์ฝ”๋“œ๋ฅผ ์งœ๋Š” ์‹œ๊ฐ„์  ์ธก๋ฉด์—์„œ ํšจ์œจ์„ ์–ป์„ ์ˆ˜ ์žˆ์ง€๋งŒ, ์•ž์„œ ๋‹ค๋ฅธ ํฌ์ŠคํŒ…์—์„œ๋„ ์–ธ๊ธ‰ํ–ˆ๋“ฏ์ด ์ด ๋ฐฉ๋ฒ•์€ ์˜์™ธ๋กœ ๋ณ€์ˆ˜๊ฐ€ ๋งŽ์ด ๋ฐœ์ƒํ•˜๋Š” ์ž‘์—…์ž…๋‹ˆ๋‹ค(๋‹น์—ฐํžˆ ๋‚ด๊ฐ€ ์ž˜ ๋ชจ๋ฅด๋Š” ๋ถ€๋ถ„์ผ ์ˆ˜๋„ ์žˆ๋‹ค. ์—ฌ๋Ÿฌ๊ฐ€์ง€ ํ˜•ํƒœ๋กœ ์‹œ๋„ํ•ด๋ณด๋‹ค๊ฐ€ ์„ฑ๊ณตํ•˜๋ฉด ์ง !!ํ•˜๊ณ  ํฌ์ŠคํŒ…ํ•  ์ƒ๊ฐ์ž…๋‹ˆ๋‹คโ€ฆ๐Ÿ˜…)

๐Ÿ“Š ๋ฐ์ดํ„ฐ๋ถ„์„ ๊ณต๋ถ€๋ฅผ ํ•˜๋‹ค๋ณด๋ฉด ๋Œ€๋†“๊ณ  ํŒŒ์ด์ฌ ๊ณต๋ถ€๋ฅผ ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋”๋ผ๋„ ์ด๋Ÿฐ์ €๋Ÿฐ ์ƒํ™ฉ ์†์—์„œ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ์‘์šฉํ•˜๋Š” ๊ฒŒ ์กฐ๊ธˆ์”ฉ ์ต์ˆ™ํ•ด์ง€๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์ €๋ ‡๊ฒŒ ๊ณต๋ถ€ํ•˜๋ฉด์„œ ์‹ค๋ ฅ์ด ๋Š˜์—ˆ์œผ๋ฉด ์ข‹๊ฒ ์Šต๋‹ˆ๋‹ค๐Ÿ™Œ.


Leave a comment