在绘图期间更改线条颜色

时间:2015-04-11 18:24:43

标签: gnuplot

此命令从数据文件中绘制漂亮的绿色正弦波,但col4值会定期变为负值。

"" u (myDateSP(1,2)):4 lc rgb "green" lt 1 lw 6 sm cspl notitle,\

我想让它为col4的负值绘制红色,为正值绘制绿色,似乎我应该能够使用三元运算符,但我遇到了语法问题:

"" 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,\

和其他变种抛出错误。有人能指出我错在哪里吗?

您的表格示例效果很好,非常感谢。

我尝试将它应用于这样的数据文件:

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 

并且没有太多运气。

使用此:

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

它抱怨xrange是错误的,所以当我添加一个带有开始和结束日期的范围时,它仍然无济于事。 “01”列是月份编号。

1 个答案:

答案 0 :(得分:1)

您必须使用linecolor variable,这允许您提供一个额外的列,用于指定要使用的线色。但这不能直接与smooth一起使用,因此您必须先将平滑后的数据写入外部文件(使用gnuplot 5,您也可以使用heredoc),然后使用linecolor variable绘制此文件。

执行此操作时,还必须注意使用哪种时间格式来编写平滑数据。格式取自set format x设置。我会使用%s,即Unix时间戳。

以下是一个完整的,自包含的示例,显示了不同的步骤:

示例文件data.txt

2014-02-11 2
2014-03-12 -1
2014-04-15 3
2014-05-22 -2

脚本

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

enter image description here

相关问题