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) plt.imshow(img) plt.colorbar(orientation=[1]) plt.show()
The orientation parameter controls the colorbar direction. Use 'horizontal' for a horizontal colorbar.
Fix the error in the code to set the colorbar label.
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()
The correct method to set a colorbar label is set_label(). Other options are incorrect or do not exist.
Fill both blanks to create a colorbar with ticks at 0, 0.5, and 1 and format ticks as percentages.
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()
Use ticks=[0, 0.5, 1] to set tick locations. Use PercentFormatter(1) to format ticks as percentages.
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.
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()
Set orientation='horizontal' for horizontal colorbar, ticks=[0, 0.25, 0.5, 0.75, 1] for tick positions, and use set_label('Value') to label the colorbar.