0

How do i plot an array in a histogram style using matplotlib.plot() or similar functions?

I also use scikit as a package but im completely new to that one and have not found an alternative there either so far.

The function hist in matplotlib calculates the histogram (e.g. of a picture) and plots it. However, for my assignment i have to calculate the histogram myself as an array of shape (256,), where each index corresponds to a calculated grey-scale value from 0 to 255.

So far i looked at the different parameters of matplotlib.plot() but could not see or figure out a way to plot in a hist style.

3
  • I am sure you can calculate your histogram with the keywords provided by matplotlib hist, e.g., the bin parameter. Commented Mar 30, 2022 at 21:42
  • @Mr.T the bins parameter lets you choose your resolution. If you set it to 256 in my case, you will get a bin for each value, reduce it to 128 and 2 bins will be merged together reducing resolution. However, i still would like to use my own calculated array, which i cant when i use hist. Using my array with hist would yield me a histogram of my histogram. Commented Mar 30, 2022 at 21:48
  • bins: int or sequence or str - it takes an array, if that is what you want. Commented Mar 30, 2022 at 21:51

1 Answer 1

1

Try calculating the histogram and than plotting this as bars?

import numpy as np
from matplotlib import pyplot as plt

dummie_data = np.random.randint(0, 256, (100,100))

values, count = np.unique(dummie_data, return_counts=True)

plt.figure(figsize=(10,6))
plt.bar(values, count/dummie_data.size, width=1)
plt.show()

enter image description here

By using:

plt.bar(values, count, width=1)

You use the absolute frequency.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, thats exactly what i needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.