본문 바로가기
학부 자료/Python

[Python] 파이썬 기초 17/20, 데이터의 시각화 처리

by jackMK 2023. 11. 7.

<내용정리>


- 관련 파일

17일차_221228.py
0.00MB


- 본문

# # p328 응용예제 3번
# import requests
# from bs4 import BeautifulSoup
# url = 'https://movie.naver.com/movie/sdb/rank/rmovie.naver'
# response = requests.get(url)
# html = response.text
# soup = BeautifulSoup(html, 'html.parser')
# movie_list = soup.find_all('tr')
#
# up_list = []
# for movie in movie_list:
#     target_list = movie.find_all('td', class_='ac')
#     if target_list:
#         if target_list[1].find('img', class_='arrow').get('alt') == 'up':
#             up_list.append(movie.find('td', class_='title').text.strip())
# for up_movie in up_list:
#     print(up_movie)

### 섹션 19 데이터 시각화 처리
# 꺾은 선형 그래프 : plot()
import matplotlib.pyplot as plt
figure = plt.figure()
axes = figure.add_subplot(111)  # 1행 1열 1번째
x = [0, 1, 2, 3, 4]  # x축 값
y = [4, 1, 3, 5, 2]  # y축 값
axes.plot(x, y)
plt.show()

# 꺾은 선형 그래프 2개
import matplotlib.pyplot as plt
figure = plt.figure()
axes = figure.add_subplot(111)
x1 = [0, 1, 2, 3, 4]
y1 = [4, 1, 3, 5, 2]
x2 = [0, 1, 2, 3, 4]
y2 = [0, 8, 5, 3, 1]
axes.plot(x1, y1)
axes.plot(x2, y2)
plt.show()

# 꺾은 선형 꾸미기
import matplotlib.pyplot as plt
figure = plt.figure()
axes = figure.add_subplot(111)
axes.plot([0, 1, 2, 3, 4], [0, 3, 0, 3, 0], linestyle='--', linewidth=5.0, color='skyblue')
axes.plot([0, 1, 2, 3, 4], [1, 2, 3, 4, 5], color='green', marker='o')
plt.show()

# p340 ex)
import matplotlib.pyplot as plt
figure = plt.figure()
axes = figure.add_subplot(111)
x = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
y = [1200, 800, 500, 400, 700, 800]
axes.plot(x, y, linestyle='--', marker='^')
plt.show()

# 막대 그래프 : bar()
import matplotlib.pyplot as plt
figure = plt.figure()
axes = figure.add_subplot(111)
x = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
y = [8, 6, 5, 4, 7, 9, 5]
axes.bar(x, y)
plt.title('WeekDay Call')  # 차트 제목
plt.xlabel('Week')
plt.ylabel('Call')
plt.show()

# 산포 그래프 : scatter()
import matplotlib.pyplot as plt
figure = plt.figure()
axes = figure.add_subplot(111)
x = [1, 2, 3, 4, 5, 6]
y = [6, 4, 1, 2, 7, 5]
area = [50, 100, 150, 200, 250, 300]  # 각 점의 크기
color = ['red', 'green', 'blue', 'orange', 'aqua', 'crimson']
axes.scatter(x, y, s=area, c=color)
plt.show()

# p343 ex)
# 산포 그래프 : scatter()
import matplotlib.pyplot as plt
figure = plt.figure()
axes = figure.add_subplot(111)
noise = [20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70]
stress = [10, 11, 15, 20, 30, 42, 55, 70, 88, 110, 150]
axes.scatter(noise, stress, s=50, c='aqua')
plt.xlabel('noise')
plt.ylabel('stress')
plt.show()

# 원형 그래프 : pie()
import matplotlib.pyplot as plt
figure = plt.figure()
axes = figure.add_subplot(111)
data = [1, 2, 3]
axes.pie(data)
plt.axis('equal')
plt.show()

# 범례와 레이블을 추가한 원형 그래프
import matplotlib.pyplot as plt
figure = plt.figure()
axes = figure.add_subplot(111)
data = [1, 2, 3]
label = ['Good', 'Bad', 'Normal']
axes.pie(data, labels = label, autopct = '%d%%')
plt.axis('equal')
plt.legend(label, loc="lower right")
plt.show()

# p348 응용예제 1번
import matplotlib.pyplot as plt
figure = plt.figure(figsize=(12, 12))  # 차트 사이즈
axes = figure.add_subplot(111)
bitamin = ['bA', 'bB1', 'bB2', 'na', 'bB6', 'bC', 'bD', 'bE', 'bM']
data = [0.18, 0.3, 3.33, 3.75, 0.38, 25, 0.25, 2.75, 0.1]
axes.pie(data, labels = bitamin, autopct='%0.1f%%')
plt.axis('equal')
plt.show()

# 응용예제 2번
import random
import matplotlib.pyplot as plt
figure = plt.figure(figsize=(20, 6))
axes = figure.add_subplot(111)
x = [n for n in range(101)]  # 리스트 내포
y1 = []  # 꺾은선
y2 = []  # 막대
for i in range(101):
  y1.append(random.randint(0, 100))
  y2.append(random.randint(0, 100))
axes.plot(x, y1, color='r', marker='.')
axes.bar(x, y2, color='g')
plt.show()

loading