如何在gnuplot

时间:2018-02-09 16:59:35

标签: gnuplot graph-coloring

我想为2D-Plot的线条着色。我希望着色是x的函数。说

f(x)=x^2

我想制作一个情节,其中f(x)的颜色范围从红色到蓝色, 取决于函数g(x),比如说

g(x)=x

如果g(x)=10f(x)应为蓝色,if g(x)=0,则f应为红色。在两者之间,我需要在两种颜色之间平滑过渡。

非常感谢任何帮助,想法或疑问,谢谢!

1 个答案:

答案 0 :(得分:0)

在绘制点时,Gnuplot支持lc variable选项,该选项允许为各个点指定线型(以及颜色)。但是,使用lines绘图样式(以及沿着线绘制的颜色渐变),人们很可能会偏离另一种策略。例如,可以将x - 范围划分为独立的细分,为每个细分计算相应的颜色(根据函数g(x)),最后在每个细分上绘制f(x)独立地是:

set terminal pngcairo rounded enhanced font ",16"
set output 'test.png'
set size ratio 2/(1+sqrt(5.))

unset key

xMin = 0.0
xMax = 10.0

set xr [xMin:xMax]
set yr [0:100]

#coloring function, gMin/gMax has to be set accordingly
#so that the scaled function h(x) does not overflow the [0,1] interval
gMin = 0.
gMax = +10.
g(x) = x

#function to be plotted
f(x) = x*x

#coloring function rescaled into interval [0,1]
#for each g(x), gMin/gMax should be adjusted correspondingly
h(x) = (g(x) - gMin)/(gMax - gMin)

#generate RGB representation of the interpolated color
#corresponding to the red (gMin) -> blue (gMax) transition
color(x) = sprintf('#%02X00%02X', (1-h(x))*255, h(x)*255);

#divide the x range into N segments
N = 200
dx = (xMax - xMin)/N

set samples 1000

#make the segments overlap a bit so that we don't need too high "samples"
eps = dx/10

#plot each segment with corresponding color 
binLeftBorder(i) = xMin + i*dx
binMidPoint(i) = (binLeftBorder(i) + binLeftBorder(i+1))/2

isInBin(i, x) = (x >= (binLeftBorder(i) - eps) && x < (binLeftBorder(i+1) + eps))

plot for [i=0:N-1] isInBin(i, x)?f(x):(1/0) w l \
    lw 4 lc rgb color(binMidPoint(i))

这会产生: enter image description here