0

i have a small favour/question to ask! I have around 25 of these Images: images of letters

And always the same size. I am trying to calculate the amount of black and white space in the image.

enter image description here

But on some it goes up to 4 and some less, of course! But I would love to norm it so the z axes always goes up to a fixed number. I found some ways here: Plot a histogram such that the total area of the histogram equals 1 (density) But couldn't find the solution I am looking for. I tried seaboard, change the scale and so on. code:

np.array(pic_b) #letter Picture
pixels_b = np.array(pic_b).flatten()
pixels_b
fig, ax = plt.subplots(figsize=(6, 8) )


plt.hist(pixels_b,3, density=True)

# df = sns.load_dataset('penguins') #example data of course 
# fig, ax = plt.subplots(figsize=(5, 6))
# p = sns.histplot(data=df, x='flipper_length_mm', stat='density', ax=ax)


plt.show()

Any tips? Thank you so much, I know I am not very good!

calvin
  • 15
  • 1
  • 4
  • 1
    I highly recommend using seaborn histplot. What you can do is use a weighted histogram. Make a dataframe that precomputes what the bins are, and how much weight goes in each bin, and then divide the weights by the max weight. Then just use stat='count'. – Erotemic Jan 07 '22 at 21:07

1 Answers1

1

You can use np.histogram to calculate the histogram values. And then divide the resulting array with its maximum. ax.bar(...) can display the histogram, using the bin boundaries to position the bars:

import matplotlib.pyplot as plt
import numpy as np

# first generate some test data
pixels_a = np.random.choice([0, 255], 1000000, p=[0.11, 0.89])
pixels_b = np.random.choice([0, 255], 1000000, p=[0.19, 0.81])
pixels_c = np.random.choice([0, 255], 1000000, p=[0.03, 0.97])

fig, axs = plt.subplots(ncols=3, figsize=(12, 4))
for data, ax in zip([pixels_a, pixels_b, pixels_c], axs):
    values, bin_bounds = np.histogram(data, bins=3)
    values = values / values.max()
    ax.bar(bin_bounds[:-1], values, width=bin_bounds[1] - bin_bounds[0], align='edge')
plt.tight_layout()
plt.show()

histogram normalized via its maximum

JohanC
  • 71,591
  • 8
  • 33
  • 66