Bird
Raised Fist0
Data Analysis Pythondata~20 mins

Categorical plots (boxplot, violinplot) in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Categorical Plot Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of boxplot with grouped categories
What will be the output of the following code snippet that creates a boxplot grouped by category?
Data Analysis Python
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

data = pd.DataFrame({
    'Category': ['A', 'A', 'B', 'B', 'C', 'C'],
    'Value': [10, 15, 10, 20, 15, 25]
})
sns.boxplot(x='Category', y='Value', data=data)
plt.show()
AA boxplot with three boxes labeled A, B, C showing value distributions: A (10,15), B (10,20), C (15,25)
BA line plot connecting points (A,10), (A,15), (B,10), (B,20), (C,15), (C,25)
CA scatter plot with points at (A,10), (A,15), (B,10), (B,20), (C,15), (C,25)
DA bar chart showing sum of values for each category
Attempts:
2 left
💡 Hint
Boxplots show distribution of values grouped by categories.
❓ data_output
intermediate
1:30remaining
Number of boxes in violinplot
Given the following data and code, how many violin shapes will appear in the plot?
Data Analysis Python
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

data = pd.DataFrame({
    'Group': ['X', 'X', 'Y', 'Y', 'Y', 'Z'],
    'Score': [5, 7, 6, 8, 7, 9]
})
sns.violinplot(x='Group', y='Score', data=data)
plt.show()
A6
B2
C1
D3
Attempts:
2 left
💡 Hint
Each unique category in 'Group' creates one violin shape.
❓ visualization
advanced
1:30remaining
Identify the plot type from description
Which plot type best shows the distribution shape and individual data points for categories?
AViolinplot
BBoxplot
CBar plot
DScatter plot
Attempts:
2 left
💡 Hint
One plot type shows a smooth density shape and can include individual points.
🔧 Debug
advanced
1:30remaining
Error in boxplot code
What error will this code produce and why? import seaborn as sns import matplotlib.pyplot as plt sns.boxplot(x='Category', y='Value') plt.show()
Data Analysis Python
import seaborn as sns
import matplotlib.pyplot as plt

sns.boxplot(x='Category', y='Value')
plt.show()
ANameError: name 'Category' is not defined
BValueError: x and y must be numeric
CTypeError: boxplot() missing 1 required positional argument: 'data'
DNo error, plot shows empty figure
Attempts:
2 left
💡 Hint
Seaborn needs data argument to know where to find columns.
🚀 Application
expert
2:30remaining
Choosing plot for comparing distributions with many categories
You have a dataset with 20 categories and want to compare the distribution of a numeric variable across them. Which plot is best to use and why?
ABar plot, because it shows average values clearly for many categories
BBoxplot, because it summarizes distribution with quartiles and is compact for many categories
CViolinplot, because it shows detailed distribution shape for each category clearly even with many categories
DScatter plot, because it shows all individual points for all categories
Attempts:
2 left
💡 Hint
Consider clarity and compactness when many categories exist.

Practice

(1/5)
1. What does a boxplot primarily show about a dataset?
easy
A. The correlation between two variables
B. The median, quartiles, and outliers of the data
C. Only the mean and standard deviation
D. The exact values of all data points

Solution

  1. Step 1: Understand boxplot components

    A boxplot displays the median line, the box showing the interquartile range (25th to 75th percentile), and whiskers for range excluding outliers.
  2. Step 2: Identify what it does not show

    It does not show every data point's exact value or correlation between variables.
  3. Final Answer:

    The median, quartiles, and outliers of the data -> Option B
  4. Quick Check:

    Boxplot = median + quartiles + outliers [OK]
Hint: Boxplots summarize data spread with median and quartiles [OK]
Common Mistakes:
  • Thinking boxplots show all individual data points
  • Confusing boxplots with scatterplots
  • Assuming boxplots show mean instead of median
2. Which of the following is the correct way to create a violin plot using seaborn for a DataFrame df with columns 'category' and 'value'?
easy
A. sns.violinplot(y='category', x='value', data=df)
B. sns.violinplot(data=df, x='value', y='category')
C. sns.violinplot(df['category'], df['value'])
D. sns.violinplot(x='category', y='value', data=df)

Solution

  1. Step 1: Check seaborn violinplot syntax

    The correct syntax is sns.violinplot(x='category', y='value', data=df) where x is the categorical variable and y is the numeric variable.
  2. Step 2: Identify incorrect options

    sns.violinplot(data=df, x='value', y='category') swaps x and y incorrectly. sns.violinplot(df['category'], df['value']) passes series without specifying data keyword. sns.violinplot(y='category', x='value', data=df) swaps x and y axes.
  3. Final Answer:

    sns.violinplot(x='category', y='value', data=df) -> Option D
  4. Quick Check:

    Violinplot syntax = sns.violinplot(x=cat, y=num, data=df) [OK]
Hint: Use x=category, y=value, data=DataFrame for seaborn violinplot [OK]
Common Mistakes:
  • Swapping x and y parameters
  • Passing columns without data= argument
  • Using positional arguments instead of keywords
3. Given the following code, what will be the output type?
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt

# Sample data
df = pd.DataFrame({
    'group': ['A', 'A', 'B', 'B', 'C', 'C'],
    'score': [10, 15, 10, 20, 15, 25]
})

plot = sns.boxplot(x='group', y='score', data=df)
plt.show()
medium
A. A boxplot showing score distribution per group
B. A scatter plot of scores vs groups
C. A line plot connecting scores in each group
D. An error because of missing hue parameter

Solution

  1. Step 1: Understand sns.boxplot usage

    The code uses seaborn's boxplot with x as categorical 'group' and y as numeric 'score', which creates boxplots per group.
  2. Step 2: Confirm plot type and parameters

    No hue is required here; the plot will show boxplots for each group category with score distribution.
  3. Final Answer:

    A boxplot showing score distribution per group -> Option A
  4. Quick Check:

    sns.boxplot(x=cat, y=num) = boxplot per category [OK]
Hint: sns.boxplot(x=cat, y=num) plots distribution per category [OK]
Common Mistakes:
  • Expecting scatter or line plot instead of boxplot
  • Thinking hue is mandatory
  • Confusing boxplot with barplot
4. Identify the error in this code snippet for creating a violin plot:
import seaborn as sns
sns.violinplot(data=df, x='value', y='category')
medium
A. DataFrame df is not defined
B. Missing import for matplotlib.pyplot
C. The x and y parameters are swapped; x should be categorical
D. Violinplot does not accept x and y parameters

Solution

  1. Step 1: Check parameter roles in violinplot

    In seaborn violinplot, x should be the categorical variable and y the numeric variable. Here, 'value' is numeric and 'category' is categorical, so x and y are swapped.
  2. Step 2: Verify other possible errors

    Importing matplotlib is not mandatory for just creating the plot object. The DataFrame df is assumed defined. Violinplot accepts x and y parameters.
  3. Final Answer:

    The x and y parameters are swapped; x should be categorical -> Option C
  4. Quick Check:

    Violinplot x=category, y=numeric [OK]
Hint: Categorical variable must be x in violinplot [OK]
Common Mistakes:
  • Swapping x and y axes
  • Assuming matplotlib import is always needed
  • Not defining DataFrame before plotting
5. You have a dataset with three groups: 'X', 'Y', and 'Z'. Group 'Z' has many outliers. You want to visualize the distribution shape and highlight the outliers clearly. Which plot should you choose and why?
hard
A. Use a violinplot to show distribution shape and a boxplot overlay to highlight outliers
B. Use only a boxplot because violinplots hide outliers
C. Use a scatter plot to show all points without summary
D. Use a line plot to connect group means

Solution

  1. Step 1: Understand plot strengths

    Violinplots show the shape and density of data distribution well, but outliers can be less obvious. Boxplots clearly mark outliers.
  2. Step 2: Combine plots for best insight

    Overlaying a boxplot on a violinplot combines both benefits: distribution shape and clear outlier marking.
  3. Final Answer:

    Use a violinplot to show distribution shape and a boxplot overlay to highlight outliers -> Option A
  4. Quick Check:

    Violin + boxplot = shape + outliers [OK]
Hint: Combine violin and boxplot for shape and outliers [OK]
Common Mistakes:
  • Thinking violinplots alone highlight outliers well
  • Using scatter plots without summary
  • Choosing line plots for categorical distributions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard