import matplotlib.pyplot as plt import matplotlib.colors as colors # Define the two colors colors_list = ['#FFFFFF', '#0000FF'] # Define the normalized values for each color n_bins = 10 breakpoints = [0, 1] bounds = [i/n_bins for i in range(n_bins+1)] # Create the colormap object cmap = colors.LinearSegmentedColormap.from_list('', list(zip(bounds, colors_list))) # Plot a heatmap with the custom colormap plt.imshow(data, cmap=cmap) plt.colorbar() plt.show()
import numpy as np import matplotlib.pyplot as plt import matplotlib.colors as colors # Define the four colors colors_list = ['#0000FF', '#00FF00', '#FFFF00', '#FF0000'] # Define the normalized values for each color n_bins = 10 breakpoints = [0, 1, 2, 3] bounds = [i/n_bins for i in range(n_bins+1)] # Define the data points and their corresponding color values n = 100 x = np.random.rand(n) y = np.random.rand(n) c = np.random.randint(0, 4, n) # Create the colormap object cmap = colors.LinearSegmentedColormap.from_list('', list(zip(bounds, colors_list))) # Plot a scatter plot with the custom colormap plt.scatter(x, y, c=c, cmap=cmap) plt.colorbar() plt.show()In this example, we create a custom colormap with four colors (blue, green, yellow, and red) that will be used to color code the data points in a scatter plot. We define the normalized values and list of colors, and use the `from_list` method to create the colormap object. Then, we plot the data points as a scatter plot with the `c` parameter set to the color values and `cmap` set to our custom colormap object.