如何从数据列表中创建直方图 [英] How to make a histogram from a list of data
问题描述
/ usr / lib64 / python2。 6 / site-packages / matplotlib / backends / backend_gtk.py:621:DeprecationWarning:使用新的小部件gtk.Tooltip
pre>
self.tooltips = gtk.Tooltips()
追溯(最近的最后一次调用):
文件vector_final,第42行,< module>
plt.hist(data,num_bins)
文件/usr/lib64/python2.6/site-packages/matplotlib/pyplot.py,行2008,在hist
ret = ax .class(x,bins,range,normed,weight,cumulative,bottom,histtype,align,orientation,rwidth,log,** kwargs)
文件/usr/lib64/python2.6/site-packages/ matplotlib / axes.py,第7098行,在hist
w = [None] * len(x)
TypeError:len()of unsized object
我的代码是:
#!/ usr / bin / pythonl = []
with open(testdata)as f:
line = f.next()
f.next()#skip headers
nat = int(line.split()[0])
print nat
f中的行:
如果line.strip():
if line.strip():
l.append(map(float,line.split()[1:]))
b = 0
a = 1
在范围(53)中:
一个范围(b + 1,54):
import operator
import matplotlib.pyplot as plt
import numpy a s np
vector1 =(l [b] [0],l [b] [1],l [b] [2])
vector2 =(l [a] [0 ],l [a] [1],l [a] [2])
x = vector1
y = vector2
vector3 = list(np.array(x) - np .array(y))
dotProduct = reduce(operator.add,map(operator.mul,vector3,vector3))
dp = dotProduct **。5
print dp
data = dp
num_bins = 200#< - 直方图的数量块
plt.hist(data,num_bins)
plt。 show()
但是,让我知道错误的代码是我添加的新增功能,最后一部分,转载如下:
data = dp
num_bins = 200#< - 直方图
plt.hist(data,num_bins)
plt.show()
解决方案
有任何想法如何使200个均匀分隔的箱子,并且
您的程序将数据存储在适当的箱子中?
例如,您可以将NumPy的
arange
用于固定的bin大小(或Python的标准范围对象),NumPy的linspace
用于均匀间隔的箱子。以下是我的 matplotlib图库的两个简单示例。
固定垃圾桶大小
import numpy as np
import random
从matplotlib import pyplot as plt
data = np.random.normal(0,20,1000)
#固定bin大小$ b $ b bins = np.arange(-100,100,5 )#fixed bin size
plt.xlim([min(data)-5,max(data)+5])
plt.hist(data,bins = bin ,alpha = 0.5)
plt.title('Random Gaussian data(fixed bin size)')
plt.xlabel('variable X(bin size = 5)')
plt.ylabel ('count')
plt.show()
固定数量的
import numpy as np
import random
import math
from matplotlib import pyplot as plt
data = np.random.normal(0,20,1000)
b ins = np.linspace(math.ceil(min(data)),
math.floor(max(data)),
20)#固定数量的bin
plt .xlim([min(data)-5,max(data)+5])
plt.hist(data,bins = bins,alpha = 0.5)
plt.title随机高斯数据(固定数量的bin)')
plt.xlabel('变量X(20个均匀分隔的空格)')
plt.ylabel('count')
plt.show()
Well I think matplotlib got downloaded but with my new script I get this error:
/usr/lib64/python2.6/site-packages/matplotlib/backends/backend_gtk.py:621: DeprecationWarning: Use the new widget gtk.Tooltip self.tooltips = gtk.Tooltips() Traceback (most recent call last): File "vector_final", line 42, in <module> plt.hist(data, num_bins) File "/usr/lib64/python2.6/site-packages/matplotlib/pyplot.py", line 2008, in hist ret = ax.hist(x, bins, range, normed, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, **kwargs) File "/usr/lib64/python2.6/site-packages/matplotlib/axes.py", line 7098, in hist w = [None]*len(x) TypeError: len() of unsized object
And my code is: #!/usr/bin/python
l=[] with open("testdata") as f: line = f.next() f.next()# skip headers nat = int(line.split()[0]) print nat for line in f: if line.strip(): if line.strip(): l.append(map(float,line.split()[1:])) b = 0 a = 1 for b in range(53): for a in range(b+1,54): import operator import matplotlib.pyplot as plt import numpy as np vector1 = (l[b][0],l[b][1],l[b][2]) vector2 = (l[a][0],l[a][1],l[a][2]) x = vector1 y = vector2 vector3 = list(np.array(x) - np.array(y)) dotProduct = reduce( operator.add, map( operator.mul, vector3, vector3)) dp = dotProduct**.5 print dp data = dp num_bins = 200 # <- number of bins for the histogram plt.hist(data, num_bins) plt.show()
But the code thats getting me the error is the new addition that I added which is the last part, reproduced below:
data = dp num_bins = 200 # <- number of bins for the histogram plt.hist(data, num_bins) plt.show()
解决方案do you have any idea how to make 200 evenly spaced out bins, and have your program store the data in the appropriate bins?
You can, for example, use NumPy's
arange
for a fixed bin size (or Python's standard range object), and NumPy'slinspace
for evenly spaced bins. Here are 2 simple examples from my matplotlib galleryFixed bin size
import numpy as np import random from matplotlib import pyplot as plt data = np.random.normal(0, 20, 1000) # fixed bin size bins = np.arange(-100, 100, 5) # fixed bin size plt.xlim([min(data)-5, max(data)+5]) plt.hist(data, bins=bins, alpha=0.5) plt.title('Random Gaussian data (fixed bin size)') plt.xlabel('variable X (bin size = 5)') plt.ylabel('count') plt.show()
Fixed number of bins
import numpy as np import random import math from matplotlib import pyplot as plt data = np.random.normal(0, 20, 1000) bins = np.linspace(math.ceil(min(data)), math.floor(max(data)), 20) # fixed number of bins plt.xlim([min(data)-5, max(data)+5]) plt.hist(data, bins=bins, alpha=0.5) plt.title('Random Gaussian data (fixed number of bins)') plt.xlabel('variable X (20 evenly spaced bins)') plt.ylabel('count') plt.show()
这篇关于如何从数据列表中创建直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!