为什么我自己计算的直方图看起来与内置直方图不同?

Why does the histogram I calculate myself look different then the in-build one?

我有一个DataFrame,其中包含灰度图像的像素。它有两列: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()