1

I am aware of some similar questions but I have tried the code on them and it does not work in my environment. So I have a large list of data (floating point numbers) and I want to plot it as a histogram. Although I can plot absolute frequency easily, I would like to plot relative frequency (fractions/percentages on the ordinate, i.e. y-axis). My code is:

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
#1 - n, bins, patches = ax.hist(zdata, bins=ceil(min-max), normed=1, cumulative=0)
#2 - n, bins, patches = ax.hist(zdata, weights=np.zeros_like(data) + 1. / data.size)
ax.set_xlabel('Atomic z coordinate', size=ceil(min-max))
ax.set_ylabel('Relative Frequency')
ax.legend
plt.show()

I have noticed that python asks for the bin count as an integer, but I know that I use 70 so that is easy to fix. My more urgent question is, when I try both commands that I have commented out in this snippet, neither works. Case 1 does not raise an error but when I finish the commands and call show() the figure does not appear. Case 2 raises a NameError and complains that it does not know what "data" is. I followed the template in other questions and am not sure myself what "data" is; my array (list) of values is zdata. Also, is there a difference between bins and patches?

I am using python 2.7.3 and the IDE is Spyder 2.1.11. Many thanks and I apologize if this is very similar to past questions.

4

1 回答 1

1

该命令np.zeros_like(data)意味着您将使用与数组相同长度和数据类型的零填充数组data。如果要使用此命令,则需要定义一个名为的数组data(这NameError意味着您正在尝试使用尚未定义的变量名。),或者您想替换datazdatasince zdataexists 而我猜您希望权重与输入数据的长度相同。

笔记

更好的写法:

np.zeros_like(data) + 1. / data.size

会是这个

np.ones_like(data) / data.size
于 2013-06-07T17:54:53.933 回答