달나라 노트

Python matplotlib : xaxis.set_visible, yaxis.set_visible, set_axis_off (x축 숨기기, y축 숨기기, 좌표 전체 숨기기) 본문

Python/Python matplotlib

Python matplotlib : xaxis.set_visible, yaxis.set_visible, set_axis_off (x축 숨기기, y축 숨기기, 좌표 전체 숨기기)

CosmosProject 2024. 3. 29. 19:48
728x90
반응형

 

 

 

x축을 숨기거나

y축을 숨기거나

아니면 그냥 좌표평면 자체를 숨겨버리는 기능을 알아봅시다.

 

 

import matplotlib.pyplot as plt

sub_plots = plt.subplots(2, 2)

fig = sub_plots[0]
graph = sub_plots[1]

fig.suptitle('Axis off test')
fig.tight_layout(pad=3)

graph[0][0].set_title('original')

graph[0][1].set_title('x axis off')
graph[0][1].xaxis.set_visible(False)

graph[1][0].set_title('y axis off')
graph[1][0].yaxis.set_visible(False)

graph[1][1].set_title('axis off')
graph[1][1].set_axis_off()


plt.show()

 

위 코드는 4개의 subplot을 그리는 코드이며 실행하면 아래와 같이 보입니다.

 

 

총 4개의 subplot이 있습니다.

 

왼쪽 위 그래프는 그냥 기본값 그래프입니다. x축 y축 그리고 그래프의 영역을 나타내는 사각형이 모두 보입니다.

 

오른 쪽 위 그래프는 x축을 숨김 처리 한 그래프 입니다. x축의 값들이 보이지 않습니다.

 

왼쪽 아래 그래프는 y축을 숨김 처리 한 그래프 입니다. y축의 값들의 보이지 않습니다.

 

오른쪽 아래 그래프는 모든 축을 숨긴 그래프 입니다. x축, y축, 그리고 그래프의 영역을 나타내는 사각형 선이 보이지 않습니다.

 

 

 

위는 subplot을 사용할 때의 예시였는데 subplot을 사용하지 않는 경우는 어떻게 해야할까요?

 



import matplotlib.pyplot as plt
import numpy as np

x = np.arange(10)
y = np.sin(x)

plt.plot(x, y)

# 현재 Axes 객체를 가져와 x축 숨기기
plt.gca().get_xaxis().set_visible(False)

# 현재 Axes 객체를 가져와 y축 숨기기
plt.gca().get_yaxis().set_visible(False)

plt.show()

 

subplot을 사용하지 않는 경우는

plt.gca().get_xaxis().set_visible(False)

plt.gca().get_yaxis().set_visible(False)

를 사용하여 x축 또는 y축의 표시 여부를 결정할 수 있습니다.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

728x90
반응형
Comments