Question: Function 2 : histogram In a file named histogram.py , implement the function histogram. The function will take two input parameters: ( 1 ) a
Function : histogram
In a file named histogram.py implement the function histogram. The function will take two input parameters: a list of dictionaries, and a string. The string will indicate what attribute will be plotted. You can also assume that the string provided is a key in all dictionaries in the list of dictionaries.
The function will go through all students and store the number of students that are at each level of the attribute. For example, if the attribute is School it will store the number of students for each school. For numerical values eg Age you need to define intervals between and themaximum value of the attribute. Note that you do not know in advance which values will be there.
This data will then be used to plot a histogram. The function will plot and show the histogram. The function will return if the attribute is categorical School and the maximum value for the given attribute if it is numerical.
Note: You have both numerical and string attributes. As hist only works with numerical values, you cannot use this function. You are required to use the function bar from Matplotlib.pyplot. You are not allowed to use hist.
import numpy as np
import matplotlib.pyplot as plt
def histogramdatalist, attribute:
if not datalist or attribute not in datalist:
return
isnumerical isinstancedatalistattributeint float
if isnumerical:
# Numerical data
values itemattribute for item in datalist
maxvalue maxvalues
minvalue minvalues
bins minvalue i
maxvalue minvalue for i in range
bincounts
for value in values:
for i in range:
if binsi value binsi :
bincountsi
break
if value maxvalue:
bincounts
pltbarrange bincounts, width align'center', ticklabel
fbinsi:fbinsi :f for i in range
pltxlabelattribute
pltylabelFrequency
plttitlefHistogram of attribute
pltshow
else:
values itemattribute for item in datalist
categorycounts
for value in values:
categorycountsvalue categorycounts.getvalue
categories listcategorycounts.keys
counts listcategorycounts.values
pltbarcategories counts, align'center'
pltxlabelattribute
pltylabelFrequency
plttitlefHistogram of attribute
pltshow
return None
Failed Tests
testcurvefit unittestloader.FailedTest
testfigure testhistogram.histogramtestcase
Test Failed: Expected 'figure' to have been called.
testreturn testhistogram.histogramtestcase
Test Failed: None : make sure your histogram function returns the correct value
Passed Tests
Check submitted files.
testshow testhistogram.histogramtestcase
testxlabel testhistogram.histogramtestcase
testylabel testhistogram.histogramtestcase
testtitle testhistogram.histogramtestcase
testbar testhistogram.histogramtestcase
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
