What is the output of the following code snippet that uses FuncFormatter to format y-axis ticks as percentages?
import matplotlib.pyplot as plt from matplotlib.ticker import FuncFormatter fig, ax = plt.subplots() ax.plot([0, 1, 2], [0.1, 0.5, 0.9]) formatter = FuncFormatter(lambda x, _: f'{x*100:.0f}%') ax.yaxis.set_major_formatter(formatter) ticks = [tick.get_text() for tick in ax.yaxis.get_major_ticks()] print(ticks)
Think about how the formatter multiplies the tick value by 100 and formats it as a percentage with no decimals.
The FuncFormatter multiplies each tick value by 100 and formats it as an integer percentage. The default major ticks on the y-axis are at 0.0, 0.2, 0.4, 0.6, 0.8, and 1.0, so the formatted labels become '0%', '20%', '40%', '60%', '80%', and '100%'.
After applying a custom tick formatter that formats x-axis ticks as integers with a prefix 'Val-', how many major ticks will be shown on the x-axis for the following plot?
import matplotlib.pyplot as plt from matplotlib.ticker import FuncFormatter fig, ax = plt.subplots() ax.plot(range(10), range(10)) formatter = FuncFormatter(lambda x, _: f'Val-{int(x)}') ax.xaxis.set_major_formatter(formatter) num_ticks = len(ax.get_xticks()) print(num_ticks)
Consider the default ticks matplotlib generates for the range 0 to 9.
Matplotlib by default places 11 ticks for the range 0 to 9 (including 0 and 10). The formatter only changes the label appearance, not the number of ticks. So the number of ticks remains 11.
What error will the following code raise when setting a custom tick formatter?
import matplotlib.pyplot as plt from matplotlib.ticker import FuncFormatter fig, ax = plt.subplots() ax.plot([1, 2, 3], [4, 5, 6]) def formatter(x): return f'Value: {x:.1f}' ax.yaxis.set_major_formatter(FuncFormatter(formatter))
Check the expected signature of the function passed to FuncFormatter.
The function passed to FuncFormatter must accept two arguments: the tick value and the tick position. The provided formatter function only accepts one argument, so Python raises a TypeError because it is called with two arguments.
Given the code below, what will the y-axis tick labels look like?
import matplotlib.pyplot as plt from matplotlib.ticker import FuncFormatter import numpy as np fig, ax = plt.subplots() ax.plot(np.logspace(0.1, 2, 10)) formatter = FuncFormatter(lambda y, _: f'{y:.1e}') ax.set_yscale('log') ax.yaxis.set_major_formatter(formatter) labels = [tick.get_text() for tick in ax.yaxis.get_major_ticks()] print(labels)
Remember that the formatter formats the tick values in scientific notation with one decimal place.
The y-axis is set to log scale, so the major ticks are at powers of 10 (1, 10, 100). The formatter formats these values in scientific notation with one decimal place, resulting in labels like '1.0e+00', '1.0e+01', and '1.0e+02'.
You want to display x-axis ticks representing time in seconds as MM:SS format (minutes and seconds). Which of the following FuncFormatter functions will correctly format the ticks?
Remember the formatter function must accept two arguments and format minutes and seconds with leading zeros.
Option C correctly defines a function with two arguments (required by FuncFormatter) and formats the time in MM:SS with leading zeros using int() and :02d formatting. Other options either have wrong arguments, missing leading zeros, or incorrect rounding.