rem45acp
rem45acp

Reputation: 469

Gnuplot put xtics between bars

I need to be able to position the tic marks so that they are between the bars in the graph. That way it can be read that there were 2 points between 0 and 14, 0 between 15 and 30, and so on, instead of the tics centered underneath the bars. Is there any way to automatically do that based on the box width? Image Here is my current script attempt:

set xtics out nomirror
set ytics out nomirror
set tic scale 0
set style fill solid 1.00 border 0
set offset graph 0.1, 0.05, 0.1, 0.0
set boxwidth 15 *.9
set xtics offset -(15 *.9)/2
set term png
set output "graph.png"
plot 'data.dat' using 1:2:xtic(1) with boxes

Here is the .dat:

0       2
15      0
30      0
45      0
60      1
75      1
90      33

EDIT It appears that the following works consistently, based on the boxwidth:

set bwidth=15*.9
set boxwidth bwidth
set xtics out nomirror offset -bwidth/2 left

There still might be a better way.

Upvotes: 2

Views: 2018

Answers (1)

Christoph
Christoph

Reputation: 48430

With your solution you only shift the tic labels. I would also shift the tics.

Here is my solutions:

set tics out nomirror
set style fill solid 1.00 noborder
set autoscale fix
set offset 5,5,5,0

# extract the first and second x-values
stats 'data.dat' using 1 every ::::1 nooutput
start = STATS_min
width = STATS_max - STATS_min

set boxwidth width *.9
set xtics start, width

set term pngcairo
set output "graph.png"

plot 'data.dat' using ($1+width/2.0):2 with boxes

The first and second data values are extracted automatically (requires version >= 4.6.0). These values are used to

  1. Set the boxwidth

  2. Set the xtics (start value and increment)

  3. Shift the data points by half of the x-value increment.

See e.g. Gnuplot: How to load and display single numeric value from data file for another example for extracting data with the stats command. Instead of loading the data with stats you could of course also use

start = 0
width = 15

The result with 4.6.3 is:

enter image description here

Upvotes: 3

Related Questions