Grzenio
Grzenio

Reputation: 36639

Automatically removing empty space on the left in box plot in gnuplot

Following the answers to this question: Histogram using gnuplot? and some other sources I created this script:

set terminal postscript eps enhanced color
set title "Histogram\_CreatesFile"

colour1="#00A0ff"
colour2="navy"
colour3="#ffA000"
colour4="#800000"
set output 'Histogram_CreatesFile.eps'
set yrange [0:]
set style fill solid 0.8 border -1
bin_width = 0.2
set boxwidth bin_width
bin_number(x) = floor(x/bin_width)
rounded(x) = bin_width * ( bin_number(x) + 0.5 )
plot 'Histogram_CreatesFile.txt' using (rounded($1)):(1) smooth frequency with boxes lc rgb colour1 notitle

and I have a test data file:

0
0.2
0.4
0.41

and everything is beautiful, but I get a weird empty space on the left of the first bar: enter image description here

how can I make the graph start from the first bar, when I don't know apriori what are the values in the data file (i.e. it might start from other value than 0)?

Upvotes: 0

Views: 629

Answers (1)

mgilson
mgilson

Reputation: 309841

It seems to me that the stats command could probably help you out here.

set terminal postscript eps enhanced color
set title "Histogram\_CreatesFile"

colour1="#00A0ff"
colour2="navy"
colour3="#ffA000"
colour4="#800000"
set output 'Histogram_CreatesFile.eps'
set yrange [0:]
set style fill solid 0.8 border -1
bin_width = 0.2
set boxwidth bin_width
bin_number(x) = floor(x/bin_width)
rounded(x) = bin_width * ( bin_number(x) + 0.5 )

stats 'Histogram_CreatesFile.txt' using (rounded($1)) nooutput
set xrange [STATS_min-bin_width/2.:]

plot 'Histogram_CreatesFile.txt' using (rounded($1)):(1) smooth frequency with boxes lc rgb colour1 notitle

stats was added in gnuplot4.6(?). Prior to that, the trick that I always used for these things was to plot to the dummy terminal and pick up the minimum from the special read-only variable GPVAL_X_MIN. stats is much cleaner and more powerful though and should be used now that the current stable version is 4.6.

Upvotes: 2

Related Questions