π 2. νμ±μ μ¬λ¬κ°μ§ μ 보λ₯Ό μμ보μ(2)
π μ λ² ν¬μ€ν κ³Ό κ°μ planets λ°μ΄ν°λ₯Ό κ°μ§κ³ μ μ²λ¦¬λ₯Ό μ§νν΄λ³΄μ.
1. λ°μ΄ν° λΆλ¬μ€κΈ°
import pandas as pd
import seaborn as sns
planets = sns.load_dataset('planets')
planets.shape
>> (1035, 6)
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
planets.info()
>>
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1035 entries, 0 to 1034
Data columns (total 6 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 method 1035 non-null object
1 number 1035 non-null int64
2 orbital_period 992 non-null float64
3 mass 513 non-null float64
4 distance 808 non-null float64
5 year 1035 non-null int64
dtypes: float64(3), int64(2), object(1)
memory usage: 48.6+ KB
orbital_period / mass / distance μ΄μ NaN κ°μ΄ μ‘΄μ¬ν¨μ νμΈν μ μλ€.
νμ§λ§ μ΄ κ°λ€μ λμ€μ μ°λ¦¬κ° μνλ λ°μ΄ν°λ₯Ό μ ννλ κ²½μ°μ μ²λ¦¬ν΄μ£Όλλ‘ νμ.
2. Finding numbers per year by Redial Velocity since 1995
π μ΄μ ν¬μ€ν
μμ νμ±μ κ°μ₯ λ§μ΄ λ°κ²¬ν κ΄μΈ‘λ°©λ²μ Radial Velocity μμ νμΈν μ μμλ€.
π μ΄μ μ΄ λ°©λ²μ΄ λ
λλΉ λ°κ²¬ν νμ± μλ λͺκ°μΈμ§ κ·Έ μΆμ΄λ₯Ό νλ² μμ보μ.
planets[planets['method'] == "Radial Velocity"].sort_values('year').head()
>>
method number orbital_period mass distance year
441 Radial Velocity 1 83.888000 11.6800 40.57 1989
16 Radial Velocity 1 4.230785 0.4720 15.36 1995
62 Radial Velocity 1 3.313500 3.9000 15.60 1996
64 Radial Velocity 4 4.617033 0.6876 13.47 1996
25 Radial Velocity 1 116.688400 NaN 18.11 1996
1989λ
λΆν° 2014λ
κΉμ§ μ¬λ¬κ°μ νμ±λ€μ λ°κ²¬ν κ²μ νμΈν μ μλ€.
νμ§λ§ 1989λ
κ³Ό 1995λ
μ κΈ°κ°μ΄ μλΉν κΈΈκ³ , 1989λ
μ λ°κ²¬λ νμ± μκ° νλ λΏμ΄κΈ° λλ¬Έμ 1995λ
λΆν° μ§κ³λ₯Ό νλ κ²μ΄ κΉλν μλ μμκ±° κ°λ€.
μΌλ¨μ planets λ°μ΄ν°νλ μμμ νμν method, number, year μ΄λ§ κ°μ Έμμ Radial Velocity λ§μ νν°λ§ν΄λ³΄μ.
planets_number_year = planets[['method', 'number', 'year']].copy()
planets_number_year_Radial = planets_number_year[planets_number_year['method']=='Radial Velocity']
planets_number_year_Radial.head()
>>
method number year
0 Radial Velocity 1 2006
1 Radial Velocity 1 2008
2 Radial Velocity 1 2011
3 Radial Velocity 1 2007
4 Radial Velocity 1 2009
μ΄μ νμν λ°μ΄ν°λ§ κ°μ Έμ¬ μ°¨λ‘λ€.
planets_number_year_Radial λ°μ΄ν°νλ μμ μ΄μ°¨νΌ Radial Velocity λ§μ νν°λ§ν λ°μ΄ν°μ΄κΈ° λλ¬Έμ μ°μ method μ΄μ μμ ν΄μ κ°λ΅νν΄μ£Όμ.
κ·Έ λ€μμλ yearμ΄μ κΈ°μ€μΌλ‘ κ·Έλ£Ήννκ³ sumμΌλ‘ μ§κ³ν΄μ€λ€.
κ·Έλ¦¬κ³ μΈλ±μ€λ₯Ό .sort_index() ν¨μλ‘ μ λ ¬νλ©΄ κΉλν λ°μ΄ν°κ° λμ¨λ€.
del planets_number_year_Radial['method']
planets_number_year_Radial = planets_number_year_Radial.groupby('year').sum().sort_index()
planets_number_year_Radial.head()
>>
number
year
1989 1
1995 1
1996 15
1997 1
1998 11
λ§μ§λ§μΌλ‘ 1995λ μ΄μ μ λ λλ₯Ό μμ μ£ΌκΈ° μν΄μ μ¬λΌμ΄μ±μ ν΄μ£Όμ.
planets_number_year_Radial = planets_number_year_Radial[1:]
planets_number_year_Radial.head()
>>
number
year
1995 1
1996 15
1997 1
1998 11
1999 24
2.1. iplot μκ°ν
π λ λμ λ°λ₯Έ μΆμΈλ₯Ό νλμ μμ보기 μ½κ² line κ·Έλνλ₯Ό κ·Έλ €λ³΄μ.
import chart_studio.plotly as py
import cufflinks as cf
cf.go_offline(connected = True)
2.1.1. Bar graph
layout = {
'title' : {'text' : '<b>Finding numbers per year in Redial Velocity since 1995</b>',
'font' : {'size':25}, 'x':0.5, 'y':0.9},
'xaxis' : {'showticklabels' : True, 'dtick': 1, 'title' : {'text' : 'Year', 'font' : {'size' : 15}}},
'yaxis' : {'showticklabels' : True, 'title' : {'text' : 'Number', 'font' : {'size' : 15}}}
}
planets_number_year_Radial.iplot(kind = 'scatter', mode = 'lines+markers', layout = layout)
2.2. plotly μκ°ν
π κ°μ₯ λ§μ νμ±μ λ°κ²¬ν ν΄λ₯Ό μμ보기 μ½κ² bar κ·Έλνμ line κ·Έλνλ₯Ό λ§λ€μ΄λ³΄μ.
import plotly.graph_objects as go
import plotly.offline as pyo
pyo.init_notebook_mode()
2.2.1. Bar graph
colors = ['#03658C',] * len(planets_number_year_Radial.index)
colors[16] = '#F29F05'
fig = go.Figure()
fig.add_trace(
go.Bar(
x = planets_number_year_Radial.index, y = planets_number_year_Radial['number'],
text = planets_number_year_Radial['number'],
textposition='inside', texttemplate = '%{text}', textfont=dict(color = 'white', size = 10),
marker_color = colors))
fig.update_layout({
'title' : {'text' : '<b>Finding numbers per year in Redial Velocity since 1995</b>',
'font' : {'size':25}, 'x':0.5, 'y':0.9},
'xaxis' : {'showticklabels' : True, 'dtick' : 1, 'title' : {'text' : 'Year', 'font' : {'size' : 15}}},
'yaxis' : {'showticklabels' : True, 'title' : {'text' : 'Year', 'font' : {'size' : 15}}},
'template' : 'plotly_white'
})
fig.add_annotation(
x = 2011, y = 180,
text = '<b>2011 : 176</b>',
showarrow = True,
font = {'size' : 10, 'color' : '#ffffff'},
align = 'center',
arrowhead = 2,
arrowsize = 1,
arrowwidth = 2,
arrowcolor = '#F29F05',
ax = 40, ay = -30,
bordercolor = '#F29F05',
borderwidth = 2,
borderpad = 4,
bgcolor = '#F29F05',
opacity = 0.8
)
fig.show()
2.2.2. Line graph
fig = go.Figure()
fig.add_trace(
go.Scatter(
x = planets_number_year_Radial.index, y = planets_number_year_Radial['number'], mode = 'lines+markers'))
fig.update_layout({
'title' : {'text' : '<b>Finding numbers per year in Redial Velocity since 1995</b>',
'font' : {'size' : 25}, 'x' : 0.5, 'y' : 0.9},
'xaxis' : {'showticklabels' : True, 'dtick' : 1, 'title' : {'text' : 'Year', 'font' : {'size' : 15}}},
'yaxis' : {'showticklabels' : True, 'title' : {'text' : 'Year', 'font' : {'size' : 15}}},
'template' : 'ggplot2'
})
fig.add_annotation(
x = 2011, y = 180,
text = '<b>2011 : 176</b>',
showarrow = True,
font = {'size' : 10, 'color' : '#ffffff'},
align = 'center',
arrowhead = 2,
arrowsize = 1,
arrowwidth = 2,
arrowcolor = '#F22E62',
ax = 40, ay = -30,
bordercolor = '#F22E62',
borderwidth = 2,
borderpad = 4,
bgcolor = '#F22E62',
opacity = 0.8
)
fig.show()
π μ΄λ κ² ν¬μ€ν
λκ°λ₯Ό ν΅ν΄μ νμ± λ°μ΄ν°λ₯Ό μ μ²λ¦¬νκ³ μκ°ννλ κ³Όμ μ νμΈν΄λ³΄μλ€.
π μ΄ λ°μ΄ν°λ₯Ό μ²λ¦¬νλ©΄μ μμ¬μ΄ μ μ μ°μ£Ό νμ±μ λ°μ΄ν°μ΄λ€ 보λ κ° νΉμ§λ€ κ°μ κ΄κ³κ° κ±°μ μλ€λ μ μ΄μλ€. μ§λμ΄ μ΄λ μ λμΌλ κΆ€λμ£ΌκΈ°λ μ΄λ μ λμ΄λ€ λΌλ κ΄κ³κ° μμ μ€ μμλλ° λ°μ΄ν°μ ν€λλ§ λ³΄μλ κ·Έλ μ§ μλ€λ κ²μ μ½κ² μ μ μμλ€. λ¬Όλ‘ λͺ©μ μ νμν μ΄μλ κ²°μΈ‘μΉκ° μλ κΉλν λ°μ΄ν°μμ§λ§, 곡λΆνλ μ
μ₯μμλ μ½κ° μμ¬μμ΄ μμλ€.
π νμ§λ§ Line κ·Έλνλ₯Ό κ·Έλ¦¬κ³ μ§μ annotationμ νλ κ³Όμ μ΄ μ¬λ°μλ€γ
γ
Leave a comment