Graphical histogram with preset x and y values

I am trying to build a histogram that aligns each x value with the y value on the chart. I tried to use several resources, but unfortunately I could not find anything. This is the best way I could compile the code for creating 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 need a graph that looks like the image below, without the a symbol on the x axis:

enter image description here

+4
source share
1 answer

From your plot and initial code, I could understand that you already have a bit and frequency values ​​in 2 vectors x and y. In this case, you simply plot the histogram of these values, unlike 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 cells and values.

+8
source

Source: https://habr.com/ru/post/1607104/


All Articles