반응형
matplotlib에서 x 축을 플롯의 맨 위로 이동
matplotlib의 히트 맵에 대한이 질문을 바탕으로 x 축 제목을 플롯의 맨 위로 이동하고 싶었습니다.
import matplotlib.pyplot as plt
import numpy as np
column_labels = list('ABCD')
row_labels = list('WXYZ')
data = np.random.rand(4,4)
fig, ax = plt.subplots()
heatmap = ax.pcolor(data, cmap=plt.cm.Blues)
# put the major ticks at the middle of each cell
ax.set_xticks(np.arange(data.shape[0])+0.5, minor=False)
ax.set_yticks(np.arange(data.shape[1])+0.5, minor=False)
# want a more natural, table-like display
ax.invert_yaxis()
ax.xaxis.set_label_position('top') # <-- This doesn't work!
ax.set_xticklabels(row_labels, minor=False)
ax.set_yticklabels(column_labels, minor=False)
plt.show()
그러나 matplotlib의 set_label_position (위에 언급 된대로)을 호출 하면 원하는 효과가없는 것 같습니다. 내 결과는 다음과 같습니다.
내가 뭘 잘못하고 있죠?
사용하다
ax.xaxis.tick_top()
이미지 상단에 눈금 표시를 배치합니다. 명령
ax.set_xlabel('X LABEL')
ax.xaxis.set_label_position('top')
눈금이 아닌 레이블에 영향을줍니다.
import matplotlib.pyplot as plt
import numpy as np
column_labels = list('ABCD')
row_labels = list('WXYZ')
data = np.random.rand(4, 4)
fig, ax = plt.subplots()
heatmap = ax.pcolor(data, cmap=plt.cm.Blues)
# put the major ticks at the middle of each cell
ax.set_xticks(np.arange(data.shape[1]) + 0.5, minor=False)
ax.set_yticks(np.arange(data.shape[0]) + 0.5, minor=False)
# want a more natural, table-like display
ax.invert_yaxis()
ax.xaxis.tick_top()
ax.set_xticklabels(column_labels, minor=False)
ax.set_yticklabels(row_labels, minor=False)
plt.show()
당신이 원하는 set_ticks_position
것보다 set_label_position
:
ax.xaxis.set_ticks_position('top') # the rest is the same
이것은 나에게 준다 :
tick_params 는 눈금 속성을 설정하는 데 매우 유용합니다. 레이블은 다음을 사용하여 맨 위로 이동할 수 있습니다.
ax.tick_params(labelbottom='off',labeltop='on')
진드기 (라벨 아님)가 상단과 하단 (상단뿐만 아니라)에 나타나게하려면 추가 마사지를해야합니다. 이 작업을 수행 할 수있는 유일한 방법은 unutbu의 코드를 약간 변경하는 것입니다.
import matplotlib.pyplot as plt
import numpy as np
column_labels = list('ABCD')
row_labels = list('WXYZ')
data = np.random.rand(4, 4)
fig, ax = plt.subplots()
heatmap = ax.pcolor(data, cmap=plt.cm.Blues)
# put the major ticks at the middle of each cell
ax.set_xticks(np.arange(data.shape[1]) + 0.5, minor=False)
ax.set_yticks(np.arange(data.shape[0]) + 0.5, minor=False)
# want a more natural, table-like display
ax.invert_yaxis()
ax.xaxis.tick_top()
ax.xaxis.set_ticks_position('both') # THIS IS THE ONLY CHANGE
ax.set_xticklabels(column_labels, minor=False)
ax.set_yticklabels(row_labels, minor=False)
plt.show()
산출:
참고 URL : https://stackoverflow.com/questions/14406214/moving-x-axis-to-the-top-of-a-plot-in-matplotlib
반응형
'IT story' 카테고리의 다른 글
동일한 SELECT 문에서 DISTINCT 및 ORDER BY를 사용하는 방법은 무엇입니까? (0) | 2020.08.23 |
---|---|
SP 시작 SSO와 IDP 시작 SSO의 차이점 (0) | 2020.08.23 |
간단한 "Hello World"스타일 프로그램이 Turbo C ++로 컴파일되지 않는 이유는 무엇입니까? (0) | 2020.08.23 |
프로그래밍 방식으로 Android의받은 편지함에서 SMS를 삭제하는 방법은 무엇입니까? (0) | 2020.08.23 |
복사 한 다음 이동하는 이유는 무엇입니까? (0) | 2020.08.23 |