플롯 추가 사용자 정의
이제 x 축 눈금 레이블을 상단으로 이동했으므로 플롯을 더욱 시각적으로 매력적이고 유익하게 만들기 위해 추가로 사용자 정의해 보겠습니다.
고급 플롯 사용자 정의 기술
Matplotlib 는 플롯을 사용자 정의하기 위한 다양한 옵션을 제공합니다. 이러한 옵션 중 일부를 살펴보겠습니다.
## Create a new figure and a set of axes
fig, ax = plt.subplots(figsize=(10, 6))
## Generate some data with more points for a smoother curve
x = np.linspace(0, 2*np.pi, 100)
y1 = np.sin(x)
y2 = np.cos(x)
## Plot multiple datasets
ax.plot(x, y1, linewidth=2, color='blue', label='sin(x)')
ax.plot(x, y2, linewidth=2, color='red', label='cos(x)')
## Fill the area between curves
ax.fill_between(x, y1, y2, where=(y1 > y2), alpha=0.3, color='green', interpolate=True)
ax.fill_between(x, y1, y2, where=(y1 <= y2), alpha=0.3, color='purple', interpolate=True)
## Move the x-axis tick labels to the top
ax.tick_params(
axis='x',
top=True,
labeltop=True,
bottom=False,
labelbottom=False
)
## Customize tick labels
ax.set_xticks(np.arange(0, 2*np.pi + 0.1, np.pi/2))
ax.set_xticklabels(['0', 'π/2', 'π', '3π/2', '2π'])
## Add title and labels with custom styles
ax.set_title('Sine and Cosine Functions with Customized X-Axis Labels at the Top',
fontsize=14, fontweight='bold', pad=20)
ax.set_xlabel('Angle (radians)', fontsize=12)
ax.set_ylabel('Function Value', fontsize=12)
## Add a grid and customize its appearance
ax.grid(True, linestyle='--', alpha=0.7, which='both')
## Customize the axis limits
ax.set_ylim(-1.2, 1.2)
## Add a legend with custom location and style
ax.legend(loc='upper right', fontsize=12, framealpha=0.8)
## Add annotations to highlight important points
ax.annotate('Maximum', xy=(np.pi/2, 1), xytext=(np.pi/2, 1.1),
arrowprops=dict(facecolor='black', shrink=0.05, width=1.5),
fontsize=10, ha='center')
## Display the plot
plt.tight_layout() ## Adjust spacing for better appearance
plt.show()
print("We have created a fully customized plot with x-axis tick labels at the top!")
이 코드를 실행하면 다음과 같은 훨씬 더 정교하고 전문적인 모양의 플롯이 표시됩니다.
- 두 개의 곡선 (사인 및 코사인)
- 곡선 사이의 색상 영역
- 사용자 정의 눈금 레이블 (π 표기법 사용)
- 주요 기능을 가리키는 주석
- 더 나은 간격 및 스타일 지정
tick_params() 메서드를 사용하여 x 축 눈금 레이블을 상단에 유지하면서 추가 사용자 정의로 플롯을 향상시킨 방법을 확인하십시오.
사용자 정의 이해
추가한 주요 사용자 정의 중 일부를 살펴보겠습니다.
fill_between(): 사인 및 코사인 곡선 사이에 색상 영역을 만듭니다.
set_xticks() 및 set_xticklabels(): 눈금 위치 및 레이블을 사용자 정의합니다.
tight_layout(): 더 나은 모양을 위해 플롯 간격을 자동으로 조정합니다.
annotate(): 특정 지점을 가리키는 화살표와 함께 텍스트를 추가합니다.
- 다양한 요소에 대한 사용자 정의 글꼴, 색상 및 스타일
이러한 사용자 정의는 x 축 눈금 레이블을 상단에 유지하면서 시각적으로 매력적이고 유익한 플롯을 만드는 방법을 보여줍니다.