본문 바로가기
파이썬

파이썬 Histogram Matplotlib

by º기록 2020. 10. 7.
반응형

그래서 약간의 문제가 있습니다. 이미 히스토그램 형식의 scipy에 데이터 세트가 있으므로 빈의 중심과 빈당 이벤트 수를 가지고 있습니다. 이제 플롯을 히스토그램으로 표시 할 수 있습니다. 난 그냥 해봤 어

bins, n=hist()

그러나 그것은 그것을 좋아하지 않았습니다. 권장 사항이 있습니까?

 

해결 방법

 

import matplotlib.pyplot as plt
import numpy as np

mu, sigma = 100, 15
x = mu + sigma * np.random.randn(10000)
hist, bins = np.histogram(x, bins=50)
width = 0.7 * (bins[1] - bins[0])
center = (bins[:-1] + bins[1:]) / 2
plt.bar(center, hist, align='center', width=width)
plt.show()

여기에 이미지 설명 입력

객체 지향 인터페이스도 간단합니다.

fig, ax = plt.subplots()
ax.bar(center, hist, align='center', width=width)
fig.savefig("1.png")

사용자 지정 (비상 수) Bin을 사용하는 경우 np.diff 를 사용하여 너비 계산을 전달하고 ax.bar 에 너비를 전달하고 ax를 사용할 수 있습니다. .set_xticks 를 사용하여 빈 가장자리에 레이블을 지정합니다.

import matplotlib.pyplot as plt
import numpy as np

mu, sigma = 100, 15
x = mu + sigma * np.random.randn(10000)
bins = [0, 40, 60, 75, 90, 110, 125, 140, 160, 200]
hist, bins = np.histogram(x, bins=bins)
width = np.diff(bins)
center = (bins[:-1] + bins[1:]) / 2

fig, ax = plt.subplots(figsize=(8,3))
ax.bar(center, hist, align='center', width=width)
ax.set_xticks(bins)
fig.savefig("/tmp/out.png")

plt.show()


 

참조 페이지 https://stackoverflow.com/questions/5328556

 

 

반응형

댓글