A. The percent function should multiply by 100 and add % sign as string
B. The percent function should return a string with % sign, but it returns a float
C. The percent function is missing the pos parameter
D. The FuncFormatter is applied to xaxis instead of yaxis
Solution
Step 1: Examine the percent function
The function returns f"{x}%", e.g., for x=0.1 it returns "0.1%", which does not properly represent a percentage.
Step 2: Identify the required change
For percentage formatting, the function should multiply x by 100 before appending the % sign, e.g., "10%" for 0.1.
Final Answer:
The percent function should multiply by 100 and add % sign as string -> Option A
Quick Check:
Percent formatters multiply by 100 [OK]
Hint: Formatter functions must return strings, not numbers [OK]
Common Mistakes:
Returning numbers instead of strings from formatter
Forgetting to multiply by 100 for percentages
Missing required function parameters
5. You want to display x-axis ticks as dates in 'Month-Day' format using a custom formatter. Which approach correctly applies this using FuncFormatter?
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter
import datetime
dates = [datetime.date(2024, 6, d) for d in range(1, 6)]
values = [10, 20, 15, 25, 30]
fig, ax = plt.subplots()
ax.plot(dates, values)
# Which formatter function and application is correct?
hard
A. def format_date(x, pos):
return dates[int(x)].strftime('%m-%d')
ax.xaxis.set_major_formatter(FuncFormatter(format_date))
B. def format_date(x, pos):
return str(x)
ax.xaxis.set_major_formatter(FuncFormatter(format_date))
C. def format_date(x, pos):
return x.strftime('%m-%d')
ax.xaxis.set_major_formatter(FuncFormatter(format_date))
D. def format_date(x, pos):
return datetime.datetime.fromordinal(int(x)).strftime('%m-%d')
ax.xaxis.set_major_formatter(FuncFormatter(format_date))
Solution
Step 1: Understand x values in date plots
Matplotlib converts dates to float numbers representing days since a fixed date (ordinal). So x is a float ordinal number.
Step 2: Convert ordinal to date string
Use datetime.datetime.fromordinal(int(x)) to convert float x to a date object, then format with strftime('%m-%d').
Step 3: Check options
def format_date(x, pos):
return datetime.datetime.fromordinal(int(x)).strftime('%m-%d')
ax.xaxis.set_major_formatter(FuncFormatter(format_date)) correctly converts x to date and formats it. def format_date(x, pos):
return dates[int(x)].strftime('%m-%d')
ax.xaxis.set_major_formatter(FuncFormatter(format_date)) incorrectly indexes dates list by int(x) which may cause index errors. def format_date(x, pos):
return x.strftime('%m-%d')
ax.xaxis.set_major_formatter(FuncFormatter(format_date)) tries to call strftime on float x, causing error. def format_date(x, pos):
return str(x)
ax.xaxis.set_major_formatter(FuncFormatter(format_date)) returns string of float, not formatted date.
Final Answer:
def format_date(x, pos):
return datetime.datetime.fromordinal(int(x)).strftime('%m-%d')
ax.xaxis.set_major_formatter(FuncFormatter(format_date)) -> Option D
Quick Check:
Date ticks need ordinal to date conversion [OK]
Hint: Convert float ticks to dates with fromordinal before formatting [OK]
Common Mistakes:
Using tick value as list index causing errors
Calling date methods on float values
Returning raw float strings instead of formatted dates
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.