给定x和y值绘制直方图 [英] Plotting Histogram with given x and y values

查看:155
本文介绍了给定x和y值绘制直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制一个直方图,该直方图将每个x值与y值对齐.我尝试使用多种资源,但不幸的是我什么也找不到.这是我可以编写直方图的最好方法.

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

我想要一个看起来像下面图像的图,而x轴上没有这些a:

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

推荐答案

从您的曲线图和初始代码中,我可以得知您已经在2个向量x和y中具有bin和频率值.在这种情况下,您将只绘制这些值的条形图,而不是使用plt.hist命令来绘制直方图.您可以执行以下操作:

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

这篇关于给定x和y值绘制直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆