0
0
Pandasdata~20 mins

Bar plots in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Bar Plot 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 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())
A15
B32
C10
D3
Attempts:
2 left
💡 Hint
Think about what the sum of the 'counts' column is.
data_output
intermediate
2: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()
A1
B2
C4
D3
Attempts:
2 left
💡 Hint
Count the number of bars per group and the number of groups.
🔧 Debug
advanced
2: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()
ANo error, plot displays correctly
BTypeError: unsupported operand type(s) for +: 'int' and 'str'
CValueError: could not convert string to float: 'pen'
DTypeError: unhashable type: 'list'
Attempts:
2 left
💡 Hint
Check the data types of the 'counts' column.
visualization
advanced
2: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()
AA line plot connecting Q1 and Q2 values
BA pie chart showing total sums of Q1 and Q2
CFour separate bars, two for Q1 and two for Q2
DTwo bars, each divided into two stacked segments representing Q1 and Q2 values
Attempts:
2 left
💡 Hint
Stacked bars combine segments vertically in one bar per index.
🧠 Conceptual
expert
2:00remaining
Effect of setting figsize in bar plots
How does changing the figsize parameter affect a pandas bar plot?
AIt changes the size of the entire plot area, making bars and labels larger or smaller accordingly
BIt changes only the width of the bars but not the height of the plot
CIt changes the color of the bars automatically based on size
DIt changes the data values plotted on the bars
Attempts:
2 left
💡 Hint
Think about what 'figsize' means in matplotlib plots.