Matplotlib: Exercises


4. Creating a Histogram. In Matplotlib, you can create a histogram to visualize the distribution of numerical data. You can use the plt.hist() function to create histograms.

For example, you can create a histogram of data values like this: import matplotlib.pyplot as plt; plt.hist(data, bins).

In the exercise below, you will create a histogram.


# Import the Matplotlib library import matplotlib.pyplot as plt # Define data values and the number of bins for a histogram data = [1, 2, 2, 3, 3, 3, 4, 4, 5, 5, 5, 5] bins = 5 # Create a histogram plt.hist(___) # Display the plot plt.show() # Import the Matplotlib library import matplotlib.pyplot as plt # Define data values and the number of bins for a histogram data = [1, 2, 2, 3, 3, 3, 4, 4, 5, 5, 5, 5] bins = 5 # Create a histogram plt.hist(data, bins) # Display the plot plt.show() test_object("data") test_object("bins") success_msg("Correct!")
To create a histogram, import Matplotlib and use plt.hist() with data values and the number of bins.

Previous Exercise Next Exercise