I want to use the gnuplot color map in Python. The attached picture is generated in gnuplot using the surface plot command. But I don't know much about gnuplot and its colormaps.
How can I make a colormap in matplotlib similar to this gnuplot colormap? Gnuplot commands for shown picture are as follows. But it is not possible to provide the files used in the code.
set palette rgb 32,3,36 negative
set style line 1 lc rgb "blue" pt 6 ps 0.6
set style line 2 lc rgb "blue" pt 6 ps 0.5
set style line 3 lt 2 lc rgb "red"
set style line 4 lc rgb "black" lw 0.5
set style line 5 lc rgb "blue" pt 7 ps 0.2
Any help would be appreciated.
The colormap is defined in Gnuplot as set palette rgb 32,3,36 negative
, so it's a colormap in which (see the output of show palette rgbformulae
)
Matplotlib provides LinearSegmentedColormap
to deal with this type of colormap definition, below you will find how to define a colormap matching the one defined in Gnuplot
In [62]: %reset -fs
...: import numpy as np
...: import matplotlib.pyplot as plt
...: from matplotlib.colors import LinearSegmentedColormap
...:
...: cdata = {'red':[[0, 0, 0], [0.25, 1, 1], [0.42, 1, 1], [0.92, 0, 0], [1, 1, 1]],
...: 'green':[[0, 0, 0], [1, 1, 1]],
...: 'blue':[[0,0,0], [0.5,0,0], [1,1,1]]}
...: gnuplot_cm = LinearSegmentedColormap('test', cdata)
...:
...: np.random.seed(1)
...: data = np.random.randn(30, 30)
...: fig, ax = plt.subplots(figsize=(4, 3), constrained_layout=True)
...: cmesh = ax.pcolormesh(data, cmap=gnuplot_cm, vmin=-2, vmax=2)
...: fig.colorbar(cmesh, ax=ax)
...: plt.show()
I have just learned of the Gnuplot test palette
command
gnuplot> set palette rgb 32, 3, 36 negative
gnuplot> test palette
gnuplot>
NTSC is an approximation to the perceived luminosity of the individual color (in this case it's a nice monotonous function)
After (and only after…) you issue a test palette
command, you can print the values of the palette as 'z R G B NTSC' tuples
gnuplot> print $PALETTE
0.0000 1.0000 1.0000 1.0000 1.0000
0.0039 0.9510 0.9961 0.9922 0.9821
0.0078 0.9020 0.9922 0.9843 0.9643
...
0.9922 0.0314 0.0078 0.0000 0.0140
0.9961 0.0157 0.0039 0.0000 0.0070
1.0000 0.0000 0.0000 0.0000 0.0000
gnuplot>