Miguel.G
Miguel.G

Reputation: 387

Python histogram has always incorrect bins

I am trying to plot a histogram in Python with plt.hist. This is the array I would like to plot:

[162 162 162 161 162 157 162 161 164 161 163 160 155 162 160 154 162 162 163 160 162 157 162 160 165 161 162 161 155 163 161 155 162 162 162 161 163 156 163 160 165 161 163 161 154 162 160 155 163 163 163 161 162 156 162 160 165 161 162 160 154 163 161 155 163 162 163 160 163 157 163 161 165 161 162 160 155 162 160 155 164 164 159 155 161 159 158 160 161 161 155 159 154 154 156 155 160 160 163 158 160 163 159 156 159 162 156 163 155 154 156 152 158 158 154 156 158 158 156 157 158 160 160 159 153 152 153 150 154 155 158 158 159 160 156 157 163 157 158 159 159 155 156 157 154 155 157 158 155 154 155 157 160 154 154 157 157 157 156 153 157 156 156 161 157 155 154 153 159 158 157 157 158 155 159 154 156 156 156 158 159 155 150 148 158 159 156 157 157 155 157 158 158 158 157 156 157 153 159 156 160 156 158 156 156 153 156 156 157 157 157 157 160 157 156 156 159 155 154 158 156 155 154 160 158 158 159 155 155 158 158 156 155 156 151 158 157 156 156 155 158 158 159 157 155 158 157 154 157 157 157 159 155 156 154 156]

And this is my easy code:

hst = plt.hist(stego_histogram , bins=256) 
plt.show()

This array is constructed with some pixels of a image. Obviously, 8-bit depth image has 256 values, that is why I choosed that bins. However, as this values goes from 148 to 165, the histogram return values from this range but divided into 256 values.

I have tried to configure the histogram with other bins values (16,17,18..) but it is never printed okay. This is the histogram with bins=18:

enter image description here

How can I plot this histogram correctly? I just want a bar histogram of this values. Thanks.

Upvotes: 0

Views: 1293

Answers (4)

Quang Hoang
Quang Hoang

Reputation: 150745

IIUC, you want something like this:

counts, bins, _ = plt.hist(data, bins=range(256))
plt.show()

Output:

enter image description here

Upvotes: 0

Sheldore
Sheldore

Reputation: 39052

What you need is not a histogram but a bar chart for the frequencies. The frequencies can be computed using Counter. In the below answer, replace data by your actual data list.

import matplotlib.pyplot as plt
from collections import Counter

data = [162, 162, 162, 161, 162, 157, 162, 159, 155, 155, 158, 158, 156]

freqs = Counter(data)
plt.bar(freqs.keys(), freqs.values())
plt.show()

enter image description here

Upvotes: 0

Miguel.G
Miguel.G

Reputation: 387

I really don't know why but with bins=17 everything is plotted okay. The issue was that intervals weren't integer as you can see in the graph that I attached. I tried this bins value before but it didn't works. Sorry and thanks for your answers!

Upvotes: 0

What histogram does is just count how many times a value repeats and then plots a vertical bar for every possible value, the height of each vertical bar is the number of occurrences of that value.

If you want the full histogram between 0 and 255 then you need to specify the parameter called range in the hist call.

Otherwise you could also use the bar method from matplotlib to just show the array you have provided here.

Here is the documentation for matplot bar

Upvotes: 1

Related Questions