Challenge - 5 Problems
Bar Chart Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple bar chart code
What will be the output of this Python code using matplotlib?
Data Analysis Python
import matplotlib.pyplot as plt categories = ['A', 'B', 'C'] values = [5, 3, 9] plt.bar(categories, values) plt.show()
Attempts:
2 left
💡 Hint
Look at the function plt.bar and what it does with categories and values.
✗ Incorrect
plt.bar creates vertical bars for each category with heights from values. plt.show() displays the chart.
❓ data_output
intermediate2:00remaining
Data output from grouped bar chart data
Given this data for two groups, what is the total height of bars for category 'X'?
Data Analysis Python
import pandas as pd data = {'Category': ['X', 'Y', 'Z'], 'Group1': [4, 7, 1], 'Group2': [3, 2, 5]} df = pd.DataFrame(data) total_x = df.loc[df['Category'] == 'X', ['Group1', 'Group2']].sum(axis=1).values[0] print(total_x)
Attempts:
2 left
💡 Hint
Sum the values for Group1 and Group2 where Category is 'X'.
✗ Incorrect
For category 'X', Group1 has 4 and Group2 has 3. Their sum is 7.
❓ visualization
advanced2:00remaining
Identify the error in this stacked bar chart code
What error will this code produce when trying to plot a stacked bar chart?
Data Analysis Python
import matplotlib.pyplot as plt labels = ['G1', 'G2', 'G3'] men_means = [20, 35, 30] women_means = [25, 32, 34] plt.bar(labels, men_means) plt.bar(labels, women_means, bottom=men_means) plt.show()
Attempts:
2 left
💡 Hint
Check how plt.bar handles the bottom parameter with lists.
✗ Incorrect
plt.bar accepts a list for bottom to stack bars. The code runs without error and produces a stacked bar chart.
🔧 Debug
advanced2:00remaining
Why does this horizontal bar chart code fail?
This code is intended to create a horizontal bar chart but raises an error. What is the cause?
Data Analysis Python
import matplotlib.pyplot as plt categories = ['A', 'B', 'C'] values = [10, 20, 15] plt.barh(categories, values, color='blue') plt.xlabel('Categories') plt.ylabel('Values') plt.show()
Attempts:
2 left
💡 Hint
Check if plt.barh exists and accepts color argument.
✗ Incorrect
plt.barh is a valid function and accepts color. xlabel and ylabel can be set in any order. The code runs fine.
🚀 Application
expert2:00remaining
Calculate the number of bars in a grouped bar chart
You have a DataFrame with 4 categories and 3 groups. You want to plot a grouped bar chart showing all groups side by side for each category. How many bars will appear in total?
Data Analysis Python
import pandas as pd data = {'Category': ['C1', 'C2', 'C3', 'C4'], 'Group1': [5, 7, 6, 8], 'Group2': [3, 2, 4, 1], 'Group3': [9, 5, 7, 6]} df = pd.DataFrame(data)
Attempts:
2 left
💡 Hint
Multiply the number of categories by the number of groups.
✗ Incorrect
Each category has 3 groups, so total bars = 4 categories * 3 groups = 12 bars.