π 2. νμ±μ μ¬λ¬κ°μ§ μ 보λ₯Ό μμ보μ (3)
π μ€λλ§μ λ€λ£¨λ νμ± λ°μ΄ν°λ€. μ¬μ€ μ΄λ κ² νλ² λ λ€λ£°λ§νΌ μ€μν λ°μ΄ν°λ μλλΌκ³ μκ°νμλλ°, ν리λμ€ν¬λ‘ λμμλ λμ μμ¬μ΄ μ μ μκ°ν΄λ³΄λ μκ°νλ§ ν΄λκ³ κ·Έ λ°μ΄ν°κ° μλ―Ένλ λ°λ₯Ό μ νν λΆμν΄λ³Έ μ μ μμλ€λ κ²μ΄ λ μ¬λλ€. κ·Έ μ΄μ λ‘ μ΄λ κ² λλ¬μ λ μ μ λ€λ€λ λ°μ΄ν°λ₯Ό κ°μ Έμλ΄€λ€π.
π μ΄λ² ν¬μ€ν μμλ plotly μκ°νμμ forλ¬Έμ μ¬μ©νλ λ°©λ²μ μκ°νλ€.
1. forλ¬Έμ μ¬μ©νμ§ μλ κ²½μ°
π λ¨Όμ μ΄μ ν¬μ€ν μμ μκ°νν κ·Έλνλ₯Ό κ°μ Έμ€λλ‘ νμ. νμ± λ°μ΄ν°μμ λ€λ₯Έ λΆμμ ν΄μΌκ² λ€κ³ μκ°νκ² ν κ°μ₯ ν° μ΄μ μ΄λ€.
π μμ λ§λκ·Έλνλ₯Ό 보면 μκ² μ§λ§ Radial Velocity μ Transit μ μ μΈν λλ¨Έμ§ κ΄μΈ‘λ°©λ²μ κ·Έλ κ² λμ λλ κ΄μΈ‘ μλ₯Ό 보μ΄μ§λ λͺ»νλ€(λ¬Όλ‘ νμ±μ λ°κ²¬ νλνλκ° μ°μ£Ό μμ¬μ μμ΄ λͺΉμ ν° μ μ μ΄μ§λ§ μ΄ λ°μ΄ν°μμλ λ°°μ νλ€). λ¨μ§ μ λκ°μ κ΄μΈ‘λ²λ§μ΄ λ§μ κ°μλ₯Ό κ°μ§κ³ μμμ΄ λ°μ΄ν°λΆμμμ μλ―Έκ° ν° κ²μ μλκΈ° λλ¬Έμ, λ€λ₯Έ λΆμλ²μ νμλ‘ νλ€. κ·Έλμ μκ°ν λ°©λ²μ΄ λ λλ³λ‘ κ΄μΈ‘λ²μ μΆμ΄λ₯Ό μμ보λ κ²μ΄λ€. κ·Έλ¬λ©΄ μΈμμ νλ¦μ λ°λΌ λκ°μ λνλ΄λ κ΄μΈ‘λ²κ³Ό μλ‘κ² λ μ€λ₯΄λ κ΄μΈ‘λ²μ μμ보기μ μ’μ κ²μ΄λΌ μκ°νκΈ° λλ¬Έμ΄λ€. κ·ΈλΌ μ΄μ μμν΄λ³΄μπββοΈ.
μΌλ¨ λ°μ΄ν°λ₯Ό λΆλ¬μ€μ.
import pandas as pd
import seaborn as sns
planets = sns.load_dataset('planets')
planets.head()
>>
method number orbital_period mass distance year
0 Radial Velocity 1 269.300 7.10 77.40 2006
1 Radial Velocity 1 874.774 2.21 56.95 2008
2 Radial Velocity 1 763.000 2.60 19.84 2011
3 Radial Velocity 1 326.030 19.40 110.62 2007
4 Radial Velocity 1 516.220 10.50 119.47 2009
μ°λ¦¬λ 2κ°μ ννλ‘ λ°μ΄ν°λ₯Ό μ²λ¦¬ν κ²μ΄κΈ° λλ¬Έμ λ°μ΄ν°νλ μμ μ΄λ¦μ _1 / _2 λ‘ λͺ λͺ νκ² λ€.
planets_year_method_1 = planets.groupby(['method', 'year']).agg({'number':'sum'})
planets_year_method_1 = planets_year_method_1.reset_index()
planets_year_method_1
>>
method year number
0 Astrometry 2010 1
1 Astrometry 2013 1
2 Eclipse Timing Variations 2008 4
3 Eclipse Timing Variations 2009 1
4 Eclipse Timing Variations 2010 4
... ... ... ...
64 Transit 2014 93
65 Transit Timing Variations 2011 2
66 Transit Timing Variations 2012 2
67 Transit Timing Variations 2013 2
68 Transit Timing Variations 2014 3
69 rows Γ 3 columns
method, year, number μ΄μ κ°μ§λ planets_year_method_1 λ°μ΄ν°νλ μμ΄ μμ±λμλ€π.
λ¨Όμ Bar κ·Έλνλ‘ μκ°νν΄λ³΄μ.
import plotly.graph_objects as go
import plotly.offline as pyo
pyo.init_notebook_mode()
fig = go.Figure()
fig.add_trace(
go.Bar(
x = planets_year_method_1[planets_year_method_1['method']=='Imaging']['year'],
y = planets_year_method_1[planets_year_method_1['method']=='Imaging']['number'], name = 'Imaging'))
fig.add_trace(
go.Bar(
x = planets_year_method_1[planets_year_method_1['method']=='Radial Velocity']['year'],
y = planets_year_method_1[planets_year_method_1['method']=='Radial Velocity']['number'], name = 'Radial Velocity'))
fig.add_trace(
go.Bar(
x = planets_year_method_1[planets_year_method_1['method']=='Eclipse Timing Variations']['year'],
y = planets_year_method_1[planets_year_method_1['method']=='Eclipse Timing Variations']['number'], name = 'Eclipse Timing Variations'))
fig.add_trace(
go.Bar(
x = planets_year_method_1[planets_year_method_1['method']=='Transit']['year'],
y = planets_year_method_1[planets_year_method_1['method']=='Transit']['number'], name = 'Transit'))
fig.add_trace(
go.Bar(
x = planets_year_method_1[planets_year_method_1['method']=='Microlensing']['year'],
y = planets_year_method_1[planets_year_method_1['method']=='Microlensing']['number'], name = 'Microlensing'))
fig.show()
μ°μ forλ¬Έμ μ¬μ©νμ§ μμκΈ° λλ¬Έμ λ§μ κ΄μΈ‘μλ₯Ό κ°μ§λ 5κ°μ λ°μ΄ν°λ§ μκ°νν΄λ΄€λ€. κ²°κ³Όλ μλμ κ°λ€.
λ§λκ·Έλνμ λ무 λ§μ λ΄μ©μ΄ λ€μ΄ μμ΄ νλμ μμ보기 μ΄λ ΅λ€. μ 체μ μΈ μΆμ΄λ₯Ό μν΄ line κ·Έλνλ₯Ό νλ² κ·Έλ €λ³΄μ.
fig = go.Figure()
fig.add_trace(
go.Scatter(
x = planets_year_method_1[planets_year_method_1['method']=='Imaging']['year'],
y = planets_year_method_1[planets_year_method_1['method']=='Imaging']['number'], name = 'Imaging', mode = 'lines'))
fig.add_trace(
go.Scatter(
x = planets_year_method_1[planets_year_method_1['method']=='Radial Velocity']['year'],
y = planets_year_method_1[planets_year_method_1['method']=='Radial Velocity']['number'], name = 'Radial Velocity', mode = 'lines'))
fig.add_trace(
go.Scatter(
x = planets_year_method_1[planets_year_method_1['method']=='Eclipse Timing Variations']['year'],
y = planets_year_method_1[planets_year_method_1['method']=='Eclipse Timing Variations']['number'], name = 'Eclipse Timing Variations', mode = 'lines'))
fig.add_trace(
go.Scatter(
x = planets_year_method_1[planets_year_method_1['method']=='Transit']['year'],
y = planets_year_method_1[planets_year_method_1['method']=='Transit']['number'], name = 'Transit', mode = 'lines'))
fig.add_trace(
go.Scatter(
x = planets_year_method_1[planets_year_method_1['method']=='Microlensing']['year'],
y = planets_year_method_1[planets_year_method_1['method']=='Microlensing']['number'], name = 'Microlensing', mode = 'lines'))
fig.show()
π μ΄λ κ² μκ°ννλ νμ€ν μ 체μ μΈ λͺ¨μμ΄ μ 보μΈλ€. Radial Velocity λ 1990λ λλΆν° κΎΈμ€ν κ·Έ λκ°μ 보μ¬μκ³ Transit μ 2000λ λ μ΄λ°λΆν° μ μ°¨μ μΌλ‘ μ±μ₯νλλ 2010λ λ λ€μ΄μ μ°μΈν κ΄μΈ‘μΉλ₯Ό λνλΈλ€. λλ¨Έμ§ μΈκ°μ κ΄μΈ‘λ²μ μ΅κ·Ό λ€μ΄μ λͺ¨μ΅μ 보μ΄λ λ°©λ²μΈ λ―νλ€. νλκ°μ§ λ°©λ²μ μμ‘΄νμ§ μκ³ λ€λ₯Έ κΈ°μ μ΄ λ°λ¬νκ³ μλ κ·ΈλνλΌκ³ ν μ μμ κ² κ°λ€.
2. for λ¬Έμ μ¬μ©νλ κ²½μ°
π μ΄λ²μλ for λ¬Έμ μ΄μ©ν€λ³΄μ. μ¬μ€ μ΄λ°μ λ° μ± μ μ°Έκ³ νκ±°λ ꡬκΈλ§μ ν΄μ μ°Ύμλ΄λ plotly μμ for λ¬Έμ μ¬μ©νλ λ°©λ²μ΄ μ λμ€μ§ μμμ λ΅λ΅νλλ°, κ΅³μ΄ μ¬μ©νμ§ μλ μ΄μ λ₯Ό μ΄λ²μ μ μ μμλ κ² κ°λ€(μλλ©΄ κ΅μ₯ν λμ νλ₯ λ‘ λ΄κ° λͺ¨λ₯΄λ κ±°λβ¦π’).
λ°λ‘ μκ°νν΄λ³΄μ.
fig = go.Figure()
for i in planets_year_method_1['method']:
fig.add_trace(
go.Scatter(
x = planets_year_method_1[planets_year_method_1['method']==i]['year'],
y = planets_year_method_1[planets_year_method_1['method']==i]['number'], name = i, mode = 'lines'))
fig.show()
π μμ 보μ΄λ λλ‘ forλ¬Έ μμ add_trace() λͺ λ Ήμ΄μ graph_object κ°μ²΄(go.βͺ)λ₯Ό λ£μ΄μ κ·Έλν κ°μ²΄λ₯Ό for 루νλ§νΌ μμ±νλ κ²μ΄λ€. κ·Έλ¬λ©΄ κ²°κ³Όλ λ€μκ³Ό κ°λ€.
μμ κ·Έλνμμ λ³Ό μ μλ―μ΄ κ°μ method μ΄μ§λ§ μ€λ³΅λλ legned λ₯Ό κ°μ§κ³ μλ κ²μ νμΈ ν μ μλ€. μ¬μ§μ΄ legned μμ μ€λ³΅λ μκΉλ μλ€π. κ·Έλμ λλ μ΄ μν©μ columnμ μλ λͺ¨λ λ°μ΄ν°λ₯Ό λ¬Όλ¬μ€λλΌ μκΈ΄ μΌμ΄λΌ μκ°νκ³ method λ₯Ό μΈλ±μ€λ‘ groupby νλ©΄ ν΄κ²°ν μ μμ§ μμκΉ μμνλ€. μ½λμ κ²°κ³Όλ λ€μκ³Ό κ°λ€.
planets_year_method_2 = planets.groupby(['method', 'year']).agg({'number':'sum'})
planets_year_method_2
>>
number
method year
Astrometry 2010 1
2013 1
Eclipse Timing Variations 2008 4
2009 1
2010 4
... ... ...
Transit 2014 93
Transit Timing Variations 2011 2
2012 2
2013 2
2014 3
69 rows Γ 1 columns
μμ κ²°κ³Ό λ°μ΄ν°νλ μμ 보면 method μ year κ° μΈλ±μ€λ‘ λ€μ΄κ° μκ³ number λ§ column μΌλ‘ μ§μ λ κ²μ νμΈν μ μλ€. μ΄μ μμ λ°©λ²μ²λΌ for λ¬Έμ λλ €λ³΄μ.
fig = go.Figure()
for i in range(len(planets_year_method_2.index)):
fig.add_trace(
go.Scatter(
x = pd.Series(planets_year_method_2.index[i][1]),
y = pd.Series(planets_year_method_2['number'][i]),
name = planets_year_method_2.index[i][0], mode = 'lines + markers'))
fig.show()
μ΄μ λ μ¬μ§μ΄ line λ μ κ·Έλ €μ€λ€(β¦) κ·Έλμ λ΄ μκ°μΌλ‘λ plotly μμ for λ¬Έμ μ¬μ©νλ €λ©΄ μ€λ³΅λλ λ°μ΄ν°λ₯Ό κ°μ§κ³ μλ column μ μ¬μ©νλ κ²μ μμ νκ±°λ legend λ₯Ό κ·Έλ¦¬μ§ μλ λ°©λ²μ΄ μμ κ² κ°λ€. λ¬Όλ‘ μ΄μ κ·Έμ κ±Έμλ§λ₯Ό λΌκ³ μλ νμμ΄κ³ , μμΌλ‘ λ°°μλκ°μΌ ν κ²μ΄ λ무 λ§μ§λ§ μΌλ¨ λ΄ μ§§μ μ견μΌλ‘λ κ·Έλ λ€.
π κ·Έλλ μ΄λ κ² plotly μμ for loop λ리λ λ°©λ²μ μκ² λμμΌλ λ§μ‘±νλ€!! λμ€μ μ΄μ λν ν΄κ²°μ± μ λ°κ²¬νλ©΄ ν볡νκ² λ€μ νλ² ν¬μ€ν ν΄λ΄μΌκ² λ€.
Leave a comment