Plotting Histogram with given x and y values

52,740

From your plot and initial code, I could gather that you already have the bin and the frequency values in 2 vectors x and y. In this case, you will just plot a bar chart of these values, as opposed to the histogram using the plt.hist command. You can do the following:

import matplotlib.pyplot as plt

x = (1,2,3,4,5)
y = (1,2,3,4,5)

plt.bar(x,y,align='center') # A bar chart
plt.xlabel('Bins')
plt.ylabel('Frequency')
for i in range(len(y)):
    plt.hlines(y[i],0,x[i]) # Here you are drawing the horizontal lines
plt.show()

Bar chart with bins and values

Share:
52,740
krazzy
Author by

krazzy

Updated on August 01, 2022

Comments

  • krazzy
    krazzy almost 2 years

    I am trying to plot a histogram that lines up every x value with the y value on the plot. I have tried to use multiple resources, but unfortunately I wasn't able to find anything. This is the best way I could code to make a histogram.

    x = (1,2,3,4,5)
    y = (1,2,3,4,5)
    
    h=plt.hist(x,y)
    plt.axis([0, 6, 0, 6])
    plt.show()
    

    I want a graph that looks like the image below without those a's on x-axis:

    enter image description here