Hyun Seup Jo
3주차 - MatplotLib 본문
In [74]:
import matplotlib.pyplot as plt
import numpy as np
In [75]:
x = range(100)
In [76]:
y = [value** 2 for value in x]
In [77]:
plt.plot(x, y)
Out[77]:
[<matplotlib.lines.Line2D at 0x22f88c0c250>]
In [78]:
x = np.arange(100)
In [79]:
y = list(map(lambda f: f ** 2, x))
In [80]:
plt.plot(x, y)
Out[80]:
[<matplotlib.lines.Line2D at 0x22f88b98b80>]
In [81]:
x_1 = range(100)
y_1 = [np.cos(value) for value in x_1]
plt.plot(x_1, y_1)
Out[81]:
[<matplotlib.lines.Line2D at 0x22f87dc20a0>]
In [82]:
x_2 = range(100)
y_2 = [np.sin(value) for value in x_2]
plt.plot(x_2, y_2)
plt.show()
In [83]:
plt.plot(x_1, y_1)
plt.plot(x_2, y_2)
plt.show()
In [84]:
plt.plot(y_1, x_1)
plt.show() # arccos함수
In [85]:
fig = plt.figure() # fiqure사이즈 반환
fig.set_size_inches(10, 5) # 사이즈 지정
ax_1 = fig.add_subplot(1, 2, 1) # 두개의 plot생성
ax_2 = fig.add_subplot(1, 2, 2) # 두개의 plot생성
ax_1.plot(x_1, y_1, c = 'b', ls = 'dashed') # 파란색
ax_2.plot(x_2, y_2, c = 'g', ls = 'dotted') # 초록색
plt.title('Two Lines')
plt.show()
In [86]:
plt.plot(x_1, y_1, c = 'r', ls = 'dotted', label = 'cos')
plt.plot(x_2, y_2, c = 'b', ls = 'dashed', label = 'sin')
plt.legend(shadow = True, fancybox = True, loc = 'lower right') # legend함수로 범례를 표시함
plt.show()
Matplotlib Graph¶
1 - Scatter¶
In [87]:
data_1 = np.random.rand(512, 2) # 512 * 2 크기의 random array생성
data_2 = np.random.rand(512, 2) # 512 * 2 크기의 random array생성
plt.scatter(data_1[:, 0], data_1[:,1], c = 'b', marker = 'x')
plt.scatter(data_2[:, 0], data_2[:, 1], c = 'r', marker = '^')
plt.show()
In [88]:
N = 50
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.rand(N)
area = np.pi * (15 * np.random.rand(N)) ** 2
plt.scatter(x, y, s = area, c = colors, alpha = 0.5)
plt.show()
In [89]:
data = [[5., 25., 50., 20.], [4., 23., 51., 17], [6., 22., 52., 19.]]
X = np.arange(4)
plt.bar(X + 0.00, data[0], color = 'b', width = 0.25)
plt.bar(X + 0.25, data[1], color = 'r', width = 0.25)
plt.bar(X + 0.50, data[2], color = 'g', width = 0.25)
plt.xticks(X + 0.25, ('A','B','C','D'))
plt.show()
Matplotlib with Pandas¶
In [90]:
import pandas as pd
import matplotlib.pyplot as plt
In [91]:
df = pd.DataFrame({'CRIM':[0.0632, 0.02731, 0.02729, 0.03237, 0.06905],
'ZN':[18.0,0.0,0.0,0.0,0.0],
'INDUS':[2.31, 7.07, 7.07, 2.18, 2.18],
'CHAS':[0,0,0,0,0],
'NOX':[0.538,0.469,0.469,0.458,0.458],
'RM':[6.575, 6.421, 7.185, 6.998, 7.147],
'AGE':[65.2, 78.9, 61.1, 45.8, 54.2],
'DIS':[4.0900, 4.9671, 4.9671, 6.0622, 6.0622],
'RAD':[1,2,2,3,3],
'TAX':[296.0, 242.0, 242.0, 222.0, 222.0],
'PTRATIO':[15.3, 17.8, 17.8, 18.7, 18.7],
'B':[396.90, 396.90, 392.83, 394.63, 396.90],
'LSTAT':[4.98, 9.14, 4.03, 2.94, 5.33],
'MEDV':[24.0, 21.6, 34.7, 33.4, 36.2]})
In [92]:
fig = plt.figure()
ax = []
for i in range(1, 5):
ax.append(fig.add_subplot(2, 2, i))
ax[0].scatter(df['CRIM'],df['MEDV'])
ax[1].scatter(df['PTRATIO'],df['MEDV'])
ax[2].scatter(df['AGE'],df['MEDV'])
ax[3].scatter(df['NOX'],df['MEDV'])
plt.show() # data간 상관간계를 볼 때 scatter graph 이용
In [101]:
x = np.arange(100)
y = list(map(lambda f: f ** 2 , x))
plt.scatter(x, y, c = 'r', label = 'PRACTICE')
plt.legend()
plt.show()
'Machine Learning' 카테고리의 다른 글
4주차 - Build_Matrix (문제풀이) (0) | 2021.02.17 |
---|---|
4주차 - Data Cleansing (0) | 2021.02.16 |
3주차 - Pandas (2) (0) | 2021.02.09 |
3주차 - Pandas(1) (0) | 2021.02.04 |
3주차 - Numpy Assignment(문제 풀이) (0) | 2021.02.03 |