Plotting Histogram with given x and y values

╄→尐↘猪︶ㄣ 提交于 2019-12-23 09:05:42

问题


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:


回答1:


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()



来源:https://stackoverflow.com/questions/32541659/plotting-histogram-with-given-x-and-y-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!