π 곡λΆνλ μ§μ§μνμΉ΄λ μ²μμ΄μ§?
HTMLμμ Pythonμ μ¬μ©ν μ μλ PyScript (8) λ³Έλ¬Έ
HTMLμμ Pythonμ μ¬μ©ν μ μλ PyScript (8)
μ§μ§μνμΉ΄ 2022. 11. 25. 15:33<λ³Έ λΈλ‘κ·Έλ itadventrue λμ λΈλ‘κ·Έλ₯Ό μ°Έκ³ ν΄μ 곡λΆνλ©° μμ±νμμ΅λλ€ :-)>
https://itadventure.tistory.com/550
νλ!(9) - μ보카λ νλ§€λ κ·Έλν
'νλ'λ νμ΄μ€ν¬λ¦½νΈ λμ κΈ°μ μ€μλ§μ λλ€. μ§λ κ²μκΈμ μ°μ¬λλ κΈμ λλ€ - https://itadventure.tistory.com/549 νλ!(8) - μ보카λ νλ§€λ μ§λ κ²μκΈμ μ°μ¬λλ κΈμ λλ€ - https://itadventure.tist
itadventure.tistory.com
π μ보카λ μμΉμ λ¨μ μ€μ΄κΈ°
β Total Volumn κ°μ 10,000μΌλ‘ λλμ΄ μλ‘μ΄ Volume2 μ΄λΌλ κ°μ μμ±
df.insert(5, 'Volume2', df['Total Volume']/10000, True)
- 첫째μ리κΉμ§λ§ νμνκ³ μΆλ€λ©΄, 10,000μ΄λ μμΉλ‘ λλλ λ°μ΄ν°λ₯Ό μΆκ°ν λ μλμ κ°μ΄ round ν¨μλ₯Ό μ¬μ©
df.insert(5, 'Volume2',
round(df['Total Volume']/10000, 1),
True)
β μλ³ λ§€μΆλ μ‘°μ¬
# μλ³ λ§€μΆλ μ‘°μ¬
df_group = df.fillna(0) \
.groupby('month')[['Volume2']] \
.sum() \
.sort_values(
by='month',
ascending=True
)
π μ보카λ λ§λ κ·Έλν 그리기
plt.bar() ν¨μλ₯Ό μ¬μ©
β κ·Έλν ν¬κΈ° μ‘°μ
fig = plt.figure(figsize=(15, 10))
β κΈμ¨λ₯Ό 45λ νμ
plt.xticks(rotation=45)
β 그리λλΌλ μλ΄μ μ μΆκ°
plt.grid()
β μλ³ μ보카λ λ§€μΆλ κ·Έλν
<html>
<head>
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
<py-env>
- pandas
- matplotlib
</py-env>
</head>
<body>
<link rel="stylesheet" href="pytable.css"/>
<py-script>
<!-- νλ₯Ό HTML νλ©΄μ μΆκ°νλ λΆλΆμ νμ΄μ¬ ν¨μλ‘ λ¨μν -->
def createElementDiv(name):
element = document.createElement('div')
element.id = name
document.body.append(element)
return Element(name)
import pandas as pd
from pyodide.http import open_url
<!-- νλ€μ€μμ csv λ₯Ό λ°μ΄ν° νλ μμΌλ‘ μ½μ΄μ΄ -->
df = pd.read_csv(open_url("http://dreamplan7.cafe24.com/pyscript/csv/avocado.csv"))
<!-- Total Volumn κ°μ 10,000μΌλ‘ λλμ΄ μλ‘μ΄ Volume2 μ΄λΌλ κ°μ μμ± (round λ₯Ό ν΅ν΄ 첫째 μ리κΉμ§ μμμ리 νμ) -->
df.insert(2, 'month', df['Date'].str[:7], True)
df.insert(5, 'Volume2',
round(df['Total Volume']/10000, 1),
True)
createElementDiv('output1').write(df)
<!-- Volume2μΌλ‘ μλ³ λ§€μΆλ μ‘°μ¬ -->
df_group = df.fillna(0).groupby('month')[['Total Volume']].sum().sort_values(by='month', ascending=True)
<!-- μλ³ λ§€μΆλ μ‘°μ¬ -->
df_group = df.fillna(0) \
.groupby('month')[['Volume2']] \
.sum() \
.sort_values(
by='month',
ascending=True
)
createElementDiv('output1').write(df_group)
<!-- μ보카λ κ·Έλν 기리기 -->
import matplotlib.pyplot as plt
<!-- κ·Έλν ν¬κΈ° μ‘°μ -->
fig = plt.figure(figsize=(15, 10))
plt.title('Month Avocado sales');
<!-- κΈμ¨λ₯Ό 45λ νμ -->
plt.xticks(rotation=45)
plt.bar(
df_group.index.to_list(),
df_group['Volume2'].to_list()
)
plt.xlabel('Year/Month')
plt.ylabel('Sales(units:10000)')
<!-- 그리λλΌλ μλ΄μ μ μΆκ° -->
plt.grid()
fig
</py-script>
</body>
</html>
β xlim, ylimμ μ¬μ©νμ¬ λκΈ μ§μ νκΈ°
- μλ λ³νλ₯Ό νμ€ν 보기 μν΄ 2μ΅ 5μ²λ§λΆν° μμν΄μ 6μ΅κΉμ§ YμΆμ μ§μ
plt.yticks([
25000, 30000, 35000, 40000,
45000, 50000, 55000, 60000
])
- μλ λ³κ²½
# λ§λ κ·Έλν
plt.bar(
df_group.index.to_list(),
df_group['Volume2'].to_list(),
color='forestgreen'
)
- μμ λ κ·Έλν μ½λ ꡬν
<html>
<head>
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
<py-env>
- pandas
- matplotlib
</py-env>
</head>
<body>
<link rel="stylesheet" href="pytable.css"/>
<py-script>
<!-- νλ₯Ό HTML νλ©΄μ μΆκ°νλ λΆλΆμ νμ΄μ¬ ν¨μλ‘ λ¨μν -->
def createElementDiv(name):
element = document.createElement('div')
element.id = name
document.body.append(element)
return Element(name)
import pandas as pd
from pyodide.http import open_url
<!-- νλ€μ€μμ csv λ₯Ό λ°μ΄ν° νλ μμΌλ‘ μ½μ΄μ΄ -->
df = pd.read_csv(open_url("http://dreamplan7.cafe24.com/pyscript/csv/avocado.csv"))
<!-- Total Volumn κ°μ 10,000μΌλ‘ λλμ΄ μλ‘μ΄ Volume2 μ΄λΌλ κ°μ μμ± (round λ₯Ό ν΅ν΄ 첫째 μ리κΉμ§ μμμ리 νμ) -->
df.insert(2, 'month', df['Date'].str[:7], True)
df.insert(5, 'Volume2',
round(df['Total Volume']/10000, 1),
True)
createElementDiv('output1').write(df)
<!-- Volume2μΌλ‘ μλ³ λ§€μΆλ μ‘°μ¬ -->
df_group = df.fillna(0).groupby('month')[['Total Volume']].sum().sort_values(by='month', ascending=True)
<!-- μλ³ λ§€μΆλ μ‘°μ¬ -->
df_group = df.fillna(0) \
.groupby('month')[['Volume2']] \
.sum() \
.sort_values(
by='month',
ascending=True
)
createElementDiv('output1').write(df_group)
<!-- μ보카λ κ·Έλν 기리기 -->
import matplotlib.pyplot as plt
<!-- κ·Έλν ν¬κΈ° μ‘°μ -->
fig = plt.figure(figsize=(15, 10))
plt.title('Month Avocado sales');
<!-- κΈμ¨λ₯Ό 45λ νμ -->
plt.xticks(rotation=45)
<!-- μλ λ³νλ₯Ό νμ€ν 보기 μν΄ 2μ΅ 5μ²λ§λΆν° μμν΄μ 6μ΅κΉμ§ YμΆμ μ§μ -->
plt.ylim(25000, 60000)
<!-- yμΆμ λκΈμ μ§μ -->
plt.yticks([
25000, 30000, 35000, 40000,
45000, 50000, 55000, 60000
])
<!-- # λ§λ κ·Έλν -->
plt.bar(
df_group.index.to_list(),
df_group['Volume2'].to_list(),
color='forestgreen'
)
plt.xlabel('Year/Month')
plt.ylabel('Sales(units:10000)')
<!-- 그리λλΌλ μλ΄μ μ μΆκ° -->
plt.grid()
fig
</py-script>
</body>
</html>
'π©βπ» λ°±μλ(Back-End) > Node js' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
HTMLμμ Pythonμ μ¬μ©ν μ μλ PyScript (10) (1) | 2022.11.25 |
---|---|
HTMLμμ Pythonμ μ¬μ©ν μ μλ PyScript (9) (1) | 2022.11.25 |
HTMLμμ Pythonμ μ¬μ©ν μ μλ PyScript (7) (0) | 2022.11.25 |
HTMLμμ Pythonμ μ¬μ©ν μ μλ PyScript (6) (0) | 2022.11.25 |
HTMLμμ Pythonμ μ¬μ©ν μ μλ PyScript (5) (0) | 2022.11.25 |