2014-07-19 66 views
1

我想繪製直方圖的兩個小節,一個取決於文件的第一行,而另一個取決於第二行。例如,假設有一個CSV文件中像這樣:如何使用Gnuplot從文件中繪製直方圖特定值

1 0.5 
2 0.7 

我想繪製的值0.5的第一條藍色和值0.7的第二條紅色。

我已經寫了下面的腳本到目前爲止,(我將多重,因爲我需要繪製4位數字相同的屬性):像"<(sed -n '1,1p' q1.csv)"

#!/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 

我試圖解決方案,但沒有奏效。

有什麼想法?

回答

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選項)。

+0

謝謝。你的解決方案比我的更好! –

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