Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting Histogram with given x and y values

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

like image 380
krazzy Avatar asked Sep 12 '15 17:09

krazzy


People also ask

How do you plot a value on a histogram?

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.

What is the x and y axis on a histogram?

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.


1 Answers

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

like image 186
Sahil M Avatar answered Oct 08 '22 08:10

Sahil M