0
0
Matplotlibdata~30 mins

Histogram vs bar chart distinction in Matplotlib - Hands-On Comparison

Choose your learning style9 modes available
Histogram vs Bar Chart Distinction
📖 Scenario: Imagine you are a data analyst working with sales data and customer age data. You want to understand how to visualize these two types of data correctly using charts.
🎯 Goal: You will create a bar chart to show sales by product category and a histogram to show the distribution of customer ages. This will help you see the difference between these two chart types.
📋 What You'll Learn
Create a dictionary called sales with product categories and their sales numbers.
Create a list called customer_ages with ages of customers.
Set a variable bins to 5 for the histogram.
Use plt.bar() to create a bar chart for sales data.
Use plt.hist() to create a histogram for customer ages.
Display both charts with plt.show().
💡 Why This Matters
🌍 Real World
Bar charts are used to compare categories like product sales, while histograms help understand the distribution of continuous data like customer ages.
💼 Career
Data analysts and scientists use these charts to summarize and communicate data insights clearly to teams and stakeholders.
Progress0 / 4 steps
1
Create sales data and customer ages
Create a dictionary called sales with these exact entries: 'Books': 120, 'Electronics': 90, 'Clothing': 150. Also create a list called customer_ages with these exact values: 22, 25, 47, 35, 46, 52, 23, 43, 36, 27.
Matplotlib
Need a hint?

Use curly braces {} to create the dictionary sales with keys and values. Use square brackets [] to create the list customer_ages.

2
Set number of bins for histogram
Create a variable called bins and set it to 5 to define the number of bins for the histogram.
Matplotlib
Need a hint?

Just write bins = 5 to set the number of bins.

3
Create bar chart and histogram
Import matplotlib.pyplot as plt. Use plt.bar() with sales.keys() and sales.values() to create a bar chart. Then use plt.hist() with customer_ages and bins to create a histogram. Use plt.figure() to create separate figures for each chart.
Matplotlib
Need a hint?

Use plt.figure() before each chart to separate them. Use plt.bar() for the sales dictionary and plt.hist() for the customer_ages list.

4
Display the charts
Use plt.show() to display both the bar chart and the histogram.
Matplotlib
Need a hint?

Just add plt.show() at the end to display the charts.