본문 바로가기

Programing Language/Python

파이썬을 이용한 CDF, PDF 그리기 (편집중)

반응형

이 글을 읽으면 도움이 많이 된다..!


https://mycourses.aalto.fi/pluginfile.php/146910/mod_resource/content/1/binning_tutorial.pdf



1. pdf log binning 해서 plot하기



data = 넣어주기 # data 는 1-D list 형태로


maxbin = math.ceil(np.log10(max(data)))

hist, bins = np.histogram(data

                          , bins=np.logspace(0, maxbin, num=nbins, endpoint=True, base=10.0, dtype=None)

                          , normed=False

   )

bin_centers = (bins[1:]+bins[:-1])*0.5 # histogram에서 bin의 위치 조정해주기

delt = bins[1:] - bins[:-1] # 각각의 bin length로 나눠주기기 위해 길이 구하기

hist = hist / delt /sum(hist) # hist를 확률로 구하고 구간 길이로 나눠주기

plt.scatter(bin_centers, hist ,size ,'C1', alpha=alp, label='넣어주세요')


2. cdf 그리기 (정확히는 1-CDF) for power-law plotting)

nbins = 30 : binning 개수

alp = 0.8 # 투명도; opacity

ymin = 1.0 # 맨처음에만 넣어주면 됨 


# 그래프를 여러개 그릴 땐 여기부터 계속 넣어주면 된다

data = 넣어주기 # data 는 1-D list 형태로

maxbin = math.ceil(np.log10(max(data)))

bins=np.logspace(0, maxbin, num=nbins, endpoint=True, base=10.0, dtype=None)

nsum = len(data)

cumul = []

for i in range(0,len(bins)):

    cumul.append(np.sum(data >= bins[i])/nsum)

    if cumul[-1] == 0.0:

        cumul.pop()

x, y = bins[:len(cumul)], cumul

plt.plot(x, y, 'C2-o', alpha=alp, label='compsoer') # 'C2-o'는 현재 palette의 색의 2번째를 o 마킹과 함께 그리기

if ymin > cumul[-1]/2:

    ymin = cumul[-1]/2 # 여러개 그릴 때 가장 작은 끝을 찾아서 그리기 위해


3. fitting part 이후


x = np.array(range(2,20))

y = (lambda y : 10**-0.8*x**(-2.0))(x)

plt.plot(x, y,'C4--', label='x**-2.0')


plt.ylim(ymin = ymin, ymax = 2) # 이건 맨 마지막에만 넣으면 됨

plt.savefig('./Results/파일이름.pdf', format='pdf', bbox_inches='tight') # bbox_inches 옵션은 글자가 밖으로 나가지 말라고

plt.show()




각자 응용하세요~~~

반응형