[英]Gnuplot - use different line colors for average bar
我有一個簡單的 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
我嘗試使用 linecolor 的條件值,但出現了一個我無法理解的錯誤:
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
歡迎來到 StackOverflow,老實說,我不知道如何在with histogram
的繪圖樣式中設置條件 colors 。 也許有一種我不知道的方法。 由於您的數據相對簡單,我會通過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
結果:
我找不到為linecolor指令使用條件的方法,所以我最終得到了使用. 更新后的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 行 plot 不是以“平均”開頭的行,最后 2 行 plot 是最后一行。 每列的顏色都用任意顏色繪制。 output 現在看起來符合預期。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.