Sjoerd222888
Sjoerd222888

Reputation: 3476

gnuplot: filledcurves with color from data

I would like to use filledcurves with palette.

My data looks like this:

0 0 0 1 1 1 1 0 0.5
2 2 2 3 3 3 3 2 0.6

My plotting commands:

set terminal pngcairo size 800,600
set output "test.png"
set style fill solid 1.0 noborder
unset border
unset xtics
unset ytics
plot '< awk ''{print $1,$2,$9,"\n",$3,$4,$9,"\n",$5,$6,$9,"\n",$7,$8,$9,"\n",$1,$2,$9,"\n"}'' rect.txt' \
using 1:2:3 with filledcurves fillcolor palette notitle

This unfortunately only yields black squares where as I would like them to be colored. Is there a way to achieve correctly colored figures with gnuplot?

EDIT:

Using as suggested:

set terminal pngcairo size 800,600
set output "test.png"
set style fill solid 1.0 noborder
unset border
unset xtics
unset ytics
file = 'rect.txt'
lines = system(sprintf('wc -l %s | cut -d'' '' -f 1', file))
frac(x) = system(sprintf('awk ''{if (NR==%d){ print $9 }}'' %s', x, file))
rect(x) = sprintf('awk ''{if (NR==%d){ print $1,$2,$4,"\n",$5,$8,$6,"\n"}}'' %s', x, file)
plot for [i=1:lines] '< '.rect(i) using 1:2:3 with filledcurves palette frac frac(i) notitle

with the content of rect.txt:

0 0 0 1 1 1 1 0 0.5
2 2 2 3 3 3 3 2 0.6
2 2 2 1 1 1 1 2 0.2

yields the following result: enter image description here

Upvotes: 1

Views: 1460

Answers (1)

Christoph
Christoph

Reputation: 48390

The filledcurves plotting style doesn't support coloring by palette with data from a file. The main issue here is, that usually, when using palette, one wants to change the color within a single area depending on some data values.

To color a single rectangle taking the color value from a fractional position of the palette, you can use palette frac <value>. This requires you to iterate over all rectangles.

And for proper use with filledcurves, you need a different data format, like

x1 y1_low y1_high
x2 x2_low y2_high

Since you are using awk anyways, you can extract some more information for the actual plotting:

set terminal pngcairo size 800,600
set output "test.png"
set style fill solid 1.0 noborder
unset border
unset xtics
unset ytics
file = 'rect.txt'
lines = system(sprintf('wc -l %s | cut -d'' '' -f 1', file))
frac(x) = system(sprintf('awk ''{if (NR==%d){ print $9 }}'' %s', x, file))
rect(x) = sprintf('awk ''{if (NR==%d){ print $1,$2,$4,"\n",$5,$8,$6,"\n"}}'' %s', x, file)
plot for [i=1:lines] '< '.rect(i) using 1:2:3 with filledcurves palette frac frac(i) notitle

With the data file

0 0 0 1 1 1 1 0 0.5
2 2 2 3 3 3 3 2 0.6
2 2 2 1 1 1 1 2 0.2

you get the output

enter image description here

Upvotes: 1

Related Questions