I want to create a new colormap which interpolates between green and blue (or any other two colours for that matter). My goal is to get something like:
First of all I am really not sure if this can be done using linear interpolation of blue and green. If it can, I'm not sure how to do so, I found some documentation on using a matplotlib method that interpolates specified RGB values here
The real trouble is understanding how "cdict2" works below. For the example the documentation says:
"Example: suppose you want red to increase from 0 to 1 over the bottom half, green to do the same over the middle half, and blue over the top half. Then you would use:"
from matplotlib import pyplot as plt
import matplotlib
import numpy as np
plt.figure()
a=np.outer(np.arange(0,1,0.01),np.ones(10))
cdict2 = {'red': [(0.0, 0.0, 0.0),
(0.5, 1.0, 1.0),
(1.0, 1.0, 1.0)],
'green': [(0.0, 0.0, 0.0),
(0.25, 0.0, 0.0),
(0.75, 1.0, 1.0),
(1.0, 1.0, 1.0)],
'blue': [(0.0, 0.0, 0.0),
(0.5, 0.0, 0.0),
(1.0, 1.0, 1.0)]}
my_cmap2 = matplotlib.colors.LinearSegmentedColormap('my_colormap2',cdict2,256)
plt.imshow(a,aspect='auto', cmap =my_cmap2)
plt.show()
EDIT: I now understand how the interpolation works, for example this will give a red to white interpolation:
White to red: Going down the columns of the "matrix" for each colour, in column one we have the xcoordinate of where we want the interpolation to start and end and the two other columns are the actual values for the colour value at that coordinate.
cdict2 = {'red': [(0.0, 1.0, 1.0),
(1.0, 1.0, 1.0),
(1.0, 1.0, 1.0)],
'green': [(0.0, 1.0, 1.0),
(1.0, 0.0, 0.0),
(1.0, 0.0, 0.0)],
'blue': [(0.0, 1.0, 1.0),
(1.0, 0.0, 0.0),
(1.0, 0.0, 0.0)]}
It is evident that the gradient I want will be very difficult to create by interpolating in RGB space...
This creates a colormap controlled by a single parameter,
y
:red
ramps up from 0 toy
and then back down to 0.green
ramps up from 0 toy
and then is constant.blue
stars aty
and is constant for the first half, then ramps down to 0.Here's the plot with
y = 0.7
:You could smooth it out by using adding another segment or two.
A simple answer I have not seen yet is to just use the colour package.
Install via pip
Use as so:
Change the inputs to any colors you want
I needed this as well, but I wanted to enter multiple arbitrary color points. Consider a heat map where you need black, blue, green... all the way up to "hot" colors. I borrowed Mark Ransom's code above and extended it to meet my needs. I'm very happy with it. My thanks to all, especially Mark.
This code is neutral to the size of the image (no constants in the gaussian distribution); you can change it with the width= parameter to pixel(). It also allows tuning the "spread" (-> stddev) of the distribution; you can muddle them up further or introduce black bands by changing the spread= parameter to pixel().
If you just need to interpolate in between 2 colors, I wrote a simple function for that.
fadeColor
creates you a hex color code out of two other hex color codes.result:
The first element of each tuple (0, 0.25, 0.5, etc) is the place where the color should be a certain value. I took 5 samples to see the RGB components (in GIMP), and placed them in the tables. The RGB components go from 0 to 1, so I had to divide them by 255.0 to scale the normal 0-255 values.
The 5 points are a rather coarse approximation - if you want a 'smoother' appearance, use more values.
Note that red is quite present. It's there because the center area approaches gray - where the three components are necessary.
This produces:
It's obvious that your original example gradient is not linear. Have a look at a graph of the red, green, and blue values averaged across the image:
Attempting to recreate this with a combination of linear gradients is going to be difficult.
To me each color looks like the addition of two gaussian curves, so I did some best fits and came up with this:
Using these calculated values lets me create a really pretty gradient that matches yours almost exactly.
Unfortunately I don't yet know how to generalize it to arbitrary colors.