我並沒有完全理解透明度如何取決於值,所以我會給出一個一般答案,您可以在其中替換透明度函數。
雖然您可以指定transparency for line colors,但在使用調色板時這似乎不可行,這將是實現您想要的最直接的方法。只能使用gnuplot的最遠距離是讓顏色顯示爲透明,如下面的腳本,其中in.data是包含示例數據的文件。
#!/usr/bin/env gnuplot
set term pngcairo
in_data = "in.data"
set out "out.png"
# function to combine color and alpha channels with white background
# 0: no transparency, 1: fully transparent
make_transparent(x1, t) = (1-t)*x1 + t
# a function to decide transparency
# the input and output must be in range of [0,1]
#get_transparency(x1) = 0 # no transparency
#get_transparency(x1) = 1 # fully transparent
get_transparency(x1) = 1 - x1 # smaller values are more transparent
# convenience function to truncate values
minval(x1, x2) = x1<x2?x1:x2
maxval(x1, x2) = x1>x2?x1:x2
truncval(x1, xmin, xmax) = maxval(minval(x1, xmax), xmin)
trunc(x1) = truncval(x1, 0, 1)
# the default palette consists of rgbfunctions 7,5,15
# we redefine their transparency enabled versions here
# the input and output must be in range of [0,1]
# see other formulae with "show palette rgbformulae" command in gnuplot
f7(x1) = make_transparent(sqrt(x1) , get_transparency(x1))
f5(x1) = make_transparent(x1**3 , get_transparency(x1))
f15(x1) = make_transparent(trunc(sin(2*pi*x1)), get_transparency(x1))
set palette model RGB functions f7(gray),f5(gray),f15(gray)
splot in_data palette
此腳本假定背景爲白色,但可以適應任何其他純色背景。然而,一旦點開始重疊,它就會分崩離析。
爲了獲得真正的透明度,您需要將每個數據點繪製爲單獨的線條,併爲其賦予不同的線條顏色。這可以通過預處理數據來實現,如下面的bash腳本。
#!/usr/bin/env bash
set -eu
in_data="in.data"
out_png="out.png"
pi=3.141592653589793
# function to convert data value into rgba value
function value2rgba()
{
# arguments to function
local val="${1}"
local min="${2}"
local max="${3}"
# normalized value
local nval=$(bc -l <<< "(${val}-${min})/(${max}-${min})")
#### alpha channel value ####
local alpha="$(bc -l <<< "255 * (1-${nval})")"
# round to decimal
alpha=$(printf "%0.f" "${alpha}")
#### red channel value ####
# rgbformulae 7 in gnuplot
local red="$(bc -l <<< "255 * sqrt(${nval})")"
# round to decimal
red=$(printf "%0.f" "${red}")
#### green channel value ####
# rgbformulae 5 in gnuplot
local red="$(bc -l <<< "255 * sqrt(${nval})")"
local green="$(bc -l <<< "255 * ${nval}^3")"
# round to decimal
green=$(printf "%0.f" "${green}")
#### blue channel value ####
# rgbformulae 15 in gnuplot
local blue="$(bc -l <<< "255 * s(2*${pi}*${nval})")"
# round to decimal
blue=$(printf "%0.f" "${blue}")
# make sure blue is positive
if ((blue < 0))
then
blue=0
fi
### whole rgba value
local rgba="#"
rgba+="$(printf "%02x" "${alpha}")"
rgba+="$(printf "%02x" "${red}")"
rgba+="$(printf "%02x" "${green}")"
rgba+="$(printf "%02x" "${blue}")"
echo "${rgba}"
}
# data without blank lines
data="$(sed -E "/^[[:space:]]*$/d" "${in_data}")"
# number of lines
nline=$(wc -l <<< "${data}")
# get the minimum and maximum value of the 4-th column
min_max=($(awk '{ print $4 }' <<< "${data}" | sort -g | sed -n '1p;$p'))
# array of colors for each point
colors=()
while read -r line
do
colors+=($(value2rgba "${line}" "${min_max[@]}"))
done < <(awk '{ print $4 }' <<< "${data}")
# gather coordinates into one row
coords=($(awk '{ print $1,$2,$3 }' <<< "${data}"))
gnuplot << EOF
set term pngcairo
set out "${out_png}"
\$DATA << EOD
${coords[@]}
EOD
nline=${nline}
colors="${colors[@]}"
unset key
splot for [i=0:nline-1] \$DATA \
u (column(3*i+1)):(column(3*i+2)):(column(3*i+3)) \
pt 1 lc rgb word(colors, i+1)
EOF
這些腳本用gnuplot的5
我覺得你不必GNUPLOT測試,但Matlab或倍頻。用這些程序做你想做的事情要容易得多。 –
你想要第四列來確定顏色和透明度嗎?當它不在0和1之間時,你想要發生什麼? – user8153
@ user8153我想第四列確定顏色和透明度。第四列的值對於我的數據總是正值,可能小於或大於1.如果它大於1,那麼如果它們接近1,我仍然希望它們更透明。如果我們繪製第四列以對數刻度表示,並且假設對數刻度在1附近是對稱的,這意味着它是從10^-n到10^n,其中n是功率,則顏色條的中間將是1,並且最透明。 – James