데이터시각화 : 1. Matplotlib
데이터시각화 : 2. Seaborn(1)
데이터시각화 : 3. Seaborn(2)
import matplotlib
import matplotlib.pyplot as plt
# 데이터 준비
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# 그래프 그리기
plt.plot(x, y)
# 그래프 보여주기
plt.show()
plt.axis([xmin, xmax, ymin, ymax])
그래프의 축 범위를 지정할 수 있습니다. 각각의 값은 x 축의 최솟값, 최댓값, y 축의 최솟값, 최댓값을 나타냅니다.
# 데이터 준비
x = [1, 2, 3, 4]
y = [2, 4, 6, 8]
# 그래프 그리기
plt.plot(x, y)
# x 축 레이블 추가
plt.xlabel('X-axis Label')
# 그래프 보여주기
plt.show()
x = [1, 2, 3, 4]
y1 = [2, 4, 6, 8]
y2 = [1, 3, 5, 7]
# 그래프 그리기
plt.plot(x, y1, label='Line 1')
plt.plot(x, y2, label='Line 2')
# 범례 추가
plt.legend()
# 그래프 보여주기
plt.show()
plt.title('Graph Title')
# 데이터 준비
x = [1, 2, 3, 4]
y = [2, 4, 6, 8]
# 그래프 그리기
plt.plot(x, y)
# 격자 추가
plt.grid()
# 그래프 보여주기
plt.show()
import matplotlib.pyplot as plt
# 선 그래프
plt.plot([1, 2, 3, 4], 'b*:', label='graph1') # (0, 1), (1, 2), ....
plt.plot([1, 2, 3, 4], [1, 2, 3, 4], linestyle='dashed', color="#8f34eb", label='graph2') # (1, 1), (2, 2), ....
font1 = {'family': 'fantasy', 'color': 'red', 'weight': 'normal', 'size': 'xx-large'}
# 축 레이블 지정
plt.axis([0, 5, 0, 10])
plt.xlabel('xxxxx', labelpad=30, loc='right', fontdict=font1)
plt.ylabel('yyy', labelpad=0, loc='top')
# 범례
plt.legend(loc="upper right", ncol=2, fontsize=20, frameon=True, shadow=True)
# 타이틀
plt.title('Title', loc='right', pad=50)
# 그리드
plt.grid(True, axis='y')
plt.show()
import matplotlib.pyplot as plt
# 데이터 준비
x = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
y1 = [1000, 1200, 1400, 1100, 1300, 1500, 1600, 1700, 1800, 2000, 1900, 2100]
y2 = [900, 1100, 1300, 1200, 1400, 1600, 1700, 1600, 1500, 1800, 2000, 1900]
# 그래프 그리기
plt.plot(x, y1, color='blue', label='2022')
plt.plot(x, y2, color='orange', label='2023')
# 가로축 눈금 설정
plt.xticks(x)
#축 레이블 지정
plt.xlabel('Month')
plt.ylabel('Sales')
# 그래프 제목 설정
plt.title('Monthly Sales Comparison (2022-2023)')
# 범례 추가
plt.legend()
# 그래프 보여주기
plt.show()
# x 값 범위 설정
x = np.linspace(0, 10, 100)
print(x)
# sin 함수와 cos 함수 계산
y_sin = np.sin(x)
y_cos = np.cos(x)
# 그래프 그리기
plt.plot(x, y_sin, color='green', label='sin(x)')
plt.plot(x, y_cos, color='orange', label='cos(x)')
# 가로축 눈금 설정
plt.xticks(rotation=180)
plt.yticks(rotation=180)
#축 레이블 지정
plt.xlabel('x')
plt.ylabel('y')
# 그래프 제목 설정
plt.title('sine and cosice Funtions')
# 격자 추가
plt.grid()
# 범례추가
plt.legend()
# 그래프 보여주기
plt.show()
# 데이터 준비
categories = ['A', 'B', 'C', 'D']
values = [10, 20, 15, 25]
# 막대 그래프 그리기
plt.bar(categories, values)
# 그래프 보여주기
plt.show()
import numpy as np
x = np.arange(3) # [0 1 2]
plt.bar(x, [100, 200, 300], color=['r', 'g', 'b'], width=0.6)
plt.xticks(x, ['a', 'b', 'c'])
plt.show()
import numpy as np
y = np.arange(3) # [0 1 2]
plt.barh(y, [100, 200, 300], color=['r', 'g', 'b'], height=0.6)
plt.yticks(y, ['a', 'b', 'c'])
plt.show()
# 데이터 준비
x = [1, 2, 3, 4]
y = [10, 20, 15, 25]
# 그래프 그리기
plt.plot(x, y)
# x 축 눈금 설정
plt.xticks(x, ['A', 'B', 'C', 'D'])
# 그래프 보여주기
plt.show()
# 데이터 준비
categories = ['Category 1', 'Category 2','Category 3', 'Category 4', 'Category 5']
data = [20, 35, 15, 27, 45]
# 막대 그래프 그리기
plt.bar(categories, data)
# 가로축 눈금 설정
plt.xticks(categories, rotation=45)
#축 레이블 지정
plt.xlabel('Categories')
plt.ylabel('Value')
# 그래프 제목 설정
plt.title('Bar Chart')
# 격자 추가
plt.grid()
# 그래프 보여주기
plt.show()
# 데이터 준비
x = [1, 2, 3, 4, 5]
y = [3, 5, 4, 6, 8]
# 산점도 그리기
plt.scatter(x, y)
# 그래프 보여주기
plt.show()
import numpy as np
n = 50
x = np.random.rand(n) # n개의 난수 array
y = np.random.rand(n)
area = (10 * np.random.rand(n)) ** 2
colors = np.random.rand(n)
plt.scatter(x, y, s=area, cmap='plasma', c=colors, alpha=0.5 )
plt.show()
# 데이터 준비
sizes = [30, 40, 20, 10]
labels = ['A', 'B', 'C', 'D']
# 파이 차트 그리기
plt.pie(sizes, labels=labels)
# 그래프 보여주기
plt.show()
ratio = [34, 32, 16, 18]
labels = ['a', 'b', 'c', 'd']
wedgeprops = {'width': 0.7, 'edgecolor': 'k', 'linewidth': 1}
plt.pie(ratio, labels=labels, autopct="%.1f%%", explode=[0, 0.1, 0, 0.1], colors=['red', 'yellow', 'green', 'blue'], wedgeprops=wedgeprops)
plt.show()