Python Forum

Full Version: Need help with NumPy Histogram function coding
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I'm a student currently doing experimental work in a lab and need to analyse some data. The data includes sniffing data from animals and I need to plot out sniffing rate. My mentor told me to use the NumPy Histogram function to do it, but I'm running into some struggles. The assignment is the following:


# calculate for each trial how many inhalations were performed in 0,5 bins
# trials are found in single ses_ids
# look up function np histogram
# then calculate mean sniffing across time for all trials and plot it


What I have already got so far is:


import matplotlib.pyplot as plt
import numpy as np
import os

data_path = r'C:\Users\32487\OneDrive\Documenten\Internship_data_KK4748'
ses_ids = ['230317_KK047', '230317_KK048']

for ses_id in ses_ids:
    ses_path = os.path.join(data_path, ses_id)
    print(ses_path)
    hist, bin_edges = np.histogram([ses_path], bins = range(10))
    print(hist)
    print(bin_edges)


What I want to do but can't is:


# how can I arrange bins to be 0,5 interval and how can I make the range fit the data?
# how can I make my path go INTO the ses_id and pick individual trials from there?
# how can I plot the histogram then? I think it's something like:

plt.hist([ses_path])
plt.show()
So basically with the function I have, I get the histogram [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], because I put the range at 10, and I did not make my path go into my file folder with the data (which I don't know how to do) but rather into the general named folder.

Any help is appreciated!
Read the documentations.

https://numpy.org/doc/stable/reference/g...ogram.html

Of particular interest:

Quote:bins : int or sequence of scalars or str, optional
If bins is an int, it defines the number of equal-width bins in the given range (10, by default). If bins is a sequence, it defines a monotonically increasing array of bin edges, including the rightmost edge, allowing for non-uniform bin widths.
Quote:range(float, float), optional
The lower and upper range of the bins. If not provided, range is simply (a.min(), a.max()). Values outside the range are ignored. The first element of the range must be less than or equal to the second. range affects the automatic bin computation as well. While bin width is computed to be optimal based on the actual data within range, the bin count will fill the entire range including portions containing no data.