Canuck Canuck -4 years ago 180
Python Question

Cannot get histogram to show separated bins with vertical lines

Annoying strange problem and I have not been able to find a solution on this site yet (although the question has popped up)

I am trying to make a histogram where the bins have the 'bar style' where vertical lines separate each bin but no matter what I change the histtype constructor to I get a step filled histogram.

Here is my code. Note I am using jupyter notebook installed via anaconda with python version 2.7.6

import numpy as np
import matplotlib.pyplot as plt

x = np.random.rand((100))
bins = np.linspace(0, 2, 40)
plt.title('Relative Amplitude',fontsize=30)
plt.xlabel('Random Histogram')
plt.ylabel('Frequency',fontsize=30)
plt.hist(x, bins, alpha=0.5, histtype='bar')

plt.legend(loc='upper right',fontsize=30)
plt.xticks(fontsize = 20)
plt.yticks(fontsize = 20)
plt.show()


Thats it and I get a step filled diagram with no vertical lines separating the bars. What is annoying is that I didn't have this problem awhile ago, something clearly has changed and I don't know what.I have tried histype='barstacked' as well. Thank you kindly for your help

enter image description here

Answer Source

Using your example:

import numpy as np
import matplotlib.pyplot as plt

x = np.random.rand((100)) 
bins = np.linspace(0, 2, 40)
plt.title('Relative Amplitude',fontsize=30)
plt.xlabel('Random Histogram')
plt.ylabel('Frequency',fontsize=30)
plt.hist(x, bins, alpha=0.5, histtype='bar', ec='black')

plt.legend(loc='upper right',fontsize=30)
plt.xticks(fontsize = 20) 
plt.yticks(fontsize = 20) 
plt.show() 

Which produces the following image:

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download