0
0
Matplotlibdata~10 mins

Colorbar formatting in Matplotlib - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to add a colorbar to the plot.

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

img = np.random.rand(10,10)
plt.imshow(img)
plt.[1]()
plt.show()
Drag options to blanks, or click blank then click option'
Axlabel
Bcolorbar
Ctitle
Dlegend
Attempts:
3 left
💡 Hint
Common Mistakes
Using plt.legend() instead of plt.colorbar()
Trying to add xlabel or title for colorbar
2fill in blank
medium

Complete the code to set the colorbar orientation to horizontal.

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

img = np.random.rand(10,10)
plt.imshow(img)
plt.colorbar(orientation=[1])
plt.show()
Drag options to blanks, or click blank then click option'
A'horizontal'
B'sideways'
C'vertical'
D'updown'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'vertical' when horizontal is needed
Using invalid strings like 'sideways'
3fill in blank
hard

Fix the error in the code to set the colorbar label.

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

img = np.random.rand(10,10)
plt.imshow(img)
cbar = plt.colorbar()
cbar.[1]('Intensity')
plt.show()
Drag options to blanks, or click blank then click option'
AsetLabel
Blabel
Cset_label
Dsettitle
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'label' attribute instead of method
Using camelCase like 'setLabel'
4fill in blank
hard

Fill both blanks to create a colorbar with ticks at 0, 0.5, and 1 and format ticks as percentages.

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

img = np.random.rand(10,10)
plt.imshow(img)
cbar = plt.colorbar(ticks=[1])
cbar.ax.yaxis.set_major_formatter([2](1))
plt.show()
Drag options to blanks, or click blank then click option'
A[0, 0.5, 1]
BPercentFormatter
CFuncFormatter
D[0, 1]
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong tick list like [0,1]
Using FuncFormatter without defining a function
5fill in blank
hard

Fill all three blanks to create a horizontal colorbar with label 'Value', ticks at 0, 0.25, 0.5, 0.75, 1, and format ticks as percentages.

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

img = np.random.rand(10,10)
plt.imshow(img)
cbar = plt.colorbar(orientation=[1], ticks=[2])
cbar.[3]('Value')
plt.show()
Drag options to blanks, or click blank then click option'
A'vertical'
B[0, 0.25, 0.5, 0.75, 1]
Cset_label
D'horizontal'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up orientation strings
Forgetting to set label with set_label()