Create a Custom Colormap and Color Scale in Matplotlib
Problem:
Design a custom colormap that transitions smoothly from red to violet to blue, mapping to values between -2 and 2. Utilize the colormap to color points in a plot and display the associated color scale.
Implementation:
import numpy as np import matplotlib.pyplot as plt import matplotlib.colors # Generate random data x, y, c = zip(*np.random.rand(30, 3) * 4 - 2) # Create a custom colormap colors = ["red", "violet", "blue"] norm = plt.Normalize(-2, 2) cmap = matplotlib.colors.LinearSegmentedColormap.from_list("", colors) # Plot using custom colormap plt.scatter(x, y, c=c, cmap=cmap, norm=norm) # Add color scale plt.colorbar() plt.show()
Explanation:
Additional Considerations:
The above is the detailed content of How to Create a Custom Colormap and Color Scale in Matplotlib?. For more information, please follow other related articles on the PHP Chinese website!