1

基本上我有一些数据,我从中制作了直方图。没有太大的困难,只需使用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

现在我无处可去,如果我尝试绘制filt它会给我错误

4

1 回答 1

1

我会说,用 制作直方图np.histogaram,然后应用 Sobel 过滤器plt.hist

>>> n, bins=histogram(q, bins=30)
>>> bin_updated=[item for item, jtem in zip(bins, n) if do_Sobel_stuff_on(jtem)]
>>> plt.hist(data, bins=bin_updated)

好的,基本上,您要使用.set_height()方法:

>>> a=range(1000)
>>> n,b,p=plt.hist(a)
>>> p[0]
<matplotlib.patches.Rectangle object at 0x026E1070>
>>> p[0].get_height()
100
>>> p[0].set_height(19)
>>> plt.show()
>>> n_adjusted #your new n
>>> for i1, i2 in zip(p, n_adjusted):
        i1.set_height(i2)

在此处输入图像描述

于 2013-09-15T07:48:08.157 回答