Python Forum
typeerror, building histogram from data
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
typeerror, building histogram from data
#1
I am trying to run a code to build histogram from a list of data and keep getting the below error :

Error:
TypeError: list indices must be integers or slices, not float
Below is my code, can you help me with what i am doing wrong?

%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from helper import plotHisto

data = np.loadtxt('math_scores.txt')
def myMap(inp, f) :
    res = []
    for i in inp :
        res.append(f(i))
    return res

def new(x) : return int(x)
pdata = myMap(data, new)

def buildHisto(pdata, numbins, minimum, maximum) :

    minimum = min(pdata)
    maximum = max(pdata)
    diff = maximum - minimum
    binwidth = diff/numbins
    histo = []
    for d in pdata :
        histo[d//binwidth]+=1
    return histo

histo = buildHisto(pdata, 10, minimum, maximum)
plotHisto(histo, 'histo1.png', minimum, maximum, plotinline = True)
Reply


Messages In This Thread
typeerror, building histogram from data - by newatpython11 - Jul-16-2019, 03:05 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Need help with NumPy Histogram function coding Triikey 1 1,037 May-15-2023, 01:45 PM
Last Post: deanhystad
  Plotting histogram of dataframe column Mark17 4 2,744 Jul-30-2020, 09:52 AM
Last Post: Mark17
  Can someone find out what kind of histogram is it? J_tin 1 1,858 Apr-26-2020, 05:23 PM
Last Post: buran
  Histogram using pandas dataframe not showing proper output ift38375 1 2,269 Jul-04-2019, 10:43 PM
Last Post: scidam
  Histogram and text file. pawlo392 1 4,193 May-24-2019, 03:14 AM
Last Post: heiner55
  how do i use get for my histogram function? pseudo 4 4,945 Oct-14-2018, 04:23 AM
Last Post: stullis

Forum Jump:

User Panel Messages

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