Gnuplot在直方图中改变条形的颜色

时间:2012-09-01 03:00:59

标签: gnuplot histogram

是否可以动态更改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

是否可以使低于零的条形颜色变为红色?

谢谢,
斯文

4 个答案:

答案 0 :(得分:10)

您可以使用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

答案 1 :(得分: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格式生成图形,这样可以提供比任何位图格式更好看的图形。

答案 2 :(得分: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 :(得分:2)

实际上你也可以使用linecolor rgb变量并给出如下颜色:

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