Reputation: 527
This command draws nice green sine waves from a data file, but the col4 value goes negative periodically.
"" u (myDateSP(1,2)):4 lc rgb "green" lt 1 lw 6 sm cspl notitle,\
I'd like to have it draw the line red for negative values of col4 and green for positive values and it seems as if I should be able to use the ternary operator for that but I'm having problems with the grammar:
"" u (myDateSP(1,2)):4:($4<= 0) ? lc rgb "red" lt 1 lw 6 sm cspl notitle,\:lc rgb "green" lt 1 lw 6 sm cspl notitle,\
and other variants throw errors. Can someone point out where I have this wrong?
Your table example works nicely, many thanks.
I tried to apply it to a data file like this:
2015-01-03 18:02 01 29.49 feet High Tide
2015-01-04 01:12 01 -2.29 feet Low Tide
2015-01-04 07:02 01 29.09 feet High Tide
2015-01-04 13:22 01 4.54 feet Low Tide
2015-01-04 18:41 01 29.80 feet High Tide
2015-01-04 19:54 01 Full Moon
2015-01-05 01:52 01 -1.87 feet Low Tide
2015-01-05 07:36 01 29.30 feet High Tide
2015-01-05 14:00 01 4.51 feet Low Tide
2015-01-05 19:17 01 29.99 feet High Tide
2015-01-06 02:26 01 -1.30 feet Low Tide
and am not having much luck.
Using this:
set linetype 11 linecolor rgb "green"
set linetype 12 linecolor rgb "red"
set xdata time
set timefmt '%Y-%m-%d'
set format x '%s'
set table 'data-smoothed1.txt'
set samples 1000
plot 'data.txt' using 1:4 smooth cspline
unset table
set timefmt '%s'
set format x '%Y-%m-%d'
set xzeroaxis
plot 'data-smoothed.txt' using 1:4:($4 < 0 ? 12 : 11) linecolor variable with lines no title
It complains that the xrange is wrong, so when I add an range with the beginning and ending dates, it still doesn't help. The "01" column is the month number.
Upvotes: 2
Views: 2698
Reputation: 48390
You must use linecolor variable
, which allows you to give an additional column which specifies the linecolor to use. But that doesn't work directly with smooth
, so that you must first write the smoothed data to an external file (with gnuplot 5 you can also use a heredoc) and then plot this file with linecolor variable
.
When doing this, you must also take care of which time format is used to write the smoothed data. The format is taken from the set format x
settings. I would use %s
, i.e. Unix timestamp.
Here is a full, self-contained example showing the different steps:
An example file data.txt
:
2014-02-11 2
2014-03-12 -1
2014-04-15 3
2014-05-22 -2
And the script
set linetype 11 linecolor rgb "green"
set linetype 12 linecolor rgb "red"
set xdata time
set timefmt '%Y-%m-%d'
set format x '%s'
set table 'data-smoothed.txt'
set samples 1000
plot 'data.txt' using 1:2 smooth cspline
unset table
set timefmt '%s'
set format x '%Y-%m-%d'
set xzeroaxis
plot 'data-smoothed.txt' using 1:2:($2 < 0 ? 12 : 11) linecolor variable with lines notitle
Upvotes: 1