Complete the code to add a colorbar to the plot.
import matplotlib.pyplot as plt import numpy as np img = np.random.rand(10,10) plt.imshow(img) plt.[1]() plt.show()
The colorbar() function adds a colorbar to the current plot, which shows the mapping of colors to data values.
Complete the code to set the colorbar orientation to horizontal.
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()
The orientation parameter controls the direction of the colorbar. Use 'horizontal' to make it horizontal.
Fix the error in the code to correctly link the colorbar to the image.
import matplotlib.pyplot as plt import numpy as np img = np.random.rand(10,10) im = plt.imshow(img) plt.colorbar([1]) plt.show()
The colorbar needs the image object returned by imshow(), which is stored in im.
Fill both blanks to create a colorbar with ticks at 0, 0.5, and 1 and label it 'Intensity'.
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()
The ticks parameter sets the positions of ticks on the colorbar. The set_label() method adds a label to the colorbar.
Fill all three blanks to create a colorbar with a label 'Temperature', orientation vertical, and shrink it to 0.8 size.
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()
Set orientation to 'vertical' for vertical colorbar, shrink to 0.8 to reduce size, and label it with 'Temperature'.