0
0
Matplotlibdata~20 mins

Colorbar formatting in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Colorbar Formatting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this colorbar tick labels code?

Consider this code snippet that creates a colorbar with custom tick labels. What will be the printed list of tick labels?

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()
img = ax.imshow(np.random.rand(5,5), cmap='viridis')
cbar = fig.colorbar(img)
cbar.set_ticks([0.2, 0.5, 0.8])
ticks = cbar.ax.get_yticklabels()
labels = [tick.get_text() for tick in ticks]
print(labels)
A['0', '0.5', '1']
B['0.20', '0.50', '0.80']
C['', '', '']
D['0.2', '0.5', '0.8']
Attempts:
2 left
💡 Hint

Remember that get_yticklabels() returns label objects which may not have text set until drawn.

data_output
intermediate
2:00remaining
How many ticks will this colorbar display?

This code creates a colorbar with a fixed number of ticks. How many ticks will appear on the colorbar?

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()
img = ax.imshow(np.random.rand(10,10), cmap='plasma')
cbar = fig.colorbar(img, ticks=np.linspace(0,1,5))
print(len(cbar.ax.get_yticks()))
A4
B6
C10
D5
Attempts:
2 left
💡 Hint

Check how many values are in np.linspace(0,1,5).

🔧 Debug
advanced
2:00remaining
Why does this colorbar label formatting code raise an error?

Look at this code that tries to format colorbar tick labels. What error does it raise?

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()
img = ax.imshow(np.random.rand(5,5), cmap='cool')
cbar = fig.colorbar(img)
cbar.ax.yaxis.set_major_formatter('{:.2f}')
ATypeError: 'str' object is not callable
BAttributeError: 'str' object has no attribute 'format'
CValueError: Invalid format string
DNo error, runs fine
Attempts:
2 left
💡 Hint

Check what type set_major_formatter expects as argument.

🚀 Application
advanced
3:00remaining
Which code correctly formats colorbar ticks as percentages?

You want to show colorbar tick labels as percentages (e.g., 20%, 50%). Which option correctly formats the colorbar ticks?

Matplotlib
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import FuncFormatter

fig, ax = plt.subplots()
img = ax.imshow(np.random.rand(5,5), cmap='inferno')
cbar = fig.colorbar(img)
Acbar.ax.yaxis.set_major_formatter(FuncFormatter(lambda x, _: f'{int(x*100)}%'))
B
cbar.formatter = FuncFormatter(lambda x, _: f'{x*100}%')
cbar.update_ticks()
Ccbar.ax.yaxis.set_major_formatter('{:.0%}')
D
cbar.set_ticks([0, 0.5, 1])
cbar.ax.set_yticklabels(['0%', '50%', '100%'])
Attempts:
2 left
💡 Hint

Use FuncFormatter to create a custom label format function.

🧠 Conceptual
expert
2:00remaining
What is the effect of calling cbar.ax.invert_yaxis() on a vertical colorbar?

In matplotlib, what happens when you call invert_yaxis() on the colorbar's axis?

AThe colorbar disappears from the plot.
BThe colorbar colors are reversed vertically, so high values appear at the bottom.
CThe colorbar tick labels are rotated 180 degrees but colors stay the same.
DThe colorbar tick labels become bold but colors stay the same.
Attempts:
2 left
💡 Hint

Think about what inverting the y-axis means visually.