如何将多个直方图保存到不同的(单独的)文件中?

How do I save multiple histograms to different (separate) files?

我正在尝试将一堆直方图保存为图像。这就是我到目前为止所做的。

for i in range(len(images)):
    histogram, bin_edges = np.histogram(images[i].ravel(), 256, [0, 256])
    plt.plot(histogram)
    plt.show()

以上代码的输出:



这个输出很好,但我想将这些保存为图像。

我使用了 plt.savefig() 但这不是我想要的结果。

for i in range(len(images)):
    histogram, bin_edges = np.histogram(images[i].ravel(), 256, [0, 256])
    plt.plot(histogram)
    plt.savefig("histograms/hist_frame"+str(count)+".png")
    count+=1

以上代码的输出:

如何将这些直方图保存在单独的文件中?

您当前的代码是在同一个图形上绘制每个直方图,为了拥有多个图形,您需要为每个图形创建一个新图形:

for i in range(len(images)):
  plt.figure()
  histogram, bin_edges = np.histogram(images[i].ravel(), 256, [0, 256])
  plt.plot(histogram)
  plt.savefig("histograms/hist_frame"+str(count)+".png")
  count+=1

注意,如果要显示剧情,plt.show()必须跟在plt.savefig()之后,否则文件图像将是空白的。