gnuplot - Histogram with histeps connects bars

不打扰是莪最后的温柔 提交于 2019-12-11 03:34:41

问题


This is a minimal working example of the code I'm using:

#!/bin/bash
gnuplot << EOF

set term postscript portrait color enhanced 
set encoding iso_8859_1
set output 'temp.ps'
set grid noxtics noytics noztics front
set size ratio 1
set multiplot
set lmargin 9; set bmargin 3; set rmargin 2; set tmargin 1

n=32    #number of intervals
max=13. #max value
min=-3.0    #min value
width=(max-min)/n        #interval width
hist(x,width)=width*floor(x/width)+width/2.0

set boxwidth width
set style fill solid 0.25 noborder

plot "< awk '{if (3.544068>=\$1) {print \$0}}' /data_file" u (hist(\$2,width)):(1.0) smooth freq w boxes lc rgb "red" lt 1 lw 1.5 notitle


EOF

which gets me this:

What I need is to use histeps instead, but when I change boxes for histeps in the plotcommand above, I get:

What is going on here??

Here's the data_file. Thank you!


EDIT: If having histeps follow the actual outer bars limits instead of interpolating values in between (like boxesdoes) is not possible, then how could I draw just the outline of a histogram made with boxes?


EDIT2: As usual mgilson, your answer is beyond useful. One minor glitch though, this is the output I'm getting which when I combine both plots with the command:

plot "< awk '{if (3.544068>=\$1) {print \$0}}' data_file" u (hist(\$2,width)):(1.0) smooth freq w boxes lc rgb "red" lt 1 lw 1.5 notitle, \
"<python pyscript.py data_file" u 1:2 w histeps lc rgb "red" lt 1 lw 1.5 notitle

Something appears to be shifting the output of the python script and I can't figure out what it might be. (Fixed in comments)


回答1:


The binning is quite easy if you have python + numpy. It's a very popular package, so you should be able to find it in your distribution's repository if you're on Linux.

#Call this script as:
#python this_script_name.py 3.14159 data_file.dat

import numpy as np
import sys

n=32         #number of intervals
dmax=13.     #max value
dmin=-3.0    #min value


#primitive commandline parsing
limit = float(sys.argv[1])   #first argument is the limit
datafile = sys.argv[2]       #second argument is the datafile to read

data = []    #empty list
with open(datafile) as f:  #Open first commandline arguement for reading.
    for line in f:            #iterate through file returning 1 line at a time
        line = line.strip()   #remove whitespace at start/end of line
        if line.startswith('#'): #ignore comment lines.
            continue
        c1,c2 = [float(x) for x in line.split()] #convert line into 2 floats and unpack
        if limit >= c1:  #Check to make sure first one is bigger than your 3.544...
            data.append(c2) #If c1 is big enough, then c2 is part of the data

counts, edges = np.histogram(data,               #data to bin
                             bins=n,             #number of bins
                             range=(dmin,dmax),  #bin range
                             normed=False        #numpy2.0 -- use `density` instead
                             )
centers = (edges[1:] + edges[:-1])/2.  #average the bin edges to the center. 

for center,count in zip(centers,counts):  #iterate through centers and counts at same time
    print center,count                    #write 'em out for gnuplot to read.

and the gnuplot script looks like:

set term postscript portrait color enhanced 
set output 'temp.ps'
set grid noxtics noytics noztics front
set size ratio 1
set multiplot
set lmargin 9 
set bmargin 3
set rmargin 2 
set tmargin 1

set style fill solid 0.25 noborder

plot "<python pyscript.py 3.445 data_file" u 1:2 w histeps lc rgb "red" lt 1 lw 1.5 notitle

I'll explain more when I get a little more free time ...



来源:https://stackoverflow.com/questions/13017130/gnuplot-histogram-with-histeps-connects-bars

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!