2012-09-01 270 views
14

是否可以動態改變Gnuplot腳本中柱狀圖的顏色? 我有以下腳本Gnuplot改變柱狀圖中柱狀圖的顏色

reset 
fontsize = 12 
set term postscript enhanced eps fontsize 
set output "bargraph_speedup.eps" 
set style fill solid 1.00 border 0 
set style histogram 
set style data histogram 
set xtics rotate by -45 
set grid ytics linestyle 1 
set xlabel "Benchmarks" font "bold" 
set ylabel "Relative execution time vs. reference implementation" font "bold" 
set datafile separator "," 
plot 'bm_speedup.dat' using 2:xtic(1) ti "Speedup" linecolor rgb "#00FF00" 

產生這樣的情節:

Generated plot

是否有可能使它們在零下紅色條的顏色?

感謝,
斯文

+0

您可能想要查看「rgb變量」 - 根據值是正還是負,添加指定綠色「線型」或紅色的第三列。 – mgilson

回答

8

您可以使用boxes風格模仿這種行爲:

我的測試數據:

zip 2 
baz 2 
bar -1 
cat 4 
foo -3 

然後用gnuplot的陰謀:

set style line 1 lt 1 lc rgb "green" 
set style line 2 lt 1 lc rgb "red" 
set style fill solid 
plot 'test.dat' u (column(0)):2:(0.5):($2>0?1:2):xtic(1) w boxes lc variable 
#     #xval:ydata:boxwidth:color_index:xtic_labels 
+0

看起來像一個解決方案! –

2

似乎並不像直方圖可以讓你做到這一點。可能是這樣的:

set boxwidth 0.3 
f(v)=v<0?1:2 
plot 'bm_speedup.dat' using 0:2:(f($2)):xticlabels(1) with boxes ti "Speedup" lc variable 
3

您可以拆分數據文件分爲兩個部分,正面的價值觀和負面的,分別繪製出來:

plot 'bm_speedup_pos.dat' using 2:xtic(1) ti "Faster" linecolor rgb "#00FF00", \ 
    'bm_speedup_neg.dat' using 2:xtic(1) ti "Slower" linecolor rgb "#FF0000" 

或者,如果你只需要生成幾個圖表,幾次,一種常見的技術是在gnuplot中生成原始圖形,然後在圖像編輯器中對其進行後處理以調整顏色。如果你走這條路線,我建議讓gnuplot以SVG格式生成圖形,這會使你看起來比任何位圖格式都好看。

+0

我想我會嘗試對情節進行後期處理。好主意,但! –

2

其實你也可以使用linecolor RGB變量並給出這樣的顏色:

plot 'bm_speedup.dat' using 2:xtic(1):($2 >= 0 ? 0x00FF00 : 0xFF0000) ti Speedup lc rgb variable