gnuplot-带直方图的直方图连接条 [英] gnuplot - Histogram with histeps connects bars
问题描述
这是我正在使用的代码的最小工作示例:
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
这让我明白了:
我需要的是改为使用histeps
,但是当我在上面的plot
命令中将boxes
更改为histeps
时,我得到了:
What I need is to use histeps
instead, but when I change boxes
for histeps
in the plot
command above, I get:
这是怎么回事?
这是数据文件.谢谢!
如果不能使histeps
遵循实际的外线限制,而不是在两者之间进行插值(如boxes
做),那么如何绘制 just 的轮廓boxes
制成的直方图?
If having histeps
follow the actual outer bars limits instead of interpolating values in between (like boxes
does) is not possible, then how could I draw just the outline of a histogram made with boxes
?
像往常一样,Mililson,您的答案无济于事.不过有一个小故障,当我将两个图与命令结合使用时,这就是我得到的输出:
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
某些东西似乎正在改变python
脚本的输出,我不知道它可能是什么.
(已修复评论)
Something appears to be shifting the output of the python
script and I can't figure out what it might be.
(Fixed in comments)
推荐答案
如果您使用python + numpy,则合并非常容易.这是一个非常受欢迎的软件包,因此,如果您使用的是Linux,则应该可以在发行版的存储库中找到它.
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.
和gnuplot脚本如下:
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 ...
这篇关于gnuplot-带直方图的直方图连接条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!