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

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

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

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

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

<λ³Έ λΈ”λ‘œκ·ΈλŠ” itadventrue λ‹˜μ˜ λΈ”λ‘œκ·Έλ₯Ό μ°Έκ³ ν•΄μ„œ κ³΅λΆ€ν•˜λ©° μž‘μ„±ν•˜μ˜€μŠ΅λ‹ˆλ‹€ :-)>

https://itadventure.tistory.com/551

 

νŒŒλ„!(10) - μ”¨λ³ΈμœΌλ‘œ 예쁜 κ·Έλž˜ν”„ κΎΈλ―ΈκΈ°

'νŒŒλ„'λŠ” 파이슀크립트 λ„μ „κΈ°μ˜ μ€„μž„λ§μž…λ‹ˆλ‹€. μ§€λ‚œ κ²Œμ‹œκΈ€μ— μ—°μž¬λ˜λŠ” κΈ€μž…λ‹ˆλ‹€ - https://itadventure.tistory.com/550 νŒŒλ„!(9) - 아보카도 νŒλ§€λŸ‰ κ·Έλž˜ν”„ 'νŒŒλ„'λŠ” 파이슀크립트 λ„μ „κΈ°μ˜ μ€„μž„λ§μž…λ‹ˆλ‹€

itadventure.tistory.com

 

 

πŸŽ„ 씨본(seaborn) λͺ¨λ“ˆμ˜ μƒ‰μƒνŒ”λ ˆνŠΈλ₯Ό μ΄μš©ν•˜μ—¬ κ·Έλž˜ν”„λ₯Ό 예쁘게 κΎΈλ―ΈκΈ°

❗  <py-env> νƒœκ·Έ 내에 seaborn λͺ¨λ“ˆμ„ 포함

<py-env>
  - pandas
  - matplotlib
  - seaborn
</py-env>

 

❗ λˆˆκΈˆμ„ μ •ν•˜λŠ” λΆ€λΆ„μ—μ„œ λ„˜νŒŒμ΄(numpy)λͺ¨λ“ˆμ„ μ‚¬μš©

numpy λͺ¨λ“ˆμ€ <py-env>νƒœκ·Έ 내에 적어주지 μ•Šμ•„λ„ 됨

25000 λΆ€ν„° 60000 κΉŒμ§€ 5000 λ§ˆλ‹€ κ±΄λ„ˆλ›΄ λ°°μ—΄ λ§Œλ“€ 수 있음

plt.yticks(np.arange(25000, 60000, 5000))

 

❗ 컬러파레트둜 λ§‰λŒ€κ·Έλž˜ν”„ κΎΈλ―ΈκΈ°

컬레 파레트λͺ…이 'rainbor_r'

'rainbow_r'을 λΆ™μ—¬μ–΄ λΉ¨μ£Όλ…Έμ΄ˆνŒŒλ‚¨λ³΄ 순으둜 λ‚˜μ˜€κ³ , 'rainbow' νŒ”λ ˆνŠΈλ₯Ό μ‚¬μš©ν•˜λ©΄ μ—­μˆœ

plt.bar(
  df_group.index.to_list(), 
  df_group['Volume2'].to_list(),
  color=sns.color_palette(
    'rainbow_r', 
    12
  ),
  edgecolor='black'
)

 

❗ κ° λ§‰λŒ€μ˜ 검정색 경계선을 μΆ”κ°€ -> edgecolor='black' 

plt.bar(
  df_group.index.to_list(), 
  df_group['Volume2'].to_list(),
  color=sns.color_palette(
    'rainbow_r', 
    12
  ),
  edgecolor='black'
)

 

❗ λ¬΄μ§€κ°œ 색상은 7κ°€μ§€μ΄μ§€λ§Œ 12λ‹¨κ³„λ‘œ 색을 λ‚˜λˆ„κΈ°

12λ²ˆλ§ˆλ‹€ 색상이 λ¬΄μ§€κ°œ 색상이 λ‚˜λˆ„μ–΄ ν‘œν˜„

color=sns.color_palette(
  'rainbow_r', 
  12
),

 

❗ 배경색도 λ„£κΈ°

ax = plt.gca()
ax.set_facecolor('#e8e7d2')

 

❗ μ½”λ“œ κ΅¬ν˜„

<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
        - seaborn
      </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

      import seaborn as sns      
      import numpy as np
      
      <!-- νŒλ‹€μŠ€μ—μ„œ 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(np.arange(25000, 60000, 5000))

      <!-- # λ§‰λŒ€ κ·Έλž˜ν”„ -->
      plt.bar(
        df_group.index.to_list(), 
        df_group['Volume2'].to_list(),
        color=sns.color_palette(
          'rainbow_r', 
          12
        ),
        edgecolor='black'
      )
      
      plt.xlabel('Year/Month')
      plt.ylabel('Sales(units:10000)')
      
      ax = plt.gca()
      ax.set_facecolor('#e8e7d2')

      <!-- κ·Έλ¦¬λ“œλΌλŠ” μ•ˆλ‚΄μ„ μ„ μΆ”κ°€ -->
      plt.grid()
      fig
    </py-script> 
  </body> 
</html>

 

 

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