Gnuplot 在打印期间更改线颜色

Gnuplot 在打印期间更改线颜色,gnuplot,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

此命令从数据文件中绘制漂亮的绿色正弦波,但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,\
我运气不太好

使用此选项:

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
将线型11线条颜色rgb设置为“绿色”
设置线型12线条颜色rgb“红色”
设置扩展数据时间
设置时间表“%Y-%m-%d”
设置格式x“%s”
设置表格“data-smoothd1.txt”
设置样本1000个
使用1:4平滑线绘制“data.txt”
未设置的表
设置时间表“%s”
设置格式x“%Y-%m-%d”
设置X轴为零
使用1:4:($4<0?12:11)linecolor变量绘制“data smoothed.txt”,且线条没有标题

它抱怨xrange是错误的,所以当我添加一个包含开始日期和结束日期的范围时,它仍然没有帮助。“01”列是月数。

您必须使用
linecolor变量
,该变量允许您提供一个指定要使用的linecolor的附加列。但这并不直接适用于
平滑
,因此必须首先将平滑数据写入外部文件(使用gnuplot 5,您也可以使用herdeoc),然后使用
linecolor变量
打印此文件

执行此操作时,还必须注意写入平滑数据时使用的时间格式。格式取自
设置格式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
将线型11线条颜色rgb设置为“绿色”
设置线型12线条颜色rgb“红色”
设置扩展数据时间
设置时间表“%Y-%m-%d”
设置格式x“%s”
设置表格“数据平滑.txt”
设置样本1000个
使用1:2平滑线绘制“data.txt”
未设置的表
设置时间表“%s”
设置格式x“%Y-%m-%d”
设置X轴为零
使用1:2:($2<0?12:11)linecolor变量和lines notitle打印“data smoothed.txt”

通过阅读GN文档和测试您的回复,我无法判断数据文件中是否需要实际列。您的示例看起来应该可以很好地工作,但它抛出了以下警告:
平滑选项忽略了额外的列
它还以红色绘制整行。关于编辑:请查看文件
data smooted.txt
的内容。您将看到,它只包含两列,因此您必须使用第1列和第2列(而不是第1列和第4列),并且脚本中的最后一行必须与我的相同:
使用1:2:($2<0?12:11)行无标题的linecolor变量打印“data Smootherd.txt”。
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