0
0
Matplotlibdata~20 mins

Colorbar configuration in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Colorbar Configuration 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 orientation code?

Consider this matplotlib code that creates a colorbar with a specific orientation. What will be the orientation of the colorbar?

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()
img = ax.imshow(np.random.rand(5,5))
cbar = fig.colorbar(img, orientation='horizontal')
print(cbar.orientation)
Anone
Bdiagonal
Cvertical
Dhorizontal
Attempts:
2 left
💡 Hint

Check the orientation parameter passed to fig.colorbar().

data_output
intermediate
2:00remaining
How many ticks are shown on this colorbar?

This code creates a colorbar with 5 ticks. How many tick labels will be shown 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))
cbar = fig.colorbar(img, ticks=[0, 0.25, 0.5, 0.75, 1])
print(len(cbar.ax.get_yticklabels()))
A4
B6
C5
D0
Attempts:
2 left
💡 Hint

The number of tick labels matches the number of ticks specified in the ticks list.

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

Examine the code below. It raises an error when run. What is the cause?

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()
img = ax.imshow(np.random.rand(5,5))
cbar = fig.colorbar(img, orientation='sideways')
AValueError because 'sideways' is not a valid orientation
BSyntaxError due to invalid orientation value
CTypeError because colorbar expects a string but got None
DNo error, code runs fine
Attempts:
2 left
💡 Hint

Check the allowed values for the orientation parameter.

visualization
advanced
2:00remaining
Which option produces a colorbar with ticks inside the plot area?

Given the following code snippet, which option correctly sets the colorbar ticks to appear inside the plot area?

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()
img = ax.imshow(np.random.rand(5,5))
cbar = fig.colorbar(img)
# Set ticks inside here
Acbar.ax.tick_params(direction='in')
Bcbar.set_ticks_position('none')
Ccbar.set_ticks_position('outside')
Dcbar.ax.tick_params(direction='out')
Attempts:
2 left
💡 Hint

Look for the parameter controlling tick direction.

🚀 Application
expert
3:00remaining
How to create a colorbar with a custom label and fixed size?

You want to add a vertical colorbar to your plot with the label 'Intensity' and fix its width to 0.1 of the figure width. Which code snippet achieves this?

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()
img = ax.imshow(np.random.rand(10,10))
Acbar = fig.colorbar(img, label='Intensity', fraction=0.1)
B
cbar = fig.colorbar(img, fraction=0.1)
cbar.set_label('Intensity')
C
cbar = fig.colorbar(img)
cbar.set_label('Intensity')
cbar.ax.set_aspect(0.1)
D
cbar = fig.colorbar(img, fraction=0.1, orientation='horizontal')
cbar.set_label('Intensity')
Attempts:
2 left
💡 Hint

Check the fraction parameter for size and use set_label for labeling.