0
0
Matplotlibdata~10 mins

Colorbar configuration 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'
Atitle
Blegend
Cxlabel
Dcolorbar
Attempts:
3 left
💡 Hint
Common Mistakes
Using plt.legend() instead of plt.colorbar()
Trying to add xlabel instead of 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)
im = plt.imshow(img)
plt.colorbar(im, orientation=[1])
plt.show()
Drag options to blanks, or click blank then click option'
A'horizontal'
B'vertical'
C'sideways'
D'diagonal'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'vertical' which is the default
Using invalid strings like 'sideways'
3fill in blank
hard

Fix the error in the code to correctly link the colorbar to the image.

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

img = np.random.rand(10,10)
im = plt.imshow(img)
plt.colorbar([1])
plt.show()
Drag options to blanks, or click blank then click option'
Aim
Bimg
Cplt
Dnp
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the data array instead of the image object
Passing the module names like plt or np
4fill in blank
hard

Fill both blanks to create a colorbar with ticks at 0, 0.5, and 1 and label it 'Intensity'.

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

img = np.random.rand(10,10)
im = plt.imshow(img)
cbar = plt.colorbar(im, ticks=[1])
cbar.set_label([2])
plt.show()
Drag options to blanks, or click blank then click option'
A[0, 0.5, 1]
B'Intensity'
C'Value'
D[0, 1]
Attempts:
3 left
💡 Hint
Common Mistakes
Using ticks as a string instead of a list
Forgetting quotes around the label string
5fill in blank
hard

Fill all three blanks to create a colorbar with a label 'Temperature', orientation vertical, and shrink it to 0.8 size.

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

img = np.random.rand(10,10)
im = plt.imshow(img)
plt.colorbar(im, orientation=[1], shrink=[2]).set_label([3])
plt.show()
Drag options to blanks, or click blank then click option'
A'horizontal'
B0.8
C'Temperature'
D'vertical'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong orientation string
Passing shrink as string instead of float
Forgetting quotes around label