Gnuplot:渐变色箭头

Gnuplot:渐变色箭头,gnuplot,Gnuplot,我想画一个箭头,不是单一的颜色,而是沿着其长度的颜色梯度。有人知道如何做到这一点吗?创建以红色开头、以蓝色结尾的箭头的一些伪代码: set palette defined (0 "red", 1 "blue") set cbr [0:1] set arrow from 0,0 to 1,1 linecolor palette cb [0:1] # this doesn't work 使用行调色板可以对行进行颜色编码。使用第二个命令,可以通过set arrow或plotvector命令设置头部

我想画一个箭头,不是单一的颜色,而是沿着其长度的颜色梯度。有人知道如何做到这一点吗?创建以红色开头、以蓝色结尾的箭头的一些伪代码:

set palette defined (0 "red", 1 "blue")
set cbr [0:1]
set arrow from 0,0 to 1,1 linecolor palette cb [0:1] # this doesn't work

使用
行调色板
可以对行进行颜色编码。使用第二个命令,可以通过
set arrow
或plot
vector
命令设置头部

set palette defined (0 "red", 1 "blue")
set cbr [0:1]

set arrow 1 from 0.9,0.9 to 1,1 lc "blue"
plot sample [t=0:1] "+" us (t):(t):(t) w l palette


因此,需要两个命令。箭头有单色,您必须指定。

除了@Friedrich的解决方案,我想建议一个更一般的解决方案(尽管更复杂)

我想你想在箭头之外画些别的东西。 如果您的图形需要使用调色板,我想您遇到了“麻烦”,因为我不确定gnuplot是否在plot命令中支持多个调色板 (见附件)。因此,您必须自己实现箭头的调色板(参见示例)。如果要使用三次Bézier检查()执行弯曲箭头

代码:

### arrow with color gradient (besides other palette in plot)
reset session

array A[4] = [-4,-2,4,2]   # arrow coordinates x0,y0,x1,y1
Ax(t) = A[1] + t*(A[3]-A[1])
Ay(t) = A[2] + t*(A[4]-A[2])
AColorStart = 0xff0000   # red
AColorEnd =   0x0000ff   # blue
r(c) = (c & 0xff0000)>>16
g(c) = (c & 0x00ff00)>>8
b(c) = (c & 0x0000ff)
AColor(t) = ((int(r(AColorStart)*(1-t)+r(AColorEnd)*t))<<16) + \
            ((int(g(AColorStart)*(1-t)+g(AColorEnd)*t))<<8)  + \
              int(b(AColorStart)*(1-t)+b(AColorEnd)*t)

array AHead[1]   # dummy array for plotting a single point, here: arrow head
set angle degrees
set style arrow 1 lw 3 lc rgb var size 0.5,15 fixed

set palette grey

plot '++' u 1:2:($1*$2) w image notitle, \
     [0:0.99] '+' u (Ax($1)):(Ay($1)):(AColor($1)) w l lw 3 lc rgb var notitle,\
     AHead u (Ax(0.99)):(Ay(0.99)):(Ax(1)-Ax(0.99)):(Ay(1)-Ay(0.99)):(AColor($1)) w vec as 1 notitle
### end of code
### multiple arrows each with different color gradients (besides other palette in plot)
reset session

# define palettes
set print $myPalettes
    test palette                # get default palette into datablock $PALETTE
    print $PALETTE              # add palette to $myPalettes
    set palette rgb 33,13,10    # define next palette
    test palette                # get palette into datablock $PALETTE
    print $PALETTE              # add palette to $myPalettes
    set palette defined (0 "blue", 1 "black", 2 "red")   # define next palette
    test palette                                         # get palette into datablock $PALETTE
    print $PALETTE                                       # add palette to $myPalettes
set print
    
ColorComp(p,t,c) = int(word($myPalettes[p*257+int(255*t)+1],c+1)*0xff)
AColor(p,t) = (ColorComp(p,t,1)<<16) + (ColorComp(p,t,2)<<8) + ColorComp(p,t,3)
           
set size ratio -1
set angle degrees
unset key
set style arrow 1 lw 3 lc rgb var size 0.5,15 fixed
array AHead[1]      # dummy array for plotting a single point, here: arrow head
set palette grey    # yet another palette for the background

# x0 y0 x1  y1  paletteNo
$Arrows <<EOD
-4  -4   4   0   0
-4  -2   4   2   1
-4   0   4   4   2
EOD

Ax(i,t) = word($Arrows[i],1) + t*(word($Arrows[i],3)-word($Arrows[i],1))
Ay(i,t) = word($Arrows[i],2) + t*(word($Arrows[i],4)-word($Arrows[i],2))
Palette(i) = int(word($Arrows[i],5))

plot '++' u 1:2:($1*$2) w image, \
     for [i=1:|$Arrows|] [0:0.99:0.01] '+' u (Ax(i,$1)):(Ay(i,$1)):(AColor(Palette(i),$1)) w l lw 4 lc rgb var, \
     for [i=1:|$Arrows|] AHead u (Ax(i,0.99)):(Ay(i,0.99)): \
         (Ax(i,1)-Ax(i,0.99)):(Ay(i,1)-Ay(i,0.99)):(AColor(Palette(i),$1)) w vec as 1
### end of code
结果:

### arrow with color gradient (besides other palette in plot)
reset session

array A[4] = [-4,-2,4,2]   # arrow coordinates x0,y0,x1,y1
Ax(t) = A[1] + t*(A[3]-A[1])
Ay(t) = A[2] + t*(A[4]-A[2])
AColorStart = 0xff0000   # red
AColorEnd =   0x0000ff   # blue
r(c) = (c & 0xff0000)>>16
g(c) = (c & 0x00ff00)>>8
b(c) = (c & 0x0000ff)
AColor(t) = ((int(r(AColorStart)*(1-t)+r(AColorEnd)*t))<<16) + \
            ((int(g(AColorStart)*(1-t)+g(AColorEnd)*t))<<8)  + \
              int(b(AColorStart)*(1-t)+b(AColorEnd)*t)

array AHead[1]   # dummy array for plotting a single point, here: arrow head
set angle degrees
set style arrow 1 lw 3 lc rgb var size 0.5,15 fixed

set palette grey

plot '++' u 1:2:($1*$2) w image notitle, \
     [0:0.99] '+' u (Ax($1)):(Ay($1)):(AColor($1)) w l lw 3 lc rgb var notitle,\
     AHead u (Ax(0.99)):(Ay(0.99)):(Ax(1)-Ax(0.99)):(Ay(1)-Ay(0.99)):(AColor($1)) w vec as 1 notitle
### end of code
### multiple arrows each with different color gradients (besides other palette in plot)
reset session

# define palettes
set print $myPalettes
    test palette                # get default palette into datablock $PALETTE
    print $PALETTE              # add palette to $myPalettes
    set palette rgb 33,13,10    # define next palette
    test palette                # get palette into datablock $PALETTE
    print $PALETTE              # add palette to $myPalettes
    set palette defined (0 "blue", 1 "black", 2 "red")   # define next palette
    test palette                                         # get palette into datablock $PALETTE
    print $PALETTE                                       # add palette to $myPalettes
set print
    
ColorComp(p,t,c) = int(word($myPalettes[p*257+int(255*t)+1],c+1)*0xff)
AColor(p,t) = (ColorComp(p,t,1)<<16) + (ColorComp(p,t,2)<<8) + ColorComp(p,t,3)
           
set size ratio -1
set angle degrees
unset key
set style arrow 1 lw 3 lc rgb var size 0.5,15 fixed
array AHead[1]      # dummy array for plotting a single point, here: arrow head
set palette grey    # yet another palette for the background

# x0 y0 x1  y1  paletteNo
$Arrows <<EOD
-4  -4   4   0   0
-4  -2   4   2   1
-4   0   4   4   2
EOD

Ax(i,t) = word($Arrows[i],1) + t*(word($Arrows[i],3)-word($Arrows[i],1))
Ay(i,t) = word($Arrows[i],2) + t*(word($Arrows[i],4)-word($Arrows[i],2))
Palette(i) = int(word($Arrows[i],5))

plot '++' u 1:2:($1*$2) w image, \
     for [i=1:|$Arrows|] [0:0.99:0.01] '+' u (Ax(i,$1)):(Ay(i,$1)):(AColor(Palette(i),$1)) w l lw 4 lc rgb var, \
     for [i=1:|$Arrows|] AHead u (Ax(i,0.99)):(Ay(i,0.99)): \
         (Ax(i,1)-Ax(i,0.99)):(Ay(i,1)-Ay(i,0.99)):(AColor(Palette(i),$1)) w vec as 1
### end of code

哇,到目前为止,我还没有想到很多有趣的结构。在我的例子中,只有一个调色板,但手工解决方案仍然很好。我有一个关于矢量图的问题:绘制了多少个箭头?我想这取决于采样密度?对于非常高的值,可以更有效地添加单个短箭头,如@Friedrich proposed。很高兴听到这有帮助。默认采样率为100。所以,是的,最好只画一个向量。最小采样率为2。也许,您可以单独设置采样,但我不确定如何进行设置。无论如何,我修改了答案,并使用一个箭头为箭头添加了另一个“hack”。