When drawing a histogram with hist
of
matplotlib.pyplot``, correct the deviation between the x-axis scale and the center line of the bar by specifying range
. However, the code below is range
when counting in 1 increments.
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)
#If you do nothing
plt.hist(s, bins=bins)
plt.show()
#When modified
plt.hist(s, bins=bins, range=(s.min()-.5, s.max()+.5))
plt.show()