728x90
반응형
파이썬에서 제공하는 Matplotlib 라이브러리를 이용해 데이터를 그래프로 보여주는 예제를 소개합니다.
Tibero에서 데이터를 가져와서 데이터를 출력하는 예제입니다.
사전 작업
- python 설치
- pyodbc 설치
- Tibero ODBC 설치
- Tibero Sample Schema 생성(첨부 파일-scott.sql)
- pip install pyodbc
- pip install matplotlib
필수 라이브러리 설치
(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 ...
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
반응형
'03.Program > 03.Python' 카테고리의 다른 글
[Python] 문자열 붙이기 (1) | 2024.09.27 |
---|---|
[Pycharam] 프로젝트 여러개 실행 (0) | 2022.05.22 |
[Python] 파이썬 가상 환경 사용법 [python2.7,python3.7] (0) | 2022.04.15 |
[Python]리눅스 pexpect 를 이용한 사용자 패스워드 일괄변경 (0) | 2022.04.15 |
[Centos] Python 설치 (0) | 2022.04.15 |