😎 κ³΅λΆ€ν•˜λŠ” μ§•μ§•μ•ŒνŒŒμΉ΄λŠ” μ²˜μŒμ΄μ§€?

HTMLμ—μ„œ Python을 μ‚¬μš©ν•  수 μžˆλŠ” PyScript (8) λ³Έλ¬Έ

πŸ‘©‍πŸ’» λ°±μ—”λ“œ(Back-End)/Node js

HTMLμ—μ„œ Python을 μ‚¬μš©ν•  수 μžˆλŠ” PyScript (8)

μ§•μ§•μ•ŒνŒŒμΉ΄ 2022. 11. 25. 15:33
728x90
λ°˜μ‘ν˜•

<λ³Έ λΈ”λ‘œκ·ΈλŠ” 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>

 

 

 

 

728x90
λ°˜μ‘ν˜•
Comments