Gnuplot - 对平均条形使用不同的线条颜色

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

我有一个简单的 CSV 文件,由 3 列组成:日期、值 1、值 2。我正在使用 gnuplot 创建堆积条形图,我希望最后一个条形图(平均值)用不同的颜色绘制。我正在使用以下脚本:

set terminal png size 1000, 500
set output "bars.png"
set title "Trends"
set style data histograms
set style histogram rowstacked
set yrange [0:*]
set style fill solid
set boxwidth 0.9
set xlabel "Clicks"
set ylabel "Dates"
set xtics right rotate by 45
set datafile separator ","
plot newhistogram,  "data.csv" using 2:xtic(1) title "Direct" linecolor rgb "#0000ff",'' using 3 title "Indirect" linecolor rgb '#00ff00'

输入数据文件示例:

2023-01-11,234,8756
2023-01-13,54,876
2023-01-14,3333,3566
2023-01-15,543,654
2023-01-16,657,767
2023-01-17,876,88
2023-01-18,1606,55
2023-01-20,888,77
Average,1024,1855

sample output

我尝试对线条颜色使用条件值,但收到一个我无法理解的错误:

gnuplot> plot newhistogram,  "/tmp/csv.csv" using 2:xtic(1) title "Direct" linecolor rgb "#0000ff", '' using 3 linecolor rgb (strcol(1) eq "Average" ? '#ff50ff' : '#43FF00') ^ line 0: stringcolumn() called from invalid context

gnuplot
2个回答
0
投票

我找不到使用 linecolor 指令的条件的方法,所以我最终得到了 using 的条件。更新后的 plot 命令可实现此目的,如下所示:

plot newhistogram, "data.csv" \
   using (strcol(1) ne 'Average' ? $2 : NaN):xtic(1) title "Direct" lc rgb "#0000ff", \
'' using (strcol(1) ne 'Average' ? $3 : NaN) title "Indirect" lc rgb '#00ff00', \
'' using (strcol(1) eq 'Average' ? $2 : NaN) notitle lc rgb '#0088ff', \
'' using (strcol(1) eq 'Average' ? $3 : NaN) notitle lc rgb '#00ff88'

前 2 行绘制不以“平均”开头的行,后 2 行绘制最后一行。每列的颜色都用任意颜色绘制。 现在输出看起来符合预期。

barchart


0
投票

说实话,我不知道如何在绘图样式中设置条件颜色

with histogram
。也许有一种我不知道的方法。 由于您的数据相对简单,我将通过绘图样式
with boxxyerror
(检查
help boxxyerror
)。在那里你有充分的灵活性,但必须更多地考虑你正在策划的事情。检查以下示例:

脚本:

### bar chart with conditional colors
reset session

$Data <<EOD
2023-01-11,234,8756
2023-01-13,54,876
2023-01-14,3333,3566
2023-01-15,543,654
2023-01-16,657,767
2023-01-17,876,88
2023-01-18,1606,55
2023-01-20,888,77
Average,1024,1855
EOD

set title "Trends"
set style histogram rowstacked
set yrange [0:*]
set style fill solid
set xlabel "Clicks"
set ylabel "Dates"
set xtics right rotate by 45
set datafile separator ","

set offsets 0.5,0.5,0,0
myBoxwidth = 0.9
myColor(colD,colL) = (avg = strcol(colL) eq "Average" , \
                  colD==2 ? avg ? 0xaaaaff : 0x000ff : avg ? 0xaaffaa : 0x00ff00)

plot $Data u 0:($2/2):   (myBoxwidth/2.):($2/2):(myColor(2,1)):xtic(1) w boxxy ti "Direct"   lc rgb var, \
        '' u 0:($2+$3/2):(myBoxwidth/2.):($3/2):(myColor(3,1))         w boxxy ti "Indirect" lc rgb var
### end of script

结果:

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