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:
To create a histogram the first step is to create bin of the ranges, then distribute the whole range of the values into a series of intervals, and count the values which fall into each of the intervals. Bins are clearly identified as consecutive, non-overlapping intervals of variables. The matplotlib. pyplot.
Parts of a Histogram The title: The title describes the information included in the histogram. X-axis: The X-axis are intervals that show the scale of values which the measurements fall under. Y-axis: The Y-axis shows the number of times that the values occurred within the intervals set by the X-axis.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With