Bird
Raised Fist0
Pandasdata~10 mins

Bar plots in Pandas - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a bar plot of the 'sales' column.

Pandas
import pandas as pd
import matplotlib.pyplot as plt

data = {'product': ['A', 'B', 'C'], 'sales': [10, 20, 15]}
df = pd.DataFrame(data)
df['sales'].[1]()
plt.show()
Drag options to blanks, or click blank then click option'
Aplot.line
Bplot.bar
Cplot.scatter
Dplot.hist
Attempts:
3 left
💡 Hint
Common Mistakes
Using plot.line instead of plot.bar
Forgetting to call the plot method
Using plot.hist which creates a histogram
2fill in blank
medium

Complete the code to set the title of the bar plot to 'Sales Data'.

Pandas
import pandas as pd
import matplotlib.pyplot as plt

data = {'product': ['A', 'B', 'C'], 'sales': [10, 20, 15]}
df = pd.DataFrame(data)
ax = df['sales'].plot.bar()
ax.[1]('Sales Data')
plt.show()
Drag options to blanks, or click blank then click option'
Atitle
Bxlabel
Cylabel
Dlegend
Attempts:
3 left
💡 Hint
Common Mistakes
Using xlabel or ylabel instead of title
Trying to set title on plt instead of ax
Forgetting to call the method
3fill in blank
hard

Fix the error in the code to correctly plot a bar chart with product names as x-axis labels.

Pandas
import pandas as pd
import matplotlib.pyplot as plt

data = {'product': ['A', 'B', 'C'], 'sales': [10, 20, 15]}
df = pd.DataFrame(data)
df.plot.bar(x=[1], y='sales')
plt.show()
Drag options to blanks, or click blank then click option'
A'product'
B'sales'
C'index'
D'value'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'sales' as x-axis labels
Using 'index' which is default row numbers
Using a non-existent column name
4fill in blank
hard

Fill both blanks to create a horizontal bar plot with 'product' on y-axis and 'sales' on x-axis.

Pandas
import pandas as pd
import matplotlib.pyplot as plt

data = {'product': ['A', 'B', 'C'], 'sales': [10, 20, 15]}
df = pd.DataFrame(data)
df.plot.barh(x=[1], y=[2])
plt.show()
Drag options to blanks, or click blank then click option'
A'product'
B'sales'
C'index'
D'value'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping x and y parameters
Using 'index' instead of 'product'
Using non-existent columns
5fill in blank
hard

Fill all three blanks to create a bar plot with 'product' as x-axis, 'sales' as y-axis, and set the color to 'green'.

Pandas
import pandas as pd
import matplotlib.pyplot as plt

data = {'product': ['A', 'B', 'C'], 'sales': [10, 20, 15]}
df = pd.DataFrame(data)
ax = df.plot.bar(x=[1], y=[2], color=[3])
plt.show()
Drag options to blanks, or click blank then click option'
A'product'
B'sales'
C'green'
D'blue'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong column names for x or y
Using color names not supported
Forgetting to put quotes around strings

Practice

(1/5)
1. What does a bar plot primarily help you do with data?
easy
A. Sort data in ascending order
B. Compare values across different categories visually
C. Calculate the average of numerical data
D. Show the relationship between two continuous variables

Solution

  1. Step 1: Understand the purpose of a bar plot

    A bar plot uses bars to represent data values for different categories, making it easy to compare them visually.
  2. Step 2: Compare with other options

    Calculating averages, sorting data, or showing relationships between continuous variables are not the main uses of bar plots.
  3. Final Answer:

    Compare values across different categories visually -> Option B
  4. Quick Check:

    Bar plot = Compare categories [OK]
Hint: Bar plots show category comparisons with bars [OK]
Common Mistakes:
  • Confusing bar plots with scatter plots
  • Thinking bar plots calculate statistics
  • Assuming bar plots sort data automatically
2. Which of the following is the correct way to create a bar plot in pandas for columns 'Category' and 'Value' in DataFrame df?
easy
A. df.barplot('Category', 'Value')
B. df.plot.bar(y='Category', x='Value')
C. df.plot.bar('Value', 'Category')
D. df.plot.bar(x='Category', y='Value')

Solution

  1. Step 1: Recall pandas bar plot syntax

    The correct syntax is df.plot.bar(x='Category', y='Value') where x is the label and y is the height of bars.
  2. Step 2: Check other options for errors

    df.barplot('Category', 'Value') uses a non-existent method barplot, C swaps x and y incorrectly, D swaps x and y keyword arguments wrongly.
  3. Final Answer:

    df.plot.bar(x='Category', y='Value') -> Option D
  4. Quick Check:

    Use plot.bar with x and y named [OK]
Hint: Use df.plot.bar(x=..., y=...) to plot bars [OK]
Common Mistakes:
  • Using wrong method like barplot instead of plot.bar
  • Swapping x and y arguments
  • Not naming x and y parameters
3. Given the code:
import pandas as pd
import matplotlib.pyplot as plt
data = {'Fruit': ['Apple', 'Banana', 'Cherry'], 'Count': [10, 15, 7]}
df = pd.DataFrame(data)
df.plot.bar(x='Fruit', y='Count')
plt.show()

What will the bar plot display?
medium
A. A line plot connecting fruit counts
B. Bars showing counts of Apple=7, Banana=10, Cherry=15
C. Bars showing counts of Apple=10, Banana=15, Cherry=7
D. An error because plt.show() is missing

Solution

  1. Step 1: Understand the DataFrame content

    The DataFrame has fruits as categories and their counts: Apple=10, Banana=15, Cherry=7.
  2. Step 2: Analyze the bar plot code

    Using df.plot.bar(x='Fruit', y='Count') plots bars with fruit names on x-axis and counts as bar heights.
  3. Final Answer:

    Bars showing counts of Apple=10, Banana=15, Cherry=7 -> Option C
  4. Quick Check:

    Bar heights = Count values [OK]
Hint: Bar heights match y column values [OK]
Common Mistakes:
  • Mixing up fruit names and counts
  • Thinking plt.show() is optional (it is needed to display)
  • Confusing bar plot with line plot
4. What is wrong with this code snippet for creating a bar plot?
df.plot.bar('Category', 'Value')
medium
A. It will plot correctly with Category on x-axis and Value on y-axis
B. It will raise an error because x and y must be named arguments
C. It will swap x and y axes automatically
D. It will create a line plot instead of a bar plot

Solution

  1. Step 1: Check pandas plot.bar argument handling

    The plot.bar() method accepts positional arguments: first for x, second for y.
  2. Step 2: Verify the code

    df.plot.bar('Category', 'Value') correctly assigns x='Category' and y='Value', producing the intended bar plot.
  3. Final Answer:

    It will plot correctly with Category on x-axis and Value on y-axis -> Option A
  4. Quick Check:

    Positional x,y accepted in plot.bar [OK]
Hint: plot.bar accepts positional x,y args [OK]
Common Mistakes:
  • Believing x,y must always be named arguments
  • Expecting automatic axis swapping
  • Confusing bar plot with line plot
5. You have a DataFrame df with columns 'Product', 'Sales_Q1', 'Sales_Q2'. You want to create a grouped bar plot to compare sales in Q1 and Q2 for each product. Which code correctly creates this grouped bar plot?
hard
A. df.plot.bar(x='Product', y=['Sales_Q1', 'Sales_Q2'])
B. df.plot.bar(x='Product', y='Sales_Q1') df.plot.bar(x='Product', y='Sales_Q2')
C. df.plot.bar(x=['Sales_Q1', 'Sales_Q2'], y='Product')
D. df.plot.bar(y=['Sales_Q1', 'Sales_Q2'])

Solution

  1. Step 1: Understand grouped bar plot requirements

    To compare multiple columns side-by-side per category, pass y as a list of columns and x as the category column.
  2. Step 2: Analyze each option

    df.plot.bar(x='Product', y=['Sales_Q1', 'Sales_Q2']) correctly uses x='Product' and y=['Sales_Q1', 'Sales_Q2'] to create grouped bars. df.plot.bar(x='Product', y='Sales_Q1') df.plot.bar(x='Product', y='Sales_Q2') plots separately, not grouped. df.plot.bar(x=['Sales_Q1', 'Sales_Q2'], y='Product') swaps x and y incorrectly. df.plot.bar(y=['Sales_Q1', 'Sales_Q2']) misses x, so no category labels.
  3. Final Answer:

    df.plot.bar(x='Product', y=['Sales_Q1', 'Sales_Q2']) -> Option A
  4. Quick Check:

    Grouped bars need y as list and x as category [OK]
Hint: Use y=[col1, col2] and x=category for grouped bars [OK]
Common Mistakes:
  • Plotting columns separately instead of grouped
  • Swapping x and y arguments
  • Not specifying x for categories
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