0
0
Data Analysis Pythondata~20 mins

Bar charts in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Bar Chart Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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()
AA bar chart with three bars labeled A, B, C with heights 5, 3, and 9 respectively
BA line chart connecting points (A,5), (B,3), (C,9)
CA scatter plot with points at (A,5), (B,3), (C,9)
DA pie chart showing proportions of 5, 3, and 9
Attempts:
2 left
💡 Hint
Look at the function plt.bar and what it does with categories and values.
data_output
intermediate
2: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)
A3
B4
C12
D7
Attempts:
2 left
💡 Hint
Sum the values for Group1 and Group2 where Category is 'X'.
visualization
advanced
2: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()
ANo error; the code produces a stacked bar chart correctly
BTypeError because bottom parameter expects a scalar, not a list
CValueError due to mismatched lengths of labels and data
DRuntimeWarning about overlapping bars
Attempts:
2 left
💡 Hint
Check how plt.bar handles the bottom parameter with lists.
🔧 Debug
advanced
2: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()
ATypeError because plt.barh does not accept color argument
BNo error; the code runs and shows a horizontal bar chart
CValueError because xlabel and ylabel are swapped
DAttributeError because plt.barh is not a valid function
Attempts:
2 left
💡 Hint
Check if plt.barh exists and accepts color argument.
🚀 Application
expert
2: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)
A7
B3
C12
D4
Attempts:
2 left
💡 Hint
Multiply the number of categories by the number of groups.