如何使用Gnuplot从文件绘制直方图特定值

时间:2014-07-19 17:51:58

标签: plot conditional gnuplot histogram

我想为直方图绘制两个条形图,一个取决于文件的第一行,而另一个取决于第二行。例如,假设有这样的CSV文件:

1 0.5
2 0.7

我想绘制一个值为0.5的第一个条形蓝色和一个值为0.7的第二个条形红色。

到目前为止我编写了以下脚本(我有一个多色图,因为我需要绘制4个相同属性的数字):

#!/usr/bin/env gnuplot
set term pngcairo enhanced size 1500,700
set output 'accuracy_plot.png'
set multiplot layout 1,4
set xlabel 'rounds' font ',16'
set ylabel 'mean' font ',16'
set xrange[1:2]
set yrange[0:1]
set style fill solid
set grid xtics lt 0 lw 1 lc rgb "#333333"
set grid ytics lt 0 lw 1 lc rgb "#333333"
set xtics font ',14'
set xtics autofreq 1
set ytics font ',14'
set key font ',12'
set title font ',20'
###
set title 'Q1'
plot "q1.csv" using 1:2 title '' with boxes linecolor rgb 'blue', \
"truth1.csv" using 1:2 title 'truth' with lines linewidth 3 linecolor rgb 'black'
###
set title 'Q2'
plot "q2.csv" using 1:2 title '' with boxes linecolor rgb 'blue', \
"truth2.csv" using 1:2 title 'truth' with lines linewidth 3 linecolor rgb 'black'
###
set title 'Q3'
plot "q3.csv" using 1:2 title '' with boxes linecolor rgb 'blue', \
"truth3.csv" using 1:2 title 'truth' with lines linewidth 3 linecolor rgb 'black'
###
set title 'Q4'
plot "q4.csv" using 1:2 title '' with boxes linecolor rgb 'blue', \
"truth4.csv" using 1:2 title 'truth' with lines linewidth 3 linecolor rgb 'black'
###
unset multiplot

我尝试过像"<(sed -n '1,1p' q1.csv)"这样的解决方案,但它没有用。

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

您不需要发布抽搐,标签和多重图表的所有设置来显示问题。

您可以使用例如linecolor variable根据第一列中的值选择不同的行类型:

set terminal png enhanced
set output 'foobar.png'

set yrange [0:1]
set style fill solid

set linetype 1 lc rgb 'blue'
set linetype 2 lc rgb 'red'

plot 'q1.csv' using 1:2:1 with boxes lc variable notitle

enter image description here

请注意,使用set linetype尚未恢复reset的更改,但您必须重新启动gnuplot(版本5.0将为此设置reset session选项。)

答案 1 :(得分:0)

我更改了此表单中的每个CSV:

R1 R2
0.5 0.7

和我的剧本:

#!/usr/bin/env gnuplot
set term pngcairo enhanced size 1500,700
set output 'accuracy_plot.png'
set multiplot layout 1,4
set xlabel 'rounds' font ',16'
set ylabel 'mean' font ',16'
set xrange[1:2]
set yrange[0:1.5]
set style fill solid
set grid xtics lt 0 lw 1 lc rgb "#333333"
set grid ytics lt 0 lw 1 lc rgb "#333333"
set xtics font ',14'
set xtics autofreq 1
set ytics font ',14'
set key font ',12'
set title font ',20'

set style data histograms
set style histogram columnstacked

###
set title 'Q1'
plot "q1.csv" using 1 lt 1 title columnhead, \
newhistogram lt 1 at 1, '' u 1 ti col, \
newhistogram lt 2 at 2, '' u 2 ti col, \
"truth1.csv" using 1:2 title 'truth' with lines linewidth 3 linecolor rgb 'black'
###
set title 'Q2'
plot "q2.csv" using 1 lt 1 title columnhead, \
newhistogram lt 1 at 1, '' u 1 ti col, \
newhistogram lt 2 at 2, '' u 2 ti col, \
"truth1.csv" using 1:2 title 'truth' with lines linewidth 3 linecolor rgb 'black'
###
set title 'Q3'
plot "q3.csv" using 1 lt 1 title columnhead, \
newhistogram lt 1 at 1, '' u 1 ti col, \
newhistogram lt 2 at 2, '' u 2 ti col, \
"truth1.csv" using 1:2 title 'truth' with lines linewidth 3 linecolor rgb 'black'
###
set title 'Q4'
plot "q4.csv" using 1 lt 1 title columnhead, \
newhistogram lt 1 at 1, '' u 1 ti col, \
newhistogram lt 2 at 2, '' u 2 ti col, \
"truth1.csv" using 1:2 title 'truth' with lines linewidth 3 linecolor rgb 'black'
###
unset multiplot

这是结果(带有实际值):

enter image description here