Creating custom colormaps

We can set up our own colormap. This is useful when customizing heatmaps and surface plots.
A simple way to create a custom linear colormap is to prepare a list of colors and allow Matplotlib to handle the transition. Let's look at the following example:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors

# Create a 30 random dots
np.random.seed(52)
x,y,c = zip(*np.random.rand(30,3))

# Create a custom linear colormap
cmap = matplotlib.colors.LinearSegmentedColormap.from_list("", ["red","yellow","green"])

plt.scatter(x,y,c=c, cmap=cmap)
plt.colorbar()
plt.show()

Here, we have a scatter plot with a colormap set up on our own, morphing from red through yellow to green:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.67.0