0

I want to plot a matrix with colors with gnuplot, and I'm using the following code:

set terminal png
set output "Matrix.png"
set title "Matrix"
set autoscale xfix
set autoscale yfix
plot "Lattice.txt" matrix with image

Lattice.txt can be filled with 0, 1, 2, 3, 4 and 5.

The code as it works for the most part, but if in lattice.txt there are only 0 and 1, it will assign the black color to 0. If I have 1, 2 and 3 in Lattice.txt, it will assign the black color to 1.

How can I choose a color for each number so it always outputs, for example, black when there is a 0, red when there is a 1, blue when there is a 2?

Thanks for your time.

1 Answer 1

2
set palette define (0 "black", 0 "black", 1 "red", 1 "red", 2 "blue", 2 "blue", 3 "orange", 3 "orange", 4 "gold", 4 "gold", 5 "green", 5 "green")
set cbrange [0:5]

[modified to show that for this case a simpler palette definition is OK]

set palette define (0 "black", 1 "red", 2 "blue", 3 "orange", 4 "gold", 5 "green")
set cbrange [0:5]
2
  • This is exactly what I wanted and it works perfectly, thanks! But I'm wondering why we need to define the colors two times?
    – Ovog
    Nov 3, 2018 at 2:18
  • I just tested, and for this example I think you only need one entry per color. In general the command wants ranges, as in x->y is green y->z is blue, and so on. But since you only have integer values, the color that would have gone to intermediate fractional values is not relevant.
    – Ethan
    Nov 3, 2018 at 2:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.