In [2]:
import matplotlib.pyplot as plt
#%matplotlib inline
plt.plot([1, 2, 3], [2, 4, 6])
plt.title("Hello plot")
plt.show()
In [ ]:
Figure와 Axes¶
In [3]:
# plt.figure()는 주로 figure의 크기를 조절하는 데 사용됨.
plt.figure(figsize=(10, 4)) # figure 크기가 가로 10, 세로 4인 Figure객체를 설정하고 반환함.
plt.plot([1, 2, 3], [2, 4, 6])
plt.title("Hello plot")
plt.show()
In [4]:
figure = plt.figure(figsize=(10, 4))
print(type(figure))
<class 'matplotlib.figure.Figure'>
<Figure size 720x288 with 0 Axes>
In [5]:
plt.figure(figsize=(8,6), facecolor='yellow')
plt.plot([1, 2, 3], [2, 4, 6])
plt.title("Hello plot")
plt.show()
In [6]:
ax = plt.axes()
print(type(ax))
<class 'matplotlib.axes._subplots.AxesSubplot'>
In [7]:
### pyplot에서 설정된 Figure와 Axes 객체를 함께 가져오기
fig, ax = plt.subplots()
print(type(fig), type(ax))
<class 'matplotlib.figure.Figure'> <class 'matplotlib.axes._subplots.AxesSubplot'>
여러개의 plot을 가지는 figure 설정¶
In [8]:
fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(10, 6))
In [9]:
import numpy as np
x_value = [1, 2, 3, 4]
y_value = [2, 4, 6, 8]
x_value = np.array([1, 2, 3, 4])
y_value = np.array([2, 4, 6, 8])
# 입력값으로 파이썬 리스트, numpy array 가능. x축값과 y축값은 모두 같은 크기를 가져야 함.
plt.plot(x_value, y_value)
Out[9]:
[<matplotlib.lines.Line2D at 0x1ddcea38ca0>]
In [10]:
df['y_value']
Out[10]:
0 2
1 4
2 6
3 8
Name: y_value, dtype: int64
In [11]:
import pandas as pd
df = pd.DataFrame({'x_value':[1, 2, 3, 4],
'y_value':[2, 4, 6, 8]})
# 입력값으로 pandas Series 및 DataFrame도 가능.
plt.plot(df['x_value'], df['y_value'])
Out[11]:
[<matplotlib.lines.Line2D at 0x1ddce8fa670>]
In [12]:
plt.plot(x_value, y_value, color='green')
Out[12]:
[<matplotlib.lines.Line2D at 0x1ddce818d30>]
In [13]:
# API 기반으로 시각화를 구현할 때는 함수의 인자들에 대해서 알고 있어야 하는 부작용(?)이 있음.
plt.plot(x_value, y_value, color='red', marker='o', linestyle='dashed', linewidth=2, markersize=12)
Out[13]:
[<matplotlib.lines.Line2D at 0x1ddcd019520>]
x축, y축에 축명을 텍스트로 할당. xlabel, ylabel 적용¶
In [14]:
plt.plot(x_value, y_value, color='red', marker='o', linestyle='dashed', linewidth=2, markersize=12)
plt.xlabel('x axis')
plt.ylabel('y axis')
plt.show()
x축, y축 틱값을 표현을 회전해서 보여줌. x축값이 문자열이고 많은 tick값이 있을 때 적용.¶
In [15]:
x_value = np.arange(1, 100)
y_value = 2*x_value
plt.plot(x_value, y_value, color='green')
plt.xlabel('x axis')
plt.ylabel('y axis')
plt.xticks(rotation=45)
#plt.yticks(rotation=45)
plt.title('Hello plot')
plt.show()
In [16]:
x_value = np.arange(0, 100)
y_value = 2*x_value
plt.plot(x_value, y_value, color='green')
plt.xlabel('x axis')
plt.ylabel('y axis')
plt.xticks(ticks=np.arange(0, 100,5), rotation=90)
plt.yticks(rotation=45)
plt.title('Hello plot')
plt.show()
xlim()은 x축값을 제한하고, ylim()은 y축값을 제한¶
In [17]:
x_value = np.arange(0, 100)
y_value = 2*x_value
plt.plot(x_value, y_value, color='green')
plt.xlabel('x axis')
plt.ylabel('y axis')
# x축값을 0에서 50으로, y축값을 0에서 100으로 제한.
plt.xlim(0, 50)
plt.ylim(0, 100)
plt.title('Hello plot')
plt.show()
범례를 설정하기¶
In [18]:
x_value = np.arange(1, 100)
y_value = 2*x_value
plt.plot(x_value, y_value, color='green', label='temp')
plt.xlabel('x axis')
plt.ylabel('y axis')
plt.legend()
plt.title('Hello plot')
plt.show()
matplotlib을 여러개의 plot을 하나의 Axes내에서 그릴 수 있음.¶
In [19]:
x_value_01 = np.arange(1, 100)
#x_value_02 = np.arange(1, 200)
y_value_01 = 2*x_value_01
y_value_02 = 4*x_value_01
plt.plot(x_value_01, y_value_01, color='green', label='temp_01')
plt.plot(x_value_01, y_value_02, color='red', label='temp_02')
plt.xlabel('x axis')
plt.ylabel('y axis')
plt.legend()
plt.title('Hello plot')
plt.show()
In [20]:
x_value_01 = np.arange(1, 10)
#x_value_02 = np.arange(1, 200)
y_value_01 = 2*x_value_01
y_value_02 = 4*x_value_01
plt.plot(x_value_01, y_value_01, color='red', marker='o', linestyle='dashed', linewidth=2, markersize=6, label='temp_01')
plt.bar(x_value_01, y_value_01, color='green', label='temp_02')
plt.xlabel('x axis')
plt.ylabel('y axis')
plt.legend()
plt.title('Hello plot')
plt.show()
Axes 객체에서 직접 작업.¶
In [21]:
figure = plt.figure(figsize=(10, 6))
ax = plt.axes()
ax.plot(x_value_01, y_value_01, color='red', marker='o', linestyle='dashed', linewidth=2, markersize=6, label='temp_01')
ax.bar(x_value_01, y_value_01, color='green', label='temp_02')
ax.set_xlabel('x axis')
ax.set_ylabel('y axis')
ax.legend() # set_legend()가 아니라 legend()임.
ax.set_title('Hello plot')
plt.show()
여러개의 subplots을 가지는 Figure를 생성하고 여기에 개별 그래프를 시각화¶
- nrows가 1일 때는 튜플로 axes를 받을 수 있음.
- nrows나 ncols가 1일때는 1차원 배열형태로, nrows와 ncols가 1보다 클때는 2차원 배열형태로 axes를 추출해야 함.
In [22]:
x_value_01 = np.arange(1, 10)
x_value_02 = np.arange(1, 20)
y_value_01 = 2 * x_value_01
y_value_02 = 2 * x_value_02
fig, (ax_01, ax_02) = plt.subplots(nrows=1, ncols=2, figsize=(12, 6))
ax_01.plot(x_value_01, y_value_01, color='red', marker='o', linestyle='dashed', linewidth=2, markersize=6, label='temp_01')
ax_02.bar(x_value_02, y_value_02, color='green', label='temp_02')
ax_01.set_xlabel('ax_01 x axis')
ax_02.set_xlabel('ax_02 x axis')
ax_01.legend()
ax_02.legend()
#plt.legend()
plt.show()
In [23]:
import numpy as np
x_value_01 = np.arange(1, 10)
x_value_02 = np.arange(1, 20)
y_value_01 = 2 * x_value_01
y_value_02 = 2 * x_value_02
fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(12, 6))
ax[0].plot(x_value_01, y_value_01, color='red', marker='o', linestyle='dashed', linewidth=2, markersize=6, label='temp_01')
ax[1].bar(x_value_02, y_value_02, color='green', label='temp_02')
ax[0].set_xlabel('ax[0] x axis')
ax[1].set_xlabel('ax[1] x axis')
ax[0].legend()
ax[1].legend()
#plt.legend()
plt.show()
In [24]:
import numpy as np
x_value_01 = np.arange(1, 10)
x_value_02 = np.arange(1, 20)
y_value_01 = 2 * x_value_01
y_value_02 = 2 * x_value_02
fig, ax = plt.subplots(nrows=2, ncols=2, figsize=(12, 6))
ax[0][0].plot(x_value_01, y_value_01, color='red', marker='o', linestyle='dashed', linewidth=2, markersize=6, label='temp_01')
ax[0][1].bar(x_value_02, y_value_02, color='green', label='temp_02')
ax[1][0].plot(x_value_01, y_value_01, color='green', marker='o', linestyle='dashed', linewidth=2, markersize=6, label='temp_03')
ax[1][1].bar(x_value_02, y_value_02, color='red', label='temp_04')
ax[0][0].set_xlabel('ax[0][0] x axis')
ax[0][1].set_xlabel('ax[0][1] x axis')
ax[1][0].set_xlabel('ax[1][0] x axis')
ax[1][1].set_xlabel('ax[1][1] x axis')
ax[0][0].legend()
ax[0][1].legend()
ax[1][0].legend()
ax[1][1].legend()
#plt.legend()
plt.show()
In [ ]:
In [ ]:
'파이썬 기본 > 그래프 기초' 카테고리의 다른 글
시각화 기초 _ 학교수업 때 배운 부분 (0) | 2022.10.25 |
---|