import matplotlib.pyplot as plt import numpy as np x = np.random.rand(100) y = np.random.rand(100) colors = np.random.rand(100) plt.scatter(x, y, c=colors) plt.colorbar() plt.show()
import matplotlib.pyplot as plt import numpy as np x = np.linspace(-2, 2, 100) y = np.linspace(-2, 2, 100) X, Y = np.meshgrid(x, y) Z = np.exp(-(X**2 + Y**2)) plt.contourf(X, Y, Z, 20) plt.colorbar() plt.show()In this example, we have used the contourf method to create a contour plot of a 2D array. We have passed the X, Y, and Z arrays to the contourf method along with the number of levels. Finally, we have called the colorbar method to add the colorbar to the plot. In both examples, we have used the Matplotlib package library to create the plots and add the colorbars.