Complete the code to create a scatter plot with colors mapped from the 'values' array.
import matplotlib.pyplot as plt import numpy as np x = np.arange(10) y = np.random.rand(10) values = np.linspace(0, 1, 10) plt.scatter(x, y, c=[1]) plt.show()
The c parameter in plt.scatter sets the colors of the points. We use the values array to map colors.
Complete the code to add a colorbar to the scatter plot.
import matplotlib.pyplot as plt import numpy as np x = np.arange(10) y = np.random.rand(10) values = np.linspace(0, 1, 10) scatter = plt.scatter(x, y, c=values) [1] plt.show()
To add a colorbar for a scatter plot, pass the scatter object to plt.colorbar().
Fix the error in the code to correctly map colors and add a colorbar.
import matplotlib.pyplot as plt import numpy as np x = np.arange(5) y = np.random.rand(5) values = np.array([10, 20, 30, 40, 50]) scatter = plt.scatter(x, y, c=[1]) plt.colorbar(scatter) plt.show()
The c parameter should be the array of values to map colors, which is values.
Fill both blanks to create a scatter plot with a 'viridis' colormap and add a colorbar.
import matplotlib.pyplot as plt import numpy as np x = np.arange(8) y = np.random.rand(8) values = np.linspace(0, 1, 8) scatter = plt.scatter(x, y, c=values, cmap=[1]) [2] plt.show()
Use cmap='viridis' to set the colormap. Pass the scatter object to plt.colorbar() to add the colorbar.
Fill all three blanks to create a scatter plot with a 'coolwarm' colormap, set the color limits from 0 to 1, and add a colorbar.
import matplotlib.pyplot as plt import numpy as np x = np.arange(6) y = np.random.rand(6) values = np.linspace(0, 1, 6) scatter = plt.scatter(x, y, c=values, cmap=[1]) scatter.set_clim([2], [3]) plt.colorbar(scatter) plt.show()
Set cmap='coolwarm' for the colormap. Use scatter.set_clim(0, 1) to set color limits. Then add the colorbar.