Using python create a function the calculates and then plots a histogram. Please
ID: 3603598 • Letter: U
Question
Using python create a function the calculates and then plots a histogram. Please write this function yourself! The function should take a one dimensional array, a title for the histogram, a label for the x-axis, the number of boxes in the histogram, and upper & lower limits of the histogram. It should bin the data and make a plot of this reduced data using the matplotlib “plot” command to make a simple line plot of the results. The data should be saved into a png file with the name“title.png”. Please dont use import cv2, use the matplotlib “plot” .
Explanation / Answer
import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
##The function
def plot_histogram(data_array, title, x_label, no_of_boxes, upper_limit, lower_limit):
#calculation of bins
binwidth = (upper_limit - lower_limit) / no_of_boxes
bins = np.arange(lower_limit, upper_limit + binwidth, binwidth)
plt.hist(data_array, bins)
plt.title(title)
plt.xlabel(x_label)
#displays the histogram
plt.show()
#saves the data to png image in the same folder as this program
plt.savefig(title+'.png')
return
##You can test the function with the statement
plot_histogram([1,2], "histogram_title", "x_label", 100, 100, -10)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.