0
0
Matplotlibdata~10 mins

Colorbar positioning 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.colorbar(orientation=[1])
plt.show()
Drag options to blanks, or click blank then click option'
Alocation='top'
Borientation='horizontal'
Corientation='vertical'
Dlocation='left'
Attempts:
3 left
💡 Hint
Common Mistakes
Using location parameter instead of orientation
Setting orientation to horizontal when vertical is default
2fill in blank
medium

Complete the code to place the colorbar on the right side of the plot.

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'
Aorientation='horizontal'
Blocation='bottom'
Clocation='right'
Dorientation='vertical'
Attempts:
3 left
💡 Hint
Common Mistakes
Using location='right' instead of orientation='vertical'
Setting orientation to horizontal which places colorbar at bottom
3fill in blank
hard

Fix the error in the code to correctly position the colorbar below the plot.

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'bottom'
C'right'
D'vertical'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'bottom' which is not a valid orientation value
Using 'vertical' which places colorbar on the side
4fill in blank
hard

Fill both blanks to create a horizontal colorbar below the plot with a label.

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

img = np.random.rand(10,10)
im = plt.imshow(img)
cbar = plt.colorbar(im, orientation=[1], [2]='bottom')
cbar.set_label('Intensity')
plt.show()
Drag options to blanks, or click blank then click option'
A'horizontal'
B'vertical'
Clocation
Dlabel
Attempts:
3 left
💡 Hint
Common Mistakes
Using vertical orientation with bottom location
Using label parameter instead of location
5fill in blank
hard

Fill all three blanks to create a vertical colorbar on the left side with a label and shrink it.

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

img = np.random.rand(10,10)
im = plt.imshow(img)
cbar = plt.colorbar(im, orientation=[1], [2]='left', shrink=[3])
cbar.set_label('Value')
plt.show()
Drag options to blanks, or click blank then click option'
A'horizontal'
B0.5
C'vertical'
Dlocation
Attempts:
3 left
💡 Hint
Common Mistakes
Using horizontal orientation with left location
Not using shrink parameter correctly
Using label instead of location