gnuplot 更改连接线的颜色

问题描述 投票:0回答:4

我正在使用 gnuplot 进行以下操作。我有 n 个方程,我想根据 x 轴值绘制它们。这是一个样本

set xrange[0:25]
f1(x) = x
f2(x) = 3*x
f3(x) = 10*x
plot (x>0)&&(x<10)?f1(x):(x<20)?f2(x):f3(x)

我知道我们可以使用下面的内容轻松设置线条的颜色。但它改变了整个颜色

set style line 1 lt 1 lw 3 pt 3 lc rgb "blue"

但我想要的是使连接线具有不同的颜色。也就是说,如果你绘制上面的图表,你将得到 5 条线。 3 条原始线(来自函数)和 2 条连接它们的线(几乎垂直的线)。我想改变连接线的颜色。

注1:这些函数是由程序自动生成的,函数数量可能会很大。甚至精确的绘图命令也会自动生成

注 2:我想要一种方法来区分我的原始线和连接原始线的插值线。

如有任何帮助,我们将不胜感激

graph colors gnuplot
4个回答
4
投票

您实际上拥有的是分段定义的一条线,并且没有一种简单的方法可以在 gnuplot 中为分段线内的线段定义颜色。

简单方法(绘制数据文件)

我建议制作一个如下所示的数据文件:

# x y color
0   0   0
10  10  0
10  10  1
10  30  1
10  30  0
20  60  0
20  60  1
20  200 1
20  200 0
25  250 0

注意 x=10 和 x=20 处的双点。这样线段就会在过渡处相交。

现在用

linecolor variable
绘制它:

#!/usr/bin/env gnuplot

reset

set terminal pdfcairo enhanced color dashed rounded lw 5 size 3,2 font 'Arial,14'
set output 'output2.pdf'

set style data lines
set key top left
set tics scale 0.5 out nomirror

plot 'data.dat' u 1:2:3 lc variable

看起来像这样:

enter image description here

您可以更改调色板 (

set palette
) 来确定颜色,如果需要,数据文件中可以有 2 个以上的颜色值。

较难的方法(仅适用于少数片段)

您可以定义 2n-1 条单独的线并将它们连接起来:

#!/usr/bin/env gnuplot

reset

set terminal pdfcairo enhanced color dashed rounded lw 5 size 3,2 font 'Arial,14'
set output 'output.pdf'

set style data lines
set key top left
set tics scale 0.5 out nomirror

# points every 0.001 units in the range 0:25
set samples 25001

# main lines
f1(x) = (x <= 9.999)                   ? x    : 1/0
f3(x) = (x >= 10.001) && (x <= 19.999) ? 3*x  : 1/0
f5(x) = (x >= 20.001)                  ? 10*x : 1/0

# define slopes and y-offsets of connecting lines
m2 = (f3(10.001)-f1(9.999))/0.002
b2 = (30.0-10.0)/2.0 + 10.0
m4 = (f5(20.001)-f3(19.999))/0.002
b4 = (200.0-60.0)/2.0 + 60.0

# connecting functions
f2(x) = (x >= 9.999) && (x <= 10.001)  ? m2*(x-10) + b2 : 1/0
f4(x) = (x >= 19.999) && (x <= 20.001) ? m4*(x-20) + b4 : 1/0

plot [0:25] f1(x), f2(x), f3(x), f4(x), f5(x)

看起来像这样:

enter image description here


3
投票

您可以定义一个辅助函数来定义函数的断点,该断点会自动为右侧的线条着色。下面的代码很容易扩展到不同的函数和断点(即,您只需更改

x1
x2
)。添加多个点也很简单。

xmin=0.
xmax=25.
x0=0.
x1=10.
x2=20.
nsample=200.


dx=(xmax-xmin)/nsample
print dx
set xrange[xmin:xmax]
set sample nsample
f1(x) = x
f2(x) = 3*x
f3(x) = 10*x
f4(x) = (x>x0)&&(x<x1)?f1(x):(x<x2)?f2(x):f3(x)
f5(x) = x
f5(x) = ( (x>x1&&x<=x1+dx) || (x>x2&&x<=x2+dx) )?1:0

set cbrange [0:1]
unset key

plot '+' using 1:(f4($1)):(f5($1)) lc variable with lines

并不是说我使用了特殊的文件名

'+'
,它只是构建了一个具有等空间数据点的数据文件(以下
nsample
)。

Sample output


2
投票

如果可以跳过连接线,那么您可以使用@andyras第二个变体的简化版本。只需将超出指定范围的所有函数定义为

1/0
即可:

set style data lines
unset key

f1(x) = (x > 0) && (x < 10) ? x : 1/0
f2(x) = (x > 10) && (x < 20) ? 3*x : 1/0
f3(x) = (x > 20) ? 10*x : 1/0

plot [0:25] f1(x), f2(x), f3(x)

还有另一种可能性。这假设您可以选择足够高的采样,以便连接函数的“跳跃”始终大于函数内部:

set style data lines
unset key

set xrange[0:25]
f1(x) = x
f2(x) = 3*x
f3(x) = 10*x
f(x) = ( (x>0)&&(x<10)?f1(x):(x<20)?f2(x):f3(x) )

set samples 1000

curr = 0
prev = 0
lim = 1
plot '+' using (prev = curr, curr=f($1), $1):(f($1)):(abs(curr-prev) < lim ? 0 : 1) lc var

enter image description here


0
投票

在阅读评论时,我不确定添加另一个解决方案是否明智......
但为了好玩......另一个与给定的解决方案不同的解决方案:

  • 不需要具有 3 列的数据文件(@andyras)
  • 不依赖于
    set samples
  • 不需要设置
    dx
    lim
    ,这取决于样本(@Bernhard,@Christoph)

简单的想法是在变量

c1
中告诉 gnuplot 您当前位于函数的哪一部分,如果要从一个块转到下一个块,则更改颜色。所以,这是 @andyras 和 @Christoph 解决方案的紧凑组合。

脚本:(适用于 gnuplot>=4.4.0,2010 年 3 月)

### plot transitions between piecewise function in different color
reset

set xrange[0:25]
f1(x) = x
f2(x) = 3*x
f3(x) = 10*x

x1 = 10   # break points for piecewise function
x2 = 20

f(x)  = x<x1 ? (c1=1, f1(x)) : x<x2 ? (c1=2,f2(x)) : (c1=3,f3(x))

plot c1=1 '+' u (c0=c1,$1):(f($1)):(c0==c1 ? 0x0000ff : 0xff0000) w l lc rgb var notitle
### end of script

结果:

标准

set samples
100
。这就是红线不完全垂直的原因。你可以让它们“更垂直”,例如如果你
set samples 1000
.

© www.soinside.com 2019 - 2024. All rights reserved.