matplotlib.pyplot
のhist
でヒストグラムを描画した時、x軸の目盛と棒の中心線のずれをrange
を指定する事で直します。ただし下のコードは1刻みでカウントする場合のrange
です。
import pandas as pd
import matplotlib.pyplot as plt
s = pd.Series([1,2,2,4,4,4,4])
bins = (s.max()-s.min()+1)
# 何もしない場合
plt.hist(s, bins=bins)
plt.show()
# 修正した場合
plt.hist(s, bins=bins, range=(s.min()-.5, s.max()+.5))
plt.show()