我有一个数据帧,它包含一个灰色图像的像素。它有两列:n
表示像素属于哪个图像,pixel
表示像素有多暗。当我用
plt.figure()
ggplot(aes(x='pixel'), data=pixelDF) + \
geom_histogram(binwidth=8) + \
xlab('pixels') + \
ylab('') + \
ggtitle('Histogram of pixels') + \
scale_y_log() + \
facet_grid(y='n')
但当我第一次用
def my_historgram(to_histogram):
histogram = np.histogram(to_histogram, bins=32, range=(0, 255), weights=None, density=False)
return (histogram)
def get_pixel(df, i):
return (df.loc[df['n'] == i]['pixel'])
def hist_calc(hist):
return(np.log(hist) / sum(np.log(hist)))
imageNr = pixelDF['n'].drop_duplicates().tolist() hist, bin_edges = my_historgram(get_pixel(pixelDF, imageNr[0])) histograms = pd.DataFrame({
'binNr': range(len(hist)),
'binValue_' + str(imageNr[0]): pd.Series(hist_calc(hist))}).set_index('binNr') for i in imageNr[1:]:
hist, bin_edges = my_historgram(get_pixel(pixelDF, i))
histogram = pd.DataFrame({
'binNr': range(len(hist)),
'binValue_' + str(i): pd.Series(hist_calc(hist))}).set_index('binNr')
histograms = histograms.join(histogram) histograms = histograms.reset_index()
### Print new type of Histogram
plt.figure() plotDF = pd.melt(histograms, id_vars=['binNr'], var_name='imageNr', value_name='binValue')
ggplot(aes(x='factor(binNr)', weight='binValue'), data=plotDF) + \
geom_bar() + \
xlab('binNr') + \
ylab('') + \
ggtitle('Histograms of pixels') + \
facet_grid(y='imageNr')
我得到了完全不同的画面:
为什么?在第二张照片的处理过程中,我做错了什么
感谢“jeremycg”:他评论道“看起来你的版本把binNr当作了一个分类变量,需要排序——jeremycg在2小时前”
解决方法是:简单地去掉最后一个ggplot中的
factor()
相关问题 更多 >
编程相关推荐