programing

Y축이 정수만 사용하도록 강제하는 방법

lovejava 2023. 8. 17. 20:31

Y축이 정수만 사용하도록 강제하는 방법

매트플롯립을 사용하여 히스토그램을 작성하고 있습니다.pyplot 모듈과 y축 레이블에 소수(0.5, 1.5, 1.5, 2.2 등)가 아닌 정수(예: 0, 1, 2, 3 등)만 표시되도록 강제하는 방법이 궁금합니다.

나는 지침 노트를 보고 있고 답이 matplotlib 주변 어딘가에 있다고 의심합니다.음모를 꾸미다ylim 하지만 지금까지 저는 y축의 최소값과 최대값을 설정하는 것만 찾을 수 있습니다.

def doMakeChart(item, x):
    if len(x)==1:
        return
    filename = "C:\Users\me\maxbyte3\charts\\"
    bins=logspace(0.1, 10, 100)
    plt.hist(x, bins=bins, facecolor='green', alpha=0.75)
    plt.gca().set_xscale("log")
    plt.xlabel('Size (Bytes)')
    plt.ylabel('Count')
    plt.suptitle(r'Normal Distribution for Set of Files')
    plt.title('Reference PUID: %s' % item)
    plt.grid(True)
    plt.savefig(filename + item + '.png')
    plt.clf()

다른 방법은 다음과 같습니다.

from matplotlib.ticker import MaxNLocator

ax = plt.figure().gca()
ax.yaxis.set_major_locator(MaxNLocator(integer=True))

Y-데이터가 있는 경우

y = [0., 0.5, 1., 1.5, 2., 2.5]

이 데이터의 최대값과 최소값을 사용하여 이 범위의 자연수 리스트를 만들 수 있습니다.예를들면,

import math
print range(math.floor(min(y)), math.ceil(max(y))+1)

수확량

[0, 1, 2, 3]

그런 다음 matplotlib을 사용하여 y 눈금 표시 위치(및 레이블)를 설정할 수 있습니다.음모를 꾸미다yticks:

yint = range(min(y), math.ceil(max(y))+1)

matplotlib.pyplot.yticks(yint)

이것은 나에게 효과가 있습니다.

import matplotlib.pyplot as plt
plt.hist(...

# make the y ticks integers, not floats
yint = []
locs, labels = plt.yticks()
for each in locs:
    yint.append(int(each))
plt.yticks(yint)

언급URL : https://stackoverflow.com/questions/12050393/how-to-force-the-y-axis-to-only-use-integers