Skip to content Skip to sidebar Skip to footer

How To Add Specific Bin Width In Python Code

I am new in python so bear with me. I wrote a code to generate random numbers in python, then plotted it but did not know how to put the bin width in the code! my bin width should

Solution 1:

There is no way to directly set the bin width of a histogram plot. But this is not a big problem since you can compute the bins to match the desired bin width.

You may e.g. create an array between the minimum and maximum value of your data and a step size of 0.1. This array can be used as bins for the histogram.

import matplotlib.pyplot as plt
import numpy as np

data = np.random.randn(10000)*10

bins = np.arange(data.min(), data.max()+.1, 0.1)

plt.hist(data, bins=bins)

plt.show()

Post a Comment for "How To Add Specific Bin Width In Python Code"