python - 如何使用标记将直方图转换为简单图?

标签 python matplotlib

我绘制了一个直方图。但我想使用它的数据来绘制一个简单的图表,使用标记显示数据点。但为此我需要 x 轴和 y 轴上的值。对于 y 轴,我可以在下面的代码中使用 n,但是 x 轴值是多少?我无法选择 bin 作为 x 轴,因为它返回 bin 边缘。

x= np.array([0,0,0,0,0,1,1,1,2,2,1,5,5,4,4.3,10])   
n,bins,patches=plt.hist(x,bins=5,density=True)
print("n is: ",n)
plt.xlim([0,6])
plt.xlabel('s')
plt.ylabel('P(s)')`
#Result
#n is:  [0.28125, 0.0625, 0.125, 0.,0.03125]

#Simple graph:
#plt.plot(?,n,marker='.')

enter image description here enter image description here

最佳答案

你在寻找这样的东西吗?

x= np.array([0,0,0,0,0,1,1,1,2,2,1,5,5,4,4.3,10])
n,bins,patches=plt.hist(x,bins=5,density=True, alpha=0.6)

plt.xlabel('s')
plt.ylabel('P(s)')
plt.scatter(bins[:-1]+ 0.5*(bins[1:] - bins[:-1]), n, marker='o', c='red', s=40, alpha=1)


enter image description here

关于python - 如何使用标记将直方图转换为简单图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56885394/

相关文章:

python - matshow 的问题

python - 用python解析xml中的CDATA

python - 字典未引用的字符串?

python - Pygame 能在图形方面做哪些 wxPython 做不到的事情?

python - Matplotlib fiddle 图在同一列上重叠

python - 与从离散数据插值的曲线相切

python - 调用变量时运行进程

python - 如何点击以下网站的按钮?

python-2.7 - 如何从 matplotlib 向 FuncAnimation 添加时间控制面板

python - 我如何在 matplotlib 中设置小刻度的位置