基本上我有一些数据,我从中制作了直方图。没有太大的困难,只需使用matplotlib.pyplot.hist(data,bins=num)
但是,我想做一种 Sobel 边缘检测,基本上ith
直方图条/bin(无论行话是什么)变成-2*(i-1)th+0*(i)th+2*(i+1)th
我已经制定/发现你可以做到(我的数据在分栏的 txt 文件)
import matplotlib.pyplot as plt
alldata = np.genfromtxt('filename', delimiter=' ')
data = alldata[:,18]
n,bins,patches = plt.hist(data,bins=30)
哪个返回/给予
>>> n
array([3,0,3,3,6,1,...etc])
>>> bins
array([13.755,14.0298,14.3046,... etc])
>>> patches
<a list of 30 Patch objects>
在这里,我可以执行我的操作n
以获取我的 sobel 过滤的东西,(旁注:我只是在数组上迭代地执行此操作,有没有更简单的方法a = [-2,0,2]
?)
所以,我的问题和问题! 我不知道如何将结果重建为直方图或线图......并保持相同的水平轴bins
更新
这是我用来实现这一目标的代码。在此处下载数据
import numpy as np
import matplotlib.pyplot as plt
# ignore this, it is so it makes it easier to iterate over later.
filNM = 'S_MOS152_cut'
filID = filNM + '.txt'
nbins = 30
# extract the data from file
stars = np.genfromtxt(filID, delimiter=' ')
imag = stars[:,18]
# let's start the histogram dance
n,bins,patches = plt.hist(imag, bins=nbins)
# now apply the edge filter (manually for lack of a better way)
nnew=[0 for j in xrange(nbins)]
for i in range(0,len(n)):
if i==0:
nnew[i]=2*n[i+1]
elif i==len(n)-1:
nnew[i]=-2*n[i-1]
else:
nnew=-2*n[i-1]+2*n[i+1]
np.array(nnew)
# I do this because it now generates the same form
# output as if you just say >>> print plt.hist(imag, bins=nbins)
filt = nnew,bins,patches
print filt