[Jupyter] [Python] Correct the deviation between the x-axis scale and the center line of the bar with hist of matplotlib (for counting in 1 increments)

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()

スクリーンショット 2020-12-15 午前9.03.02.png

Recommended Posts

[Jupyter] [Python] Correct the deviation between the x-axis scale and the center line of the bar with hist of matplotlib (for counting in 1 increments)
Google search for the last line of the file in Python
The story of returning to the front line for the first time in 5 years and refactoring Python Django
I measured the speed of list comprehension, for and while with python2.7.
Line graphs and scale lines in python
Align the number of samples between classes of data for machine learning with Python
About the X-axis notation of Matplotlib bar graphs
Summary of the differences between PHP and Python
The answer of "1/2" is different between python2 and 3
About the difference between "==" and "is" in python
Code for checking the operation of Python Matplotlib
Automate background removal for the latest portraits in a directory with Python and API
Build API server for checking the operation of front implementation with python3 and Flask
I created a stacked bar graph with matplotlib in Python and added a data label
[Python] Explain the difference between strftime and strptime in the datetime module with an example
Check the processing time and the number of calls for each process in python (cProfile)
Calculation of standard deviation and correlation coefficient in Python
Omit the decimal point of the graph scale in matplotlib
Difference between Ruby and Python in terms of variables
Visualize the range of interpolation and extrapolation with python