Search code examples
plotgnuplotcontour

Filled contour plot with constant color between contour lines


I have followed the example here for generating a filled contour plot using gnuplot. The gnuplot commands and output are:

reset
f(x,y)=sin(1.3*x)*cos(.9*y)+cos(.8*x)*sin(1.9*y)+cos(y*.2*x)
set xrange [-5:5]
set yrange [-5:5]
set isosample 250, 250
set table 'test.dat'
splot f(x,y)
unset table

set contour base
set cntrparam level incremental -3, 0.5, 3
unset surface
set table 'cont.dat'
splot f(x,y)
unset table

reset
set xrange [-5:5]
set yrange [-5:5]
unset key
set palette rgbformulae 33,13,10
p 'test.dat' with image, 'cont.dat' w l lt -1 lw 1.5

Filled contour plot generated using gnuplot.

This method generates a very smooth filled contour plot. How can I modify this method so that the color between the contour lines is constant? For example, I would like for it to look similar to the output of this MATLAB script:

clc; clear all; close all;

Nx = 250;
Ny = 250;
x = linspace(-5,5,Nx);
y = linspace(-5,5,Ny);
[X,Y] = meshgrid(x,y);

f = sin(1.3*X).*cos(.9*Y) + cos(.8*X).*sin(1.9*Y) + cos(Y.*.2.*X);

levels = -3:0.5:3;
figure;
contourf(X,Y,f,levels);
colorbar;

Filled contour plot generated using MATLAB.


Solution

  • gnuplot's set palette option comes with a maxcolors setting. Hence for your case, as you have 12 lines, you should add

    set palette maxcolors 12