Challenge - 5 Problems
Bar Plot Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple bar plot code
What will be the output of the following code snippet that creates a bar plot using pandas?
Pandas
import pandas as pd import matplotlib.pyplot as plt data = {'fruits': ['apple', 'banana', 'cherry'], 'counts': [10, 15, 7]} df = pd.DataFrame(data) df.plot.bar(x='fruits', y='counts') plt.close() # Prevent plot display in some environments print(df['counts'].sum())
Attempts:
2 left
💡 Hint
Think about what the sum of the 'counts' column is.
✗ Incorrect
The code creates a DataFrame with counts 10, 15, and 7. The sum of these counts is 32, which is printed as output.
❓ data_output
intermediate2:00remaining
Number of bars in grouped bar plot
Given the following DataFrame and code, how many bars will the resulting grouped bar plot display?
Pandas
import pandas as pd import matplotlib.pyplot as plt data = {'year': [2020, 2020, 2021, 2021], 'category': ['A', 'B', 'A', 'B'], 'value': [5, 7, 6, 8]} df = pd.DataFrame(data) df_pivot = df.pivot(index='year', columns='category', values='value') df_pivot.plot.bar() plt.close()
Attempts:
2 left
💡 Hint
Count the number of bars per group and the number of groups.
✗ Incorrect
There are 2 years (groups) and 2 categories (bars per group), so total bars = 2 * 2 = 4.
🔧 Debug
advanced2:00remaining
Identify the error in bar plot code
What error will this code raise when trying to create a bar plot?
Pandas
import pandas as pd import matplotlib.pyplot as plt data = {'items': ['pen', 'pencil', 'eraser'], 'counts': ['10', '15', '7']} df = pd.DataFrame(data) df.plot.bar(x='items', y='counts') plt.close()
Attempts:
2 left
💡 Hint
Check the data types of the 'counts' column.
✗ Incorrect
The 'counts' column contains strings, but pandas and matplotlib can handle numeric strings for plotting without error.
❓ visualization
advanced2:00remaining
Bar plot with stacked bars
What will the stacked bar plot display when running this code?
Pandas
import pandas as pd import matplotlib.pyplot as plt data = {'Q1': [3, 5], 'Q2': [4, 6]} index = ['Product A', 'Product B'] df = pd.DataFrame(data, index=index) df.plot.bar(stacked=True) plt.close()
Attempts:
2 left
💡 Hint
Stacked bars combine segments vertically in one bar per index.
✗ Incorrect
The plot shows one bar per product with Q1 and Q2 values stacked on top of each other.
🧠 Conceptual
expert2:00remaining
Effect of setting figsize in bar plots
How does changing the figsize parameter affect a pandas bar plot?
Attempts:
2 left
💡 Hint
Think about what 'figsize' means in matplotlib plots.
✗ Incorrect
The figsize parameter controls the overall size of the figure, affecting how big the bars and labels appear.