programing

플롯 눈금 수 감소

sourcejob 2023. 1. 17. 21:19
반응형

플롯 눈금 수 감소

그래프에 체크 표시가 너무 많아서 서로 부딪혀요.

진드기 수를 줄이려면 어떻게 해야 하나요?

예를 들어, 체크 표시가 있습니다.

1E-6, 1E-5, 1E-4, ... 1E6, 1E7

그리고 나는 단지 다음을 원한다.

1E-5, 1E-3, ... 1E5, 1E7

제가 한번 해봤는데LogLocator하지만 난 이걸 이해할 수 없었어

또는 matplotlib가 눈금 위치를 지정할 수 있도록 하면서 눈금 수를 설정하려는 경우(현재에만 해당)MaxNLocator)가 있습니다.

pyplot.locator_params(nbins=4)

이 방법에서는 아래 설명과 같이 특정 축을 지정할 수 있습니다.기본값은 둘 다입니다.

# To specify the number of ticks on both or any single axes
pyplot.locator_params(axis='y', nbins=6)
pyplot.locator_params(axis='x', nbins=10)

검색 결과에 이 페이지가 표시되는 경우:

fig, ax = plt.subplots()

plt.plot(...)

every_nth = 4
for n, label in enumerate(ax.xaxis.get_ticklabels()):
    if n % every_nth != 0:
        label.set_visible(False)

커스터마이즈 및 눈금 모양 문제를 해결하려면 matplotlib 웹 사이트의 로케이터 가이드를 참조하십시오.

ax.xaxis.set_major_locator(plt.MaxNLocator(3))

x축의 총 눈금 수를 3으로 설정하고 축 전체에 균등하게 분포시킵니다.

여기에 대한 좋은 튜토리얼도 있습니다.

축 물체에 대한 기능이 있습니다.

누군가가 아직 그것을 필요로 하고 있고, 여기에서는 아무것도 효과가 없기 때문에, 나는 생성된 플롯의 외관을 그대로 유지하면서, 틱 수를 정확히 N으로 고정하는 매우 간단한 방법을 생각해냈다.

import numpy as np
import matplotlib.pyplot as plt

f, ax = plt.subplots()
ax.plot(range(100))

ymin, ymax = ax.get_ylim()
ax.set_yticks(np.round(np.linspace(ymin, ymax, N), 2))

@raffael이 제시한 해결책은 간단하고 매우 도움이 됩니다.

표시되는 틱라벨은 원래 분포에서 샘플링된 값이 아니라 에 의해 반환된 배열의 인덱스에서 샘플링된 값입니다.np.linspace(ymin, ymax, N).

원래 눈금 레이블에서 N개의 값을 균등하게 표시하려면set_yticklabels()방법.다음은 정수 라벨이 있는 y축의 스니펫입니다.

import numpy as np
import matplotlib.pyplot as plt

ax = plt.gca()

ymin, ymax = ax.get_ylim()
custom_ticks = np.linspace(ymin, ymax, N, dtype=int)
ax.set_yticks(custom_ticks)
ax.set_yticklabels(custom_ticks)

N=3 눈금마다 하나의 눈금이 필요한 경우:

N = 3  # 1 tick every 3
xticks_pos, xticks_labels = plt.xticks()  # get all axis ticks
myticks = [j for i,j in enumerate(xticks_pos) if not i%N]  # index of selected ticks
newlabels = [label for i,label in enumerate(xticks_labels) if not i%N]

또는 을 사용하여fig,ax = plt.subplots():

N = 3  # 1 tick every 3
xticks_pos = ax.get_xticks()
xticks_labels = ax.get_xticklabels()
myticks = [j for i,j in enumerate(xticks_pos) if not i%N]  # index of selected ticks
newlabels = [label for i,label in enumerate(xticks_labels) if not i%N]

(오프셋을 조정할 수 있습니다).(i+offset)%N).

원하는 경우 균일한 진드기가 발생할 수 있습니다.myticks = [1, 3, 8].

그럼, 을 사용할 수 있습니다.

plt.gca().set_xticks(myticks)  # set new X axis ticks

또는 라벨도 교환하고 싶은 경우

plt.xticks(myticks, newlabels)  # set new X axis ticks and labels

축 한계값은 축 눈금 뒤에 설정해야 합니다.

마지막으로 임의의 눈금 세트만 그릴 수 있습니다.

mylabels = ['03/2018', '09/2019', '10/2020']
plt.draw()  # needed to populate xticks with actual labels
xticks_pos, xticks_labels = plt.xticks()  # get all axis ticks
myticks = [i for i,j in enumerate(b) if j.get_text() in mylabels]
plt.xticks(myticks, mylabels)

(비활성화)mylabels순서부여가 되어 있습니다.그렇지 않은 경우는, sortmyticks재주문)

xticks 함수 범위 기능과 함께 자동 반복

start_number = 0

end_number = len(사용하는 데이터)

step_number = 스트레이트에서 엔드까지의 스킵 수

회전 = 90도 기울이면 긴 진드기에 도움이 됩니다.

plt.xticks(range(start_number,end_number,step_number),rotation=90)

로그 스케일을 사용하는 경우 다음 명령을 사용하여 주눈금의 수를 고정할 수 있습니다.

import matplotlib.pyplot as plt

....

plt.locator_params(numticks=12)
plt.show()

은 「」로 되어 있습니다.numticks을 사용법

@bgamari를 locator_params() 「」는nticks파라미터는 로그 스케일을 사용할 때 오류를 발생시킵니다.

언급URL : https://stackoverflow.com/questions/6682784/reducing-number-of-plot-ticks

반응형