0
0
Matplotlibdata~20 mins

Colorbar positioning in Matplotlib - Practice Problems & Coding Challenges

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

Consider the following code that creates a heatmap and adds a colorbar. Where will the colorbar appear?

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()
data = np.random.rand(5,5)
cax = ax.imshow(data, cmap='viridis')
fig.colorbar(cax, ax=ax, orientation='vertical', fraction=0.046, pad=0.04)
plt.show()
AThe colorbar is placed to the right of the heatmap, vertically aligned.
BThe colorbar is placed below the heatmap, horizontally aligned.
CThe colorbar is placed inside the heatmap, overlapping the data.
DThe colorbar is placed to the left of the heatmap, vertically aligned.
Attempts:
2 left
💡 Hint

Check the orientation parameter and default colorbar placement.

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

Given this code snippet, how many tick labels will the colorbar display?

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()
data = np.linspace(0, 1, 100).reshape(10,10)
cax = ax.imshow(data, cmap='plasma')
cbar = fig.colorbar(cax, ax=ax, ticks=[0, 0.25, 0.5, 0.75, 1])
plt.show()
A10 tick labels
BNo tick labels
C4 tick labels
D5 tick labels
Attempts:
2 left
💡 Hint

Look at the ticks argument passed to colorbar.

visualization
advanced
2:30remaining
Identify the colorbar position from the plot layout

Below is a plot with a heatmap and a colorbar. The colorbar is positioned using ax_divider.append_axes. Where is the colorbar located?

Matplotlib
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.axes_grid1 import make_axes_locatable

fig, ax = plt.subplots()
data = np.random.rand(6,6)
cax = ax.imshow(data, cmap='coolwarm')
divider = make_axes_locatable(ax)
cbar_ax = divider.append_axes('top', size='5%', pad=0.1)
fig.colorbar(cax, cax=cbar_ax, orientation='horizontal')
plt.show()
AThe colorbar is below the heatmap, horizontally aligned.
BThe colorbar is above the heatmap, horizontally aligned.
CThe colorbar is to the right of the heatmap, vertically aligned.
DThe colorbar is inside the heatmap, overlapping the top-left corner.
Attempts:
2 left
💡 Hint

Check the append_axes position argument.

🔧 Debug
advanced
2:30remaining
Why does this colorbar not appear?

Examine the code below. The heatmap is shown but the colorbar does not appear. What is the cause?

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()
data = np.random.rand(4,4)
cax = ax.imshow(data, cmap='magma')
fig.colorbar(cax, orientation='horizontal', fraction=0.1, pad=0.05)
plt.show()
AThe colorbar is too small and placed outside the figure area, so it is not visible.
BThe colorbar orientation 'horizontal' is invalid and causes it not to render.
CThe colorbar is not linked to the axes, so it does not display.
DThe colormap 'magma' does not support colorbars.
Attempts:
2 left
💡 Hint

Check the fraction and pad parameters and figure size.

🚀 Application
expert
3:00remaining
How to place two colorbars on opposite sides of a heatmap?

You want to create a heatmap with two colorbars: one on the left and one on the right side of the plot. Which code snippet correctly adds both colorbars?

Matplotlib
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.axes_grid1 import make_axes_locatable

fig, ax = plt.subplots()
data = np.random.rand(8,8)
cax = ax.imshow(data, cmap='viridis')
divider = make_axes_locatable(ax)
# Add left colorbar
cbar_left_ax = divider.append_axes('left', size='5%', pad=0.1)
# Add right colorbar
cbar_right_ax = divider.append_axes('right', size='5%', pad=0.1)

# Add colorbars
fig.colorbar(cax, cax=cbar_left_ax)
fig.colorbar(cax, cax=cbar_right_ax)
plt.show()
AThis code will raise an error because only one colorbar can be linked to one image.
BThe colorbars will appear but both on the right side due to default behavior.
CThis code correctly adds two colorbars on the left and right sides of the heatmap.
DThe colorbars will overlap because the axes are not resized properly.
Attempts:
2 left
💡 Hint

Check how make_axes_locatable and append_axes work for multiple colorbars.