728x90
반응형

파이썬에서 제공하는 Matplotlib  라이브러리를 이용해 데이터를 그래프로 보여주는 예제를 소개합니다. 

Tibero에서 데이터를 가져와서 데이터를 출력하는 예제입니다. 

 

사전 작업 

  1. python 설치
  2. pyodbc 설치
  3. Tibero ODBC 설치 
  4. Tibero Sample Schema 생성(첨부 파일-scott.sql)
  5. pip install pyodbc
  6. pip install matplotlib

scott.sql
0.00MB

필수 라이브러리 설치

(testEnv) C:\pydev\venv>python -m pip install --upgrade pip
Collecting pip
  Using cached https://files.pythonhosted.org/packages/4d/16/0a14ca596f30316efd412a60bdfac02a7259bf8673d4d917dc60b9a21812/pip-22.0.4-py3-none-any.whl
Installing collected packages: pip
  Found existing installation: pip 19.2.3
    Uninstalling pip-19.2.3:
      Successfully uninstalled pip-19.2.3
Successfully installed pip-22.0.4

(testEnv) C:\pydev\venv>pip install pyodbc
Collecting pyodbc
  Downloading pyodbc-4.0.32-cp37-cp37m-win_amd64.whl (73 kB)
     ---------------------------------------- 73.1/73.1 KB ? eta 0:00:00
Installing collected packages: pyodbc
Successfully installed pyodbc-4.0.32


(testEnv) C:\pydev\venv> pip install matplotlib
Collecting matplotlib
  Downloading https://files.pythonhosted.org/packages/71/97/b93e7edcdf5f0321bef2c8404ca8e8401e7f1f869ba8ee986f71514ca1b3/matplotlib-3.5.1-cp37-cp37m-win_amd64.whl (7.2MB)
     |████████████████████████████████| 7.2MB 6.4MB/s
Collecting kiwisolver>=1.0.1 (from matplotlib)
  Downloading https://files.pythonhosted.org/packages/96/27/cd4bb740e363eaf9241f26ca14504c5b2603f73d6fa1e3782913c3b0b701/kiwisolver-1.4.2-cp37-cp37m-win_amd64.whl (54kB)
     |████████████████████████████████| 61kB ...

 

소스 코드

import pyodbc
import numpy as np
import matplotlib.pyplot as plt
db = pyodbc.connect('DSN=Tibero6;UID=tibero;PWD=tmax')
cursor = db.cursor()

cursor.execute("select trunc(count(*)) from emp")
for row in cursor:
    totalemps=int(row[0])
    print(totalemps)
t=np.arange(totalemps)
cursor.execute("select ename,sal from emp")
names=[]
salaries=[]
for row in cursor:
    names.append(row[0])
    salaries.append(row[1])
bar_width=0.5
plt.bar(t,salaries,bar_width,label="Salary")
plt.title("Employee Details")
plt.xlabel("Employee")
plt.ylabel("Salary")
plt.xticks(t,names)
plt.grid(True)
plt.legend()
xs=[x for x in range(1,totalemps)]
for x,y in zip(xs,salaries):
    plt.annotate(salaries[x],(x-bar_width/2,y))
plt.show()

소스 실행

  • python matplotlib_sample.py

 

 

 

728x90
반응형

+ Recent posts