redcrow
redcrow

Reputation: 1823

How to plot into a histogram specific values from a file using Gnuplot

I'd like to plot two bars for a histogram, one depending on the first line of a file, while the other one depending on the second line. E.g., assuming to have a CSV file like this:

1 0.5
2 0.7

I want to plot a first bar blue of value 0.5 and a second bar red of value 0.7.

I've written the following script so far (I have a multiplot because I need to plot 4 figures of equal properties):

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

I've tried solutions like "<(sed -n '1,1p' q1.csv)" but it didn't work.

Any idea?

Upvotes: 1

Views: 441

Answers (2)

Christoph
Christoph

Reputation: 48390

You don't need to post all that settings for the tics, the labels and the multiplotting to show the problem.

You can use e.g. linecolor variable to select different line types depending on the value in the first column:

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

Note, that the changes with set linetype aren't restored with reset, but you must restart gnuplot (version 5.0 will have a reset session option for this).

Upvotes: 2

redcrow
redcrow

Reputation: 1823

I changed every CSV in this form:

R1 R2
0.5 0.7

and my script:

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

This is the result (with real values):

enter image description here

Upvotes: 0

Related Questions