import matplotlib.pyplot as plt
import matplotlib
print(matplotlib.__version__)
3.3.2
import matplotlib.pyplot as plt
# marker='o'(점), '-'(선)
# marker 생략 가능
plt.plot([1,2,3,4,5], marker='o', linestyle="--") # '-' 선 : 기본값
plt.plot([11,21,31,41,51], "o") # '-' 선 : 기본값
plt.show()
x = range(0, 50)
y = range(0, 100,2)
plt.plot(x, y, 'o')
plt.show()
import numpy as np
ypoints = np.array([3, 3, 1, 20, 10, 30, 20]) # 7개의 y값 지정
plt.plot(ypoints, marker = 'o')
plt.show()
plt.plot(ypoints, marker = '*')
plt.show()
x = range(0, 30)
y = [ v * v for v in x ]
print(x)
print(y)
plt.plot(x, y, 'ro') # r:빨간색, o:점
plt.show()
range(0, 30) [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841]
문자 | 색상 |
---|---|
b | blue(파란색) |
g | green(녹색) |
r | red(빨간색) |
c | cyan(청록색) |
m | magenta(마젠타색) |
y | yelow(노란색) |
k | black(검은색) |
w | white(흰색) |
마커 | 의미 |
---|---|
o | circle(원) |
v | triangle_down(역 삼각형) |
^ | triangle_up(삼각형) |
s | square(네모) |
+ | plus(플러스) |
. | point(점) |
선 종류 | 설명 |
---|---|
'-' | Solid line |
':' | Dotted line |
'--' | Dashed line |
'-.' | Dashed/dotted line |
y = np.array([3, 8, 1, 10])
# ^:b => triangle_up(삼각형)| Dotted line | blue
plt.plot(y, '^:b') #
plt.show()
y = np.array([3, 3, 1, 20, 10])
plt.plot(y , 'o-', ms=15) # o(marker), -(line)
plt.show()
y = np.array([3, 3, 1, 20, 10])
plt.plot(y , marker = 'o', ms=15, mec = 'r')
plt.show()
plt.plot(y , marker = 'o', ms=15, mfc = 'r')
plt.show()
plt.plot(ypoints, marker = 'o', ms = 20, mec = '#4CAF50', mfc = '#4CAF50')
[<matplotlib.lines.Line2D at 0x7f857397f8e0>]
plt.plot(ypoints, linestyle = 'dashed')
[<matplotlib.lines.Line2D at 0x7f8573a08a60>]
plt.plot(ypoints, color = 'r')
plt.show()
### 선 굵기
y = np.array([3, 8, 1, 12, 5])
plt.plot(y, linewidth = '12.5')
[<matplotlib.lines.Line2D at 0x7f85737fffa0>]
fig = plt.figure()
ax1 = fig.add_subplot(2,1,1)
ax2 = fig.add_subplot(2,1,2)
x1 = np.array([0, 1, 2, 3])
y1 = np.array([13, 18, 11, 10])
x2 = np.array([0, 1, 2, 3])
y2 = np.array([32, 81, 11, 11])
ax1.plot(x1, y1)
ax2.plot(x2, y2)
plt.show()
fig = plt.figure(figsize=(10,5))
ax1 = fig.add_subplot(1,3,1) # 1행 3열 중에 첫번째
ax2 = fig.add_subplot(1,3,2) # 1행 3열 중에 두번째
ax3 = fig.add_subplot(1,3,3) # 1행 3열 중에 세번째
plt.show()
### plot, bar 그래프 그리기
x = range(0,15)
y = [v*v for v in x]
fig, axs = plt.subplots(2,2, figsize=(10,10))
axs[0,0].plot(x, y, 'o')
axs[0,1].plot(x, y, '-')
axs[1,0].bar(x, y)
axs[1,1].hist(x, y)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0.0, 2 * np.pi, 0.1)
sin_y = np.sin(x)
cos_y = np.cos(x)
plt.figure(figsize=(10,12))
fig, axs = plt.subplots(2)
axs[0].plot(x, sin_y, 'b--')
axs[1].plot(x, cos_y, 'r--')
plt.show()
<Figure size 720x864 with 0 Axes>
(실습2)
2행 2열의 그래프를 그려보자.
1행 1열: sin() 그래프
1행 2열: cos() 그래프 (표시 형식:빨간색 사각형)
2행 1열: tan() 그래프 (표시 형식:청록색 점)
2행 2열: arctan() 그래프 (표시 형식:노란색 원)
x = np.arange(10)
plt.plot(x, 1*x, label='y = %ix' % 1)
plt.plot(x, 2*x, label='y = %ix' % 2)
plt.legend()
plt.show()
x = np.arange(10)
list(x)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
for i in range(5):
plt.plot(x, i*x, label='y = %ix' % i)
plt.legend()
plt.show()
plt.figure(figsize=(15,10))
plt.subplot(3,3,1)
plt.plot(x, 1*x, label='y = %ix' % 1)
plt.legend(loc='upper left') # 2
plt.subplot(3,3,2)
plt.plot(x, 1*x, label='y = %ix' % 1)
plt.legend(loc='upper center') # 9
plt.subplot(3,3,3)
plt.plot(x, 1*x, label='y = %ix' % 1)
plt.legend(loc='upper right') # 1
plt.subplot(3,3,4)
plt.plot(x, 1*x, label='y = %ix' % 1)
plt.legend(loc='center left') # 6
plt.subplot(3,3,5)
plt.plot(x, 1*x, label='y = %ix' % 1)
plt.legend(loc='center') # 10
plt.subplot(3,3,6)
plt.plot(x, 1*x, label='y = %ix' % 1)
plt.legend(loc='center right') # 5
plt.subplot(3,3,7)
plt.plot(x, 1*x, label='y = %ix' % 1)
plt.legend(loc='lower left') # 3
plt.subplot(3,3,8)
plt.plot(x, 1*x, label='y = %ix' % 1)
plt.legend(loc='lower center') # 8
plt.subplot(3,3,9)
plt.plot(x, 1*x, label='y = %ix' % 1)
plt.legend(loc='lower right') # 4
<matplotlib.legend.Legend at 0x7f8573b66e80>
import IPython.display as display
from PIL import Image
display.display(Image.open('./plt_legend_0622.png'))