Bird
Raised Fist0
Pandasdata~20 mins

Plot customization (title, labels, figsize) in Pandas - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Plot Customization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the size of the plot created by this code?
Consider this code that creates a plot with pandas. What is the size of the figure in inches?
Pandas
import pandas as pd
import matplotlib.pyplot as plt

data = {'A': [1, 3, 2, 4]}
df = pd.DataFrame(data)

ax = df.plot(figsize=(8, 4))
plt.close()  # Close plot to avoid display
A[6, 6]
B[4, 8]
C[8, 4]
D[10, 5]
Attempts:
2 left
💡 Hint
The figsize parameter expects a tuple (width, height) in inches.
❓ Predict Output
intermediate
2:00remaining
What is the title of the plot after running this code?
This code creates a plot and sets a title. What will be the exact title shown on the plot?
Pandas
import pandas as pd
import matplotlib.pyplot as plt

data = {'B': [5, 2, 7, 1]}
df = pd.DataFrame(data)

ax = df.plot()
ax.set_title('Sales Over Time')
plt.close()
ASales Over Time
Bsales over time
CSales over time
DSALES OVER TIME
Attempts:
2 left
💡 Hint
Titles are case sensitive and appear exactly as set.
❓ data_output
advanced
2:00remaining
What are the x and y axis labels after this code runs?
This code sets labels for x and y axes. What are the labels shown on the plot axes?
Pandas
import pandas as pd
import matplotlib.pyplot as plt

data = {'C': [10, 20, 15, 25]}
df = pd.DataFrame(data)

ax = df.plot()
ax.set_xlabel('Month')
ax.set_ylabel('Revenue')
plt.close()
Ax label: Month, y label: Revenue
Bx label: revenue, y label: month
Cx label: Month, y label: month
Dx label: Revenue, y label: Month
Attempts:
2 left
💡 Hint
Labels are set exactly by set_xlabel and set_ylabel methods.
❓ visualization
advanced
2:00remaining
Which option shows the correct plot title and axis labels after this code?
This code creates a plot with a title and axis labels. Which option correctly describes the plot's title and labels?
Pandas
import pandas as pd
import matplotlib.pyplot as plt

data = {'D': [3, 6, 9, 12]}
df = pd.DataFrame(data)

ax = df.plot(figsize=(5, 3))
ax.set_title('Quarterly Growth')
ax.set_xlabel('Quarter')
ax.set_ylabel('Growth (%)')
plt.close()
ATitle: quarterly growth, X label: Growth (%), Y label: Quarter
BTitle: Quarterly Growth, X label: Quarter, Y label: Growth (%)
CTitle: Quarterly Growth, X label: Growth (%), Y label: Quarter
DTitle: Growth Quarterly, X label: Quarter, Y label: Growth (%)
Attempts:
2 left
💡 Hint
Check the exact strings used in set_title, set_xlabel, and set_ylabel.
🔧 Debug
expert
2:00remaining
What error does this code raise when trying to set the plot size?
This code tries to set the figure size but causes an error. What is the error type?
Pandas
import pandas as pd
import matplotlib.pyplot as plt

data = {'E': [1, 2, 3, 4]}
df = pd.DataFrame(data)

ax = df.plot(figsize=[6, 4])
plt.close()
ATypeError
BValueError
CAttributeError
DNo error, runs fine
Attempts:
2 left
💡 Hint
Check if figsize accepts list or tuple.

Practice

(1/5)
1. What does the figsize parameter control when creating a plot with pandas?
easy
A. The type of plot (line, bar, scatter).
B. The color of the plot background.
C. The font size of the plot title.
D. The size of the plot figure in inches (width, height).

Solution

  1. Step 1: Understand the role of figsize

    The figsize parameter sets the width and height of the plot in inches.
  2. Step 2: Differentiate from other parameters

    Color, font size, and plot type are controlled by other parameters, not figsize.
  3. Final Answer:

    The size of the plot figure in inches (width, height). -> Option D
  4. Quick Check:

    Figsize = plot size in inches [OK]
Hint: Figsize sets plot width and height in inches [OK]
Common Mistakes:
  • Confusing figsize with color or font size
  • Thinking figsize changes plot type
  • Using figsize as a string instead of tuple
2. Which of the following is the correct way to set the plot title and axis labels in pandas?
easy
A. df.plot().set_title('Sales').set_xlabel('Month').set_ylabel('Revenue')
B. df.plot().set(title='Sales', xlabel='Month', ylabel='Revenue')
C. df.plot(title='Sales', x_label='Month', y_label='Revenue')
D. df.plot().title('Sales').xlabel('Month').ylabel('Revenue')

Solution

  1. Step 1: Recall pandas plot method chaining

    After plotting, you can chain set_title(), set_xlabel(), and set_ylabel() to set title and labels.
  2. Step 2: Check syntax correctness

    df.plot().set_title('Sales').set_xlabel('Month').set_ylabel('Revenue') uses correct method names and chaining syntax. df.plot(title='Sales', x_label='Month', y_label='Revenue') tries to pass labels directly to plot() which is invalid. df.plot().set(title='Sales', xlabel='Month', ylabel='Revenue') uses set() with wrong keys. df.plot().title('Sales').xlabel('Month').ylabel('Revenue') uses non-existent methods.
  3. Final Answer:

    df.plot().set_title('Sales').set_xlabel('Month').set_ylabel('Revenue') -> Option A
  4. Quick Check:

    Use set_title, set_xlabel, set_ylabel methods [OK]
Hint: Use set_title, set_xlabel, set_ylabel after plot() [OK]
Common Mistakes:
  • Passing title or labels directly to plot()
  • Using wrong method names like title() instead of set_title()
  • Trying to set labels with set() method
3. Given the code below, what will be the size of the plot figure?
import pandas as pd
import matplotlib.pyplot as plt

data = {'A': [1, 3, 2, 4]}
df = pd.DataFrame(data)

ax = df.plot(figsize=(8, 4))
plt.show()
medium
A. 8 inches wide and 4 inches tall
B. 4 inches wide and 8 inches tall
C. Default size because figsize is ignored
D. 8 pixels wide and 4 pixels tall

Solution

  1. Step 1: Understand figsize parameter

    The figsize argument takes a tuple (width, height) in inches.
  2. Step 2: Check the given tuple

    The tuple is (8, 4), so width is 8 inches and height is 4 inches.
  3. Final Answer:

    8 inches wide and 4 inches tall -> Option A
  4. Quick Check:

    Figsize=(8,4) means width=8, height=4 inches [OK]
Hint: Figsize tuple is (width, height) in inches [OK]
Common Mistakes:
  • Swapping width and height values
  • Thinking figsize uses pixels
  • Assuming figsize is ignored by pandas
4. Identify the error in the following code that tries to set the plot title and labels:
import pandas as pd
data = {'x': [1, 2, 3], 'y': [4, 5, 6]}
df = pd.DataFrame(data)
plot = df.plot()
plot.title('My Plot')
plot.xlabel('X-axis')
plot.ylabel('Y-axis')
medium
A. DataFrame is missing a column for labels.
B. Methods title(), xlabel(), ylabel() do not exist for the plot object.
C. Plotting requires plt.show() to display labels.
D. The DataFrame columns must be strings to set labels.

Solution

  1. Step 1: Check method names on plot object

    The plot object returned by pandas uses set_title(), set_xlabel(), and set_ylabel() methods, not title(), xlabel(), or ylabel().
  2. Step 2: Identify the error

    Calling plot.title() causes an AttributeError because the method does not exist.
  3. Final Answer:

    Methods title(), xlabel(), ylabel() do not exist for the plot object. -> Option B
  4. Quick Check:

    Use set_title, set_xlabel, set_ylabel methods [OK]
Hint: Use set_title, set_xlabel, set_ylabel methods [OK]
Common Mistakes:
  • Using title() instead of set_title()
  • Forgetting to call plt.show() (though not error here)
  • Assuming DataFrame columns affect label setting
5. You want to create a bar plot from a DataFrame df with columns 'Year' and 'Sales'. You want the plot to be 10 inches wide and 6 inches tall, with the title 'Annual Sales', x-axis label 'Year', and y-axis label 'Sales'. Which code snippet correctly achieves this?
hard
A. ax = df.plot.bar(x='Year', y='Sales') plt.title('Annual Sales') plt.xlabel('Year') plt.ylabel('Sales') plt.figure(figsize=(10,6))
B. df.plot.bar(x='Year', y='Sales', title='Annual Sales', x_label='Year', y_label='Sales', figsize=(10,6))
C. ax = df.plot.bar(x='Year', y='Sales', figsize=(10,6)) ax.set_title('Annual Sales') ax.set_xlabel('Year') ax.set_ylabel('Sales')
D. df.plot.bar(x='Year', y='Sales') ax.set_title('Annual Sales') ax.set_xlabel('Year') ax.set_ylabel('Sales') plt.figure(figsize=(10,6))

Solution

  1. Step 1: Set figure size correctly

    Figure size must be set in the plot.bar() call using figsize=(10,6). ax = df.plot.bar(x='Year', y='Sales', figsize=(10,6)) ax.set_title('Annual Sales') ax.set_xlabel('Year') ax.set_ylabel('Sales') does this correctly.
  2. Step 2: Set title and axis labels on the Axes object

    ax = df.plot.bar(x='Year', y='Sales', figsize=(10,6)) ax.set_title('Annual Sales') ax.set_xlabel('Year') ax.set_ylabel('Sales') uses ax.set_title(), ax.set_xlabel(), and ax.set_ylabel() which is the correct way to set labels after plotting.
  3. Step 3: Check other options

    df.plot.bar(x='Year', y='Sales', title='Annual Sales', x_label='Year', y_label='Sales', figsize=(10,6)) tries to pass title and labels directly to plot.bar(), which is not supported. ax = df.plot.bar(x='Year', y='Sales') plt.title('Annual Sales') plt.xlabel('Year') plt.ylabel('Sales') plt.figure(figsize=(10,6)) sets figure size after plotting, which has no effect. df.plot.bar(x='Year', y='Sales') ax.set_title('Annual Sales') ax.set_xlabel('Year') ax.set_ylabel('Sales') plt.figure(figsize=(10,6)) uses ax without defining it and sets figure size after plotting, which is incorrect.
  4. Final Answer:

    ax = df.plot.bar(x='Year', y='Sales', figsize=(10,6)) ax.set_title('Annual Sales') ax.set_xlabel('Year') ax.set_ylabel('Sales') -> Option C
  5. Quick Check:

    Set figsize in plot call; use set_title and set_xlabel on Axes [OK]
Hint: Set figsize in plot call; use set_title and set_xlabel on Axes [OK]
Common Mistakes:
  • Passing title and labels directly to plot.bar()
  • Setting figure size after plotting
  • Using undefined Axes object
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