Search code examples
pythonmatplotlibhistogram

How to add edge color to the histogram bins


I am currently trying to render a histogram using Python's matplotlib. I'm having difficulty drawing vertical lines between each of the bins.

Here is my current code

plt.figure(figsize=[10, 5])

array = np.random.normal(loc=0, scale=1, size=100)
plt.hist(array, bins=25, color='#0504AA', alpha=0.5)

plt.grid(axis='x', alpha=0.5)
plt.grid(axis='y', alpha=0.5)

plt.xlabel('Value',     fontsize=12.5)
plt.ylabel('Frequency', fontsize=12.5)

plt.xticks(fontsize=12.5)
plt.yticks(fontsize=12.5)

plt.title('Histogram Distribution', fontsize=12.5)
plt.show()

Here is the current output

enter image description here

Here is the desired output

enter image description here


Solution

  • Try this:

    plt.hist(array, bins=25, color='#0504AA', alpha=0.5,edgecolor ="black", linewidth=2)
    

    https://i.sstatic.net/6qGpu.png