Write code to draw a histogram.
Question
Write code to draw a histogram.
Solution
Sure, here is an example code to draw a histogram in Python:
import matplotlib.pyplot as plt
# Sample data
data = [1, 3, 2, 4, 3, 5, 4, 6, 7, 8, 9, 5, 6, 7, 8, 9, 10]
# Plotting the histogram
plt.hist(data, bins=5, edgecolor='black')
# Adding labels and title
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.title('Histogram')
# Displaying the histogram
plt.show()
This code uses the matplotlib library to create a histogram. It first imports the necessary module, then defines the data to be plotted. The hist() function is used to create the histogram, with the bins parameter specifying the number of bins or bars in the histogram. The edgecolor parameter sets the color of the edges of the bars.
After plotting the histogram, labels and a title are added using the xlabel(), ylabel(), and title() functions. Finally, the show() function is called to display the histogram.
Similar Questions
Histograms can be used to graph
From what kinds of variables would a histogram be generated?
Histograms can be drawn asSelect one:a.vertical and horizontal bar charts with gapsb.vertical bar charts with no gapsc.horizontal bar charts with gapsd.vertical bar charts with gaps
A histogram is a graphical device that is commonly used to analyze:
Fill in the gaps:By joining the centre of each column in the histogram we create a _____________ ____________
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.