ax.hist不输出与np.histogram相同的输出(ax.hist not outputting the same as np.histogram)

我不确定这是否是一个错误,但我无法使用ax.hist生成直方图。 当我尝试使用ax.hist来绘制一张numpy.nd.array我会看到一张空白图。 当调查返回的列表和垃圾箱时,该列表全部为0,而不应该是这种情况。 当我使用numpy.histogram()输出值和分类列表时,我会看到正确的值,并能够使用ax.bar()绘制我需要的图。 另外,我还检查了使用skimage.expose.histogram ,它返回与numpy.histogram()相同的numpy.histogram()

为了完整skimage.io ,我使用skimage.io加载了以下图像,并尝试绘制直方图,以显示每个通道的颜色强度。 代码是:

import matplotlib.pyplot as plt import numpy as np import skimage from skimage import io, color img = io.imread(MY_IMAGE_PATH) print(type(img)) # numpy.nd.array print(img.shape) # (256, 256, 4) fig = plt.figure(figsize=(10, 10)) grid_size = (1, 1) ax1 = plt.subplot2grid(grid_size, (0, 0), rowspan=1, colspan=1) # hist, bins = skimage.exposure.histogram(img[:, :, 0]) # ax1.bar(bins, hist, width=1, color="red") n, bins, _ = ax1.hist(img[:, :, 0].ravel(), bins=256, range=(0.0, 1.0), color="white") # Red channel ax.set_facecolor((44/255, 53/255, 57/255)) # dark gray background values, np_bins = np.histogram(img[:, :, 0].ravel()

我正在加载的图片:

我有一个解决方法,但我想知道如果问题是与我的代码或如果这确实是一个错误?

I'm not sure if this is a bug or not, but I'm not able to generate a histogram using ax.hist. When I attempt to use ax.hist to plot a numpy.nd.array I'm presented with a blank graph. When investigating the returned list and bins, the list are all 0 which shouldn't be the case. When I output the list of values and bins using numpy.histogram() I'm presented with the correct values and am able to plot what I need using ax.bar(). Additionally, I've also checked using skimage.expose.histogram which returns the same as numpy.histogram()

for completeness, I'm loading the following image with skimage.io and attempting the plot the histograms showing the colour intensity of each channel. The code is:

import matplotlib.pyplot as plt import numpy as np import skimage from skimage import io, color img = io.imread(MY_IMAGE_PATH) print(type(img)) # numpy.nd.array print(img.shape) # (256, 256, 4) fig = plt.figure(figsize=(10, 10)) grid_size = (1, 1) ax1 = plt.subplot2grid(grid_size, (0, 0), rowspan=1, colspan=1) # hist, bins = skimage.exposure.histogram(img[:, :, 0]) # ax1.bar(bins, hist, width=1, color="red") n, bins, _ = ax1.hist(img[:, :, 0].ravel(), bins=256, range=(0.0, 1.0), color="white") # Red channel ax.set_facecolor((44/255, 53/255, 57/255)) # dark gray background values, np_bins = np.histogram(img[:, :, 0].ravel()

the image that I'm loading:

I have a work around but I would like to know if the problem is with my code or if this is indeed a bug?

最满意答案

在白色背景上绘制白色条纹会使它们不可见。 使用其他颜色,例如color="red" 。 如果直方图的值介于0和255之间,则将值的范围设置为(0,1)毫无意义。而是使用range=(0, 255) 。 Plotting white bars on a white background will make them invisible. Use some other color, e.g. color="red". Setting the range of values to be (0,1) makes no sense if the values to histogram are between 0 and 255. Rather use range=(0, 255).ax.hist不输出与np.histogram相同的输出(ax.hist not outputting the same as np.histogram)

我不确定这是否是一个错误,但我无法使用ax.hist生成直方图。 当我尝试使用ax.hist来绘制一张numpy.nd.array我会看到一张空白图。 当调查返回的列表和垃圾箱时,该列表全部为0,而不应该是这种情况。 当我使用numpy.histogram()输出值和分类列表时,我会看到正确的值,并能够使用ax.bar()绘制我需要的图。 另外,我还检查了使用skimage.expose.histogram ,它返回与numpy.histogram()相同的numpy.histogram()

为了完整skimage.io ,我使用skimage.io加载了以下图像,并尝试绘制直方图,以显示每个通道的颜色强度。 代码是:

import matplotlib.pyplot as plt import numpy as np import skimage from skimage import io, color img = io.imread(MY_IMAGE_PATH) print(type(img)) # numpy.nd.array print(img.shape) # (256, 256, 4) fig = plt.figure(figsize=(10, 10)) grid_size = (1, 1) ax1 = plt.subplot2grid(grid_size, (0, 0), rowspan=1, colspan=1) # hist, bins = skimage.exposure.histogram(img[:, :, 0]) # ax1.bar(bins, hist, width=1, color="red") n, bins, _ = ax1.hist(img[:, :, 0].ravel(), bins=256, range=(0.0, 1.0), color="white") # Red channel ax.set_facecolor((44/255, 53/255, 57/255)) # dark gray background values, np_bins = np.histogram(img[:, :, 0].ravel()

我正在加载的图片:

我有一个解决方法,但我想知道如果问题是与我的代码或如果这确实是一个错误?

I'm not sure if this is a bug or not, but I'm not able to generate a histogram using ax.hist. When I attempt to use ax.hist to plot a numpy.nd.array I'm presented with a blank graph. When investigating the returned list and bins, the list are all 0 which shouldn't be the case. When I output the list of values and bins using numpy.histogram() I'm presented with the correct values and am able to plot what I need using ax.bar(). Additionally, I've also checked using skimage.expose.histogram which returns the same as numpy.histogram()

for completeness, I'm loading the following image with skimage.io and attempting the plot the histograms showing the colour intensity of each channel. The code is:

import matplotlib.pyplot as plt import numpy as np import skimage from skimage import io, color img = io.imread(MY_IMAGE_PATH) print(type(img)) # numpy.nd.array print(img.shape) # (256, 256, 4) fig = plt.figure(figsize=(10, 10)) grid_size = (1, 1) ax1 = plt.subplot2grid(grid_size, (0, 0), rowspan=1, colspan=1) # hist, bins = skimage.exposure.histogram(img[:, :, 0]) # ax1.bar(bins, hist, width=1, color="red") n, bins, _ = ax1.hist(img[:, :, 0].ravel(), bins=256, range=(0.0, 1.0), color="white") # Red channel ax.set_facecolor((44/255, 53/255, 57/255)) # dark gray background values, np_bins = np.histogram(img[:, :, 0].ravel()

the image that I'm loading:

I have a work around but I would like to know if the problem is with my code or if this is indeed a bug?

最满意答案

在白色背景上绘制白色条纹会使它们不可见。 使用其他颜色,例如color="red" 。 如果直方图的值介于0和255之间,则将值的范围设置为(0,1)毫无意义。而是使用range=(0, 255) 。 Plotting white bars on a white background will make them invisible. Use some other color, e.g. color="red". Setting the range of values to be (0,1) makes no sense if the values to histogram are between 0 and 255. Rather use range=(0, 255).