There's more...

Previously, we have learned how to create discrete user-defined colormaps. Now let's see how to create user-defined continuous colormaps. The difference is in the color specification. In discrete colormaps, we will have as many RGB tuples as the number of clusters we have in data, so that each color represents one cluster. However, in continuous colormaps, the color has to move gradually from one end of the spectrum to the other, along the complete data range.

As we know, each color is represented with a tuple of three numbers: R, G, and B channels. In some cases, the fourth channel, alpha, is also used to represent the transparency (or opaqueness) of the colors. Here is how we need to specify the colors. Let's take an example:

cdict1 = {'red':   ((0.0, 0.0, 0.25), (0.5, 0.4, 0.4), (1.0, 0.8,  
1.0)),
'green': ((0.0, 0.0, 0.25), (0.25, 0.5, 0.5), (0.75, 0.75,
0.75), (1.0, 0.9, 1.0)),
'blue': ((0.0, 0.0, 0.25), (0.5, 0.5, 0.5), (1.0, 0.75,
1.0))}

Let's say the first item in each of the tuples is x, and the second one is y0, and the third one is y1. In each color combination, x has to move from 0.0 to 1.0 gradually. Transition from the top to bottom rows has to happen gradually from y1 (top row) to y0 (next row), y0 to y1 on the same row, and again y1 to yo of the next row should be gradual. In the case of red, x moved from 0.0, to 0.5 and then to 1.0. Similarly, y1 moved from 0.25 to 0.4, 0.4, 0.8, which again is gradual. We can have as many tuples as we want in each of the colors. In this case, red and blue colors have three each, whereas green has four.

Similarly, we have defined three other color dictionaries including one with alpha as well, just to demonstrate all possible combinations:

cdict2 = {'red' : ((0.0, 0.0, 0.0), (0.5, 0.0, 1.0), (1.0, 0.1, 1.0)),
'green': ((0.0, 0.0, 0.0), (1.0, 0.0, 0.0)),
'blue': ((0.0, 0.0, 0.1), (0.5, 0.5, 0.5), (1.0, 0.8, 0.8)) }
cdict3 = {'red': ((0.0, 0.0, 0.0), (0.25, 0.0, 0.0), (0.5, 0.8, 1.0),
(0.75, 1.0, 1.0),(1.0, 0.4, 1.0)),
'green': ((0.0, 0.0, 0.0), (0.25, 0.0, 0.0), (0.5, 0.9, 0.9),
(0.75, 0.0, 0.0), (1.0, 0.0, 0.0)),
'blue': ((0.0, 0.0, 0.4), (0.25, 1.0, 1.0), (0.5, 1.0, 0.8),
(0.75, 0.0, 0.0), (1.0, 0.0, 0.0))}
cdict4 = {'red': ((0.0, 0.0, 0.5), (0.5, 0.8, 1.0), (0.75, 1.0, 1.0),
(1.0, 0.9, 1.0)),
'green': ((0.0, 0.0, 0.5), (0.25, 0.75, 0.0), (0.5, 0.9,
0.9), (1.0, 0.0, 0.0)),
'blue': ((0.0, 0.0, 0.4), (0.25, 1.0, 1.0), (0.75, 0.0, 0.0),
(1.0, 0.0, 0.0)),
'alpha': ((0.0, 1.0, 1.0), (0.5, 0.4, 0.4), (1.0, 0.7, 1.0)) }

The following code block plots four colormaps, using four color dictionaries as defined previously. It is similar to what we have seen in the case of discrete colormaps.

Read the Iris dataset and the map class names of the numeric codes:

iris = pd.read_csv('iris_dataset.csv', delimiter=',')
iris['species'] = iris['species'].map({"setosa" : 0, "versicolor" : 1,
"virginica" : 2})

Define the figure, layout, size, and adjust the space in between the plots:

fig, axs = plt.subplots(1,4, figsize=(16,6))
fig.subplots_adjust(left=0.0, bottom=0.0, right=0.95, top=0.94,
wspace=0.4)

Define a function to plot the charts:

def plot_graph(name, dictionary, axs, cbaxs):
custom = LinearSegmentedColormap(name, dictionary)
im = axs.scatter(iris.petal_length, iris.petal_width, s=100*iris.petal_length*iris.petal_width,
c=iris.species, cmap=custom)
caxs = plt.axes(cbaxs) # left, bottom, width and height
fig.colorbar(im, caxs)

Plot the graphs for each of the color combinations, and display it on the screen:

plot_graph('custom1', cdict1, axs[0], [0.2, 0.01, 0.01, 0.93])
plot_graph('custom2', cdict2, axs[1], [0.45, 0.01, 0.01, 0.93])
plot_graph('custom3', cdict3, axs[2], [0.7, 0.01, 0.01, 0.93])
plot_graph('custom4', cdict4, axs[3], [0.97, 0.01, 0.01, 0.93])
plt.show()

Here is what the output plots look like: