Gabriel
Gabriel

Reputation: 42319

Plotting a histogram with a conditional

I'm trying to make a histogram out of data in a file that looks like this:

#Column 1   Column 2
#
0.0300      0.2126
1.0000e-4   0.0104
6.0000e-3   0.1299
1.0000e-4   8.0600e-3
1.0000e-4   0.0105
0.0190      0.2204
6.0000e-3   7.4900e-3
1.0000e-4   0.0952
6.0000e-3   7.4200e-3
1.0000e-4   0.0131
0.0190      0.3062
0.0190      0.2561
0.0300      0.9748
0.0300      0.9406
0.0300      0.0139
1.0000e-4   0.0281
0.0300      0.3625
1.0000e-4   0.0945
0.0300      0.5650
1.0000e-4   0.1045
6.0000e-3   0.2362
1.0000e-4   0.0180
1.0000e-4   0.1366
1.0000e-4   0.0195
0.0300      0.4652
0.0190      0.3505
0.0300      0.5146
0.0190      0.4319
6.0000e-3   0.2054
6.0000e-3   0.2377
0.0300      0.5281
1.0000e-4   0.1128
6.0000e-3   0.0623

If I use the code:

n=20    #number of intervals
max=0.03 #max value
min=0    #min value
width=(max-min)/n        #interval width
hist(x,width)=width*floor(x/width)+width/2.0

plot 'data' u (hist(\$1,width)):(1.0) smooth freq w boxes lc rgb "blue" lt 1 lw 0.5 notitle

I get the right histogram:

right histo

but if I use the conditional line:

plot 'data' u (hist((\$2<=0.5?\$1:1/0),width)):(1.0) smooth freq w boxes lc rgb "blue" lt 1 lw 0.5 notitle

I get this:

histo wrong

You can see that gnuplot isn't adding the lines correctly but plotting them as separate columns instead.

Is there any way to fix this? Thanks!

Upvotes: 2

Views: 1447

Answers (1)

mgilson
mgilson

Reputation: 309821

I suspect this is a symptom of how gnuplot treats "missing" data. With respect to missing data, the following are actually slightly different:

plot 'data' u 1:2 w lines    #connects lines across missing records
plot 'data' u 1:($2) w lines #doesn't connect lines when a missing record is encountered

I suspect you're seeing a slightly different symptom of this design decision.

Unfortunately it makes the typical gnuplot data-filter useless here :-(. Fortunately, your condition is easy to move into awk:

plot "< awk '{if ($2 <= 0.5) {print $0}}' test.dat " u (hist($1,width)):(1.0) smooth freq w boxes lc rgb "blue" lt 1 lw 0.5 notitle

Now gnuplot sees only the data you want it to (since it doesn't see any NaN values, so it doesn't create any new counters).

Upvotes: 3

Related Questions