Python Forum
Need help with NumPy Histogram function coding
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with NumPy Histogram function coding
#1
Question 
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!
buran write May-15-2023, 10:04 AM:
Please, don't remove post content
buran write May-15-2023, 09:35 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Plotting histogram of dataframe column Mark17 4 2,619 Jul-30-2020, 09:52 AM
Last Post: Mark17
  coding a function helpless_coder 7 2,717 Apr-30-2020, 03:31 AM
Last Post: deanhystad
  Can someone find out what kind of histogram is it? J_tin 1 1,779 Apr-26-2020, 05:23 PM
Last Post: buran
  typeerror, building histogram from data newatpython11 7 3,761 Jul-17-2019, 12:54 PM
Last Post: ichabod801
  Histogram using pandas dataframe not showing proper output ift38375 1 2,184 Jul-04-2019, 10:43 PM
Last Post: scidam
  Histogram and text file. pawlo392 1 4,114 May-24-2019, 03:14 AM
Last Post: heiner55
  New to Python - tiny coding assistance on user input function and assign to variable Mountain_Duck 1 2,492 Mar-23-2019, 06:54 PM
Last Post: Yoriz
  how do i use get for my histogram function? pseudo 4 4,790 Oct-14-2018, 04:23 AM
Last Post: stullis
  The where function in numpy saund1pe 5 6,218 Nov-16-2016, 08:04 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020