Practice creating bar charts, histograms, and pie charts using sample data. This hands-on experience solidifies understanding of how and when to use each Matplotlib plot type.First, ensure you have Matplotlib imported, typically using the standard convention:import matplotlib.pyplot as plt import numpy as np # We'll use NumPy for generating some sample dataPractice 1: Creating a Bar ChartBar charts are excellent for comparing quantities across different categories. Let's imagine we want to visualize the number of units sold for different types of fruits in a small grocery stand.Data: Suppose we have the following sales data:Apples: 50 unitsOranges: 35 unitsBananas: 65 unitsGrapes: 42 unitsSteps:Prepare the data: Create lists for the fruit names (categories) and their corresponding sales figures (values).Create the bar chart: Use plt.bar() function, passing the categories for the x-axis and values for the y-axis.Add labels and title: Use plt.xlabel(), plt.ylabel(), and plt.title() to make the plot understandable.Display the plot: Use plt.show().Code:import matplotlib.pyplot as plt # 1. Prepare the data fruit_names = ['Apples', 'Oranges', 'Bananas', 'Grapes'] units_sold = [50, 35, 65, 42] # 2. Create the bar chart plt.figure(figsize=(6, 4)) # Optional: Adjust figure size plt.bar(fruit_names, units_sold, color='#339af0') # Use a color from the blue palette # 3. Add labels and title plt.xlabel('Fruit Type') plt.ylabel('Units Sold') plt.title('Fruit Sales Comparison') plt.xticks(rotation=45) # Rotate x-axis labels if they overlap plt.tight_layout() # Adjust layout to prevent labels overlapping # 4. Display the plot plt.show()Running this code will generate a bar chart showing the sales for each fruit type, making it easy to see that Bananas had the highest sales.Challenge: Try modifying the code above to create a horizontal bar chart using plt.barh(). Remember to switch the arguments and adjust the labels accordingly (plt.xlabel becomes 'Units Sold', plt.ylabel becomes 'Fruit Type').Practice 2: Generating a HistogramHistograms help us understand the distribution of a single numerical variable by grouping data into bins and showing the frequency of observations in each bin. Let's visualize the distribution of exam scores for a class.Data: We'll generate some random exam scores using NumPy to simulate a distribution. Let's assume scores are out of 100 and roughly normally distributed around a mean of 75.Steps:Generate sample data: Use np.random.normal() to create an array of scores.Create the histogram: Use plt.hist(), passing the data and optionally specifying the number of bins or letting Matplotlib decide.Add labels and title: Label the axes and give the plot a descriptive title.Display the plot: Use plt.show().Code:import matplotlib.pyplot as plt import numpy as np # 1. Generate sample data (e.g., 100 exam scores) np.random.seed(42) # for reproducible results exam_scores = np.random.normal(loc=75, scale=10, size=100) # Ensure scores are within a realistic range (e.g., 0-100) exam_scores = np.clip(exam_scores, 0, 100) # 2. Create the histogram plt.figure(figsize=(7, 5)) plt.hist(exam_scores, bins=10, color='#51cf66', edgecolor='black') # Use 10 bins, green color # 3. Add labels and title plt.xlabel('Exam Score') plt.ylabel('Number of Students') plt.title('Distribution of Exam Scores') # 4. Display the plot plt.show()This code will produce a histogram showing how many students scored within specific score ranges (the bins). You can experiment by changing the bins argument (e.g., bins=5, bins=20) to see how it affects the appearance of the distribution.Practice 3: Constructing a Pie ChartPie charts are used to show proportions of a whole. Let's visualize the percentage breakdown of a student's monthly expenses.Data: Suppose the expenses are categorized as follows:Rent: 40%Food: 25%Transport: 15%Utilities: 10%Entertainment: 10%Steps:Prepare the data: Create lists for the category labels and their corresponding percentage values.Create the pie chart: Use plt.pie(), passing the values and labels. Use autopct to display percentages on the slices.Add a title: Use plt.title(). Add plt.axis('equal') to ensure the pie chart is circular.Display the plot: Use plt.show().Code:import matplotlib.pyplot as plt # 1. Prepare the data expense_categories = ['Rent', 'Food', 'Transport', 'Utilities', 'Entertainment'] percentages = [40, 25, 15, 10, 10] colors = ['#748ffc', '#ff922b', '#51cf66', '#fcc419', '#f06595'] # Indigo, Orange, Green, Yellow, Pink # 2. Create the pie chart plt.figure(figsize=(6, 6)) plt.pie(percentages, labels=expense_categories, colors=colors, autopct='%1.1f%%', # Format percentages to one decimal place startangle=90) # Start the first slice at the top (90 degrees) # 3. Add title and ensure it's circular plt.title('Monthly Expense Breakdown') plt.axis('equal') # Equal aspect ratio ensures a circular pie chart # 4. Display the plot plt.show()This generates a pie chart illustrating how the student's budget is allocated. While useful for showing simple proportions, remember that pie charts become difficult to read when there are many categories or when the proportions are very similar. Bar charts are often a better alternative for comparisons.You have now practiced creating the fundamental plot types covered in this chapter: bar charts for comparison, histograms for distribution, and pie charts for proportions. Feel free to modify the sample data or parameters in these examples to explore how the plots change. This experimentation is a great way to build intuition about which plot works best for different kinds of data and analytical questions.