Matplotlib은 Python에서 정적, 애니메이션 및 인터랙티브 시각화를 생성하기 위한 종합적인 라이브러리입니다.
Matplotlib은 쉬운 것은 더욱 쉽게, 어려운 것도 가능하게 합니다.
Matplotlib — Visualization with Python
pip install matplotlib
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rcParams['font.family'] = 'AppleGothic' # 윈도우에서는 'Malgun Gothic'
mpl.rcParams['axes.unicode_minus'] = False
plt.style.use('_mpl-gallery')
https://matplotlib.org/stable/plot_types/index
https://matplotlib.org/stable/plot_types/basic/plot.html#sphx-glr-plot-types-basic-plot-py
Signature:
plt.plot(
*args: 'float | ArrayLike | str',
scalex: 'bool' = True,
scaley: 'bool' = True,
data=None,
**kwargs,
) -> 'list[Line2D]'
Docstring:
Plot y versus x as lines and/or markers.
x
, y
기준으로 그래프를 그린다.fmt
옵션 목록Character | Description |
---|---|
'.' | 포인트 마커 |
',' | 픽셀 마커 |
'o' | 원형 마커 |
'v' | 아래쪽 삼각형 마커 |
'^' | 위쪽 삼각형 마커 |
'<' | 왼쪽 삼각형 마커 |
'>' | 오른쪽 삼각형 마커 |
'1' | 아래쪽 작은 삼각형 마커 |
'2' | 위쪽 작은 삼각형 마커 |
'3' | 왼쪽 작은 삼각형 마커 |
'4' | 오른쪽 작은 삼각형 마커 |
'8' | 팔각형 마커 |
's' | 사각형 마커 |
'p' | 오각형 마커 |
'P' | 플러스 (채워진) 마커 |
'*' | 별 마커 |
'h' | 육각형1 마커 |
'H' | 육각형2 마커 |
'+' | 플러스 마커 |
'x' | X 마커 |
'X' | X (채워진) 마커 |
'D' | 다이아몬드 마커 |
'd' | 얇은 다이아몬드 마커 |
`' | '` |
'_' | 수평선 마커 |
Character | Description |
---|---|
'-' | 실선 스타일 |
'--' | 대시선 스타일 |
'-.' | 대시-점선 스타일 |
':' | 점선 스타일 |
Character | Color |
---|---|
'b' | blue |
'g' | green |
'r' | red |
'c' | cyan |
'm' | magenta |
'y' | yellow |
'k' | black |
'w' | white |
# make data
x = np.linspace(-5, 5, 20)
y1 = x ** 3
y2 = 5 * x + 30
y3 = 4 * (x ** 2) - 20
y4 = -25 * x + 20
# plot
plt.xlim()
plt.plot(x, y1, '--g')
plt.plot(x, y2, ':b')
plt.plot(x, y3, '-.r')
plt.plot(x, y4)
# plt.xlim(-5, 5)
# plt.ylim(-10, 100)
# plt.xticks(np.arange(-5, 6))
# plt.yticks(np.arange(-100, 121, 20))
plt.grid(True) # plt.grid()
plt.show()
left, right = xlim() # return the current xlim
xlim((left, right)) # set the xlim to left, right
xlim(left, right) # set the xlim to left, right
xlim(right=3) # adjust the right leaving left unchanged
xlim(left=1) # adjust the left leaving right unchanged
locs, labels = xticks() # Get the current locations and labels.
xticks(np.arange(0, 1, step=0.2)) # Set label locations.
xticks(np.arange(3), ['Tom', 'Dick', 'Sue']) # Set text labels.
xticks([0, 1, 2], ['January', 'February', 'March'], rotation=20) # Set text labels and properties.
xticks([]) # Disable xticks.
Location String | Location Code |
---|---|
'best' (Axes only) | 0 |
'upper right' | 1 |
'upper left' | 2 |
'lower left' | 3 |
'lower right' | 4 |
'right' | 5 |
'center left' | 6 |
'center right' | 7 |
'lower center' | 8 |
'upper center' | 9 |
'center' | 10 |
Signature:
plt.figure(
num: 'int | str | Figure | SubFigure | None' = None,
figsize: 'tuple[float, float] | None' = None,
dpi: 'float | None' = None,
*,
facecolor: 'ColorType | None' = None,
edgecolor: 'ColorType | None' = None,
frameon: 'bool' = True,
FigureClass: 'type[Figure]' = <class 'matplotlib.figure.Figure'>,
clear: 'bool' = False,
**kwargs,
) -> 'Figure'
Docstring:
Create a new figure, or activate an existing figure.
# make data
x = np.linspace(-5, 5, 20)
y1 = x ** 3
y2 = 5 * x + 30
y3 = 4 * (x ** 2) - 20
y4 = -25 * x + 20
plt.figure("y=x^3")
plt.plot(x, y1, '--g')
plt.grid()
plt.figure("y=5x+30")
plt.plot(x, y2, ':b')
plt.grid()
plt.clf() # (x, y2) 그래프는 삭제됨
plt.figure("y=4x^2-20")
plt.plot(x, y3, '-.r')
plt.grid()
plt.figure("y=-25x+20")
plt.plot(x, y4)
plt.grid()
plt.clf() # (x, y4) 그래프는 삭제됨
plt.show()
# using the variable ax for single a Axes
fig, ax = plt.subplots()
# using the variable axs for multiple Axes
fig, axs = plt.subplots(2, 2)
axs[0, 0].plot(x, y1)
axs[0][1].plot(x, y2
# using tuple unpacking for multiple Axes
fig, (ax1, ax2) = plt.subplots(1, 2)
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
# make data
x = np.linspace(0, 10, 100)
y1 = 4 + 1 * np.sin(2 * x)
x2 = np.linspace(0, 10, 25)
y2 = 4 + 1 * np.sin(2 * x2)
y3 = 4 + 1 * np.cos(2 * x)
y4 = 4 + 1 * np.cos(2 * x2)
# plot
fig, axs = plt.subplots(2,2)
axs[0, 0].plot(x, y1, 'x', markeredgewidth=2)
axs[0][1].plot(x2, y2, linewidth=2.0)
axs[1, 0].plot(x, y3, 'o-', linewidth=2)
axs[1][1].plot(x2, y4)
for ax in axs:
for x in ax:
x.set(xlim=(0, 8), xticks=np.arange(1, 8), ylim=(0, 8), yticks=np.arange(1, 8))
plt.show()
index
: 1부터 시작하는 인덱스plt.subplots(nrows, ncols, index)
한번에 모든 subplot을 만드는 반면# make data
x = np.linspace(-5, 5, 20)
y1 = x ** 3
y2 = 5 * x + 30
y3 = 4 * (x ** 2) - 20
y4 = -25 * x + 20
# subplot
nrows, ncols = (2, 2)
plt.subplot(nrows, ncols, 1)
plt.plot(x, y1, '--g')
plt.subplot(nrows, ncols, 2)
plt.plot(x, y2, ':b')
plt.subplot(nrows, ncols, 3)
plt.plot(x, y3, '-.r')
plt.subplot(nrows, ncols, 4)
plt.plot(x, y4)
plt.show()
https://matplotlib.org/stable/plot_types/basic/bar.html#sphx-glr-plot-types-basic-bar-py
# make data:
ids = np.arange(4) + 1
member_ids = list(map(lambda x: f"m_{x:02d}", ids))
before_ex = [27, 35, 40, 33]
after_ex = [30, 38, 42, 37]
# plot
bar_width = 0.3
line_width = 1
plt.bar(ids, before_ex, label = 'before', width=bar_width, color = 'm', edgecolor="white", linewidth=line_width)
# plt.barh(ids, before_ex, label = 'before', height=bar_width, color = 'm', edgecolor="white", linewidth=line_width)
plt.bar(ids + bar_width, after_ex, label = 'after', width=bar_width, color = 'c', edgecolor="white", linewidth=line_width)
# plt.barh(ids + bar_width, after_ex, label = 'after', height=bar_width, color = 'c', edgecolor="white", linewidth=line_width)
plt.xticks(ids + bar_width, member_ids)
plt.xlabel('회원 ID')
plt.ylabel('윗몸일으키기 횟수')
plt.title('운동 시작 전과 후의 근지구력 변화 비교')
plt.legend()
plt.show()
https://matplotlib.org/stable/plot_types/stats/pie.html#sphx-glr-plot-types-stats-pie-py
fruit = ['사과', '바나나', '딸기', '오렌지', '포도']
result = [7, 6, 3, 2, 2]
# colors = plt.get_cmap('Blues')(np.linspace(0.2, 0.7, len(fruit)))
explode_value = (0.3, 0.1, 0.1, 0.1, 0.1) # the radius with which to offset each wedge.
plt.figure(figsize=(3,3))
plt.pie(result, labels=fruit, colors=['m', 'y', 'r', 'c', 'g'], autopct='%.0f%%', startangle=90, counterclock=False, explode=explode_value, shadow=True)
plt.title("과일 판매량 비율")
plt.legend(loc=4)
plt.show()
plt.get_cmap('Reds')
plt.get_cmap('Greens')
plt.get_cmap('Blues')
colors = plt.get_cmap('Blues')(np.linspace(0.2, 0.7, 5))
https://matplotlib.org/stable/plot_types/stats/hist_plot.html#sphx-glr-plot-types-stats-hist-plot-py
def round_score(x):
if x > 100:
return 100
elif x < 0:
return 0
else:
return round(x)
math_scores = list(map(lambda x: round_score(x), 10 * np.random.randn(100) + 70))
fig, ax = plt.subplots()
plt.hist(math_scores, bins=8, linewidth=0.5, edgecolor="white")
plt.xlabel('수학 점수')
plt.ylabel('frequency')
plt.title('수학 점수 histogram')
fig.set_size_inches(3, 3)
https://seaborn.pydata.org/generated/seaborn.boxplot.html
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('_mpl-gallery')
# make data:
np.random.seed(10)
D =np.random.normal((3, 5, 4), (1.25, 1.00, 1.25), (100, 3))
# plot
fig,ax =plt.subplots()
VP =ax.boxplot(D, positions=[2, 4, 6], widths=1.5, patch_artist=True,
showmeans=False, showfliers=False,
medianprops={"color": "white", "linewidth": 0.5},
boxprops={"facecolor": "C0", "edgecolor": "white",
"linewidth": 0.5},
whiskerprops={"color": "C0", "linewidth": 1.5},
capprops={"color": "C0", "linewidth": 1.5})
ax.set(xlim=(0, 8), xticks=np.arange(1, 8),
ylim=(0, 8), yticks=np.arange(1, 8))
plt.show()
import seaborn as sns
before_ex = np.random.randn(100) * 10 + 30
before_ex = list(map(lambda x: round(x), before_ex))
after_ex = list(map(lambda x: x + np.random.randint(0, 21), before_ex))
data = np.array([before_ex, after_ex]).transpose()
df = pd.DataFrame(data, columns=['before_ex', 'after_ex'])
sns.boxplot(df, orient="h", palette="Set2")
sample_size = 1000
base_normal_sample = np.random.randn(sample_size)
def transform_normal_sample(loc, scale):
return list(map(lambda x: round(x), loc + scale * base_normal_sample))
height = list(map(lambda x: round(x), 170 + 10 * base_normal_sample))
weight = list(map(lambda x: round(x), 70 + 5 * base_normal_sample))
weight = list(map(lambda x: x + np.random.randint(0, 21), weight)) # 오차 적용
fig, ax = plt.subplots()
fig.set_size_inches(3,3)
plt.scatter(height, weight, s=2)
ax.set(xlabel="키", ylabel="체중", title="체중과 키 산점도")
plt.show()