1

I'm plotting a histogram with the commands:

n=3
max=0.5
min=-2.5
width=(max-min)/n
hist(x,width)=width*floor(x/width)+width/2.0
plot "< awk '{if (\$1<3.9 && \$1>3.8) {print \$0}}' /path_to_file/" u (hist(\$2,width)):(1.0) smooth freq w boxes lc rgb "black" lt 1 lw 0.5 notitle

which gets me this:

histogram

Apparently, I'm guessing based on the data file, gnuplot is deciding by itself to choose the intervals [1.0:0.0]-[0.0:-1.0]-[-1.0:-2.0] which makes the bins centered in [0.5:-0.5:-1.5]

I need those bins to use the intervals [0.5:-0.5]-[-0.5:-1.5]-[-1.5:-2.5] which would make the bins centered in [0.0:-1.0:-2.0]

How can I accomplish this?

Gabriel
  • 40,504
  • 73
  • 230
  • 404
  • Do you just want to change labels on the x-axis? – mgilson Oct 01 '12 at 22:10
  • Hi @mgilson, no I want `gnuplot` to actually use those intervals I wrote and not the ones it appears to be using by its own decision. How can I tell `gnuplot` from what position to start counting to generate the intervals with the width I set? – Gabriel Oct 01 '12 at 22:17
  • So you want to transform `x -> x-0.5`? Try: `hist(x,width)=width*floor((x-0.5)/width)+width/2.0` – mgilson Oct 01 '12 at 23:56
  • mmm I'm not entirely sure of what that is doing but it's not what I need. I need `gnuplot` to center the bins around 0.0, -1.0 and -2.0 (with a width of '1') instead of how they are centered by default which is in 0.5, -0.5 and -1.5. I want `gnuplot` to count the number of objects inside each interval [0.5:-0.5]-[-0.5:-1.5]-[-1.5:-2.5] and **not** what it is doing right now which is counting them in the intervals [1.0:0.0]-[0.0:-1.0]-[-1.0:-2.0]. – Gabriel Oct 02 '12 at 00:33

2 Answers2

0

The interval in your histogram is determined by the function hist. hist function reduces a range of numbers into a single value, so gnuplot can plot in a bar plot centered at the value returned by hist function.

In your original post, hist(x,width)=width*floor(x/width)+width/2. Hence, you will get hist(x,width)=0.5 for width=1 and 0 <= x < 1. Eventually, you will get a bar plot centered at 0.5.

Now, if you want to set your interval as [-0.5:0.5], [-0.5:-1.5], and so on, you may want to change your hist function so that it returns 0.0 when width=1 and -0.5 <= x < 0.5.

width=1
hist(x,width)=width*floor((x + width/2)/width)
plot "< awk '{if (\$1<3.9 && \$1>3.8) {print \$0}}' /path_to_file/" u (hist(\$2,width)):(1.0) smooth freq w boxes lc rgb "black" lt 1 lw 0.5 notitleenter code here
Sunhwan Jo
  • 2,293
  • 1
  • 15
  • 13
0

I think the function you want is

hist(x,width)     = width*(floor(x/width+0.5))

This is an equivalent but simplified version of the earlier answer https://stackoverflow.com/a/13896530/834416

hist(x,width)     = width*floor((x + width/2)/width)

I had the same issue and posted an answer and sample code and output at https://stackoverflow.com/a/24923363/834416

Community
  • 1
  • 1
Winston Smith
  • 367
  • 1
  • 5
  • 11