Matplotlib 에서 TickedStroke 생성하기

Beginner

This tutorial is from open-source community. Access the source code

소개

Matplotlib 에서 patheffects는 경로가 그려지는 방식을 변경하는 데 사용될 수 있습니다. TickedStroke는 틱 스타일로 선을 그리는 patheffect의 한 유형입니다. 이 튜토리얼은 Matplotlib 에서 TickedStroke 를 생성하는 단계를 안내합니다.

VM 팁

VM 시작이 완료된 후, 왼쪽 상단을 클릭하여 Notebook 탭으로 전환하여 실습을 위해 Jupyter Notebook에 접근하십시오.

때로는 Jupyter Notebook 이 로딩을 완료하는 데 몇 초 정도 기다려야 할 수 있습니다. Jupyter Notebook 의 제한 사항으로 인해 작업의 유효성 검사는 자동화될 수 없습니다.

학습 중에 문제가 발생하면 Labby 에게 문의하십시오. 세션 후 피드백을 제공해주시면 문제를 신속하게 해결해 드리겠습니다.

경로에 TickedStroke 적용하기

이 단계에서는 경로에 TickedStroke 를 적용합니다.

import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.path as path
import matplotlib.patheffects as patheffects

fig, ax = plt.subplots(figsize=(6, 6))
path = path.Path.unit_circle()
patch = patches.PathPatch(path, facecolor='none', lw=2, path_effects=[
    patheffects.withTickedStroke(angle=-90, spacing=10, length=1)])

ax.add_patch(patch)
ax.axis('equal')
ax.set_xlim(-2, 2)
ax.set_ylim(-2, 2)

plt.show()

이 코드는 TickedStroke path effect 를 가진 단위 원을 생성합니다.

선에 TickedStroke 적용하기

이 단계에서는 선에 TickedStroke 를 적용합니다.

fig, ax = plt.subplots(figsize=(6, 6))
ax.plot([0, 1], [0, 1], label="Line",
        path_effects=[patheffects.withTickedStroke(spacing=7, angle=135)])

nx = 101
x = np.linspace(0.0, 1.0, nx)
y = 0.3*np.sin(x*8) + 0.4
ax.plot(x, y, label="Curve", path_effects=[patheffects.withTickedStroke()])

ax.legend()

plt.show()

이 코드는 TickedStroke path effect 를 가진 선과 곡선을 생성합니다.

등고선 플롯에 TickedStroke 적용하기

이 단계에서는 등고선 플롯에 TickedStroke 를 적용합니다.

fig, ax = plt.subplots(figsize=(6, 6))

nx = 101
ny = 105

## Set up survey vectors
xvec = np.linspace(0.001, 4.0, nx)
yvec = np.linspace(0.001, 4.0, ny)

## Set up survey matrices.  Design disk loading and gear ratio.
x1, x2 = np.meshgrid(xvec, yvec)

## Evaluate some stuff to plot
obj = x1**2 + x2**2 - 2*x1 - 2*x2 + 2
g1 = -(3*x1 + x2 - 5.5)
g2 = -(x1 + 2*x2 - 4.5)
g3 = 0.8 + x1**-3 - x2

cntr = ax.contour(x1, x2, obj, [0.01, 0.1, 0.5, 1, 2, 4, 8, 16],
                  colors='black')
ax.clabel(cntr, fmt="%2.1f", use_clabeltext=True)

cg1 = ax.contour(x1, x2, g1, [0], colors='sandybrown')
plt.setp(cg1.collections,
         path_effects=[patheffects.withTickedStroke(angle=135)])

cg2 = ax.contour(x1, x2, g2, [0], colors='orangered')
plt.setp(cg2.collections,
         path_effects=[patheffects.withTickedStroke(angle=60, length=2)])

cg3 = ax.contour(x1, x2, g3, [0], colors='mediumblue')
plt.setp(cg3.collections,
         path_effects=[patheffects.withTickedStroke(spacing=7)])

ax.set_xlim(0, 4)
ax.set_ylim(0, 4)

plt.show()

이 코드는 TickedStroke path effect 를 가진 등고선 플롯을 생성합니다.

눈금의 방향/측면

이 단계에서는 눈금의 측면을 변경합니다.

fig, ax = plt.subplots(figsize=(6, 6))
line_x = line_y = [0, 1]
ax.plot(line_x, line_y, label="Line",
        path_effects=[patheffects.withTickedStroke(spacing=7, angle=135)])

ax.plot(line_x, line_y, label="Opposite side",
        path_effects=[patheffects.withTickedStroke(spacing=7, angle=-135)])

ax.legend()
plt.show()

이 코드는 양쪽에 눈금이 있는 선을 생성합니다.

요약

이 튜토리얼에서는 Matplotlib 에서 TickedStroke 를 생성하는 방법을 보여주었습니다. 이 단계를 따르면 경로, 선 및 등고선 플롯에 TickedStroke 를 적용할 수 있습니다. 또한 눈금의 방향/측면을 조정할 수 있습니다.