问题
I have a c++ program that generates 10000 medians from sample data and outputs the medians to a file named "data.dat". I then modified this gnuplot script from here to generate a histogram:
reset
n=100 #number of intervals
max=500
min=0 #min value
width=(max-min)/n #interval width
#function used to map a value to the intervals
hist(x,width)=width*floor(x/width)+width/2.0
set term png #output terminal and file
set output "histogram.png"
set xrange [min:max]
set yrange [0:]
#to put an empty boundary around the
#data inside an autoscaled graph.
set offset graph 0.05,0.05,0.05,0.0
set xtics min,(max-min)/5,max
set boxwidth width*0.9
set style fill solid 0.5 #fillstyle
set tics out nomirror
set xlabel "Medians"
set ylabel "Frequency"
#count and plot
plot "data.dat" u (hist($1,width)):(1.0) smooth freq w boxes lc rgb"green" notitle
set xrange [GPVAL_DATA_X_MIN:GPVAL_DATA_X_MAX]
reset
replot
This script generates exactly what I want, except I need the ability to change the x range of the histogram. My C++ program asks the user for the upper boundary of sample data to use. Which means that the median data could be anywhere between 0 and whatever the user enters as the upper boundary. As you can see above, I have attempted to change the xrange of the data, but I haven't been able to get it to work. Even when I use GLPVAL_DATA and reset the xrange and replot the data, the histogram ranges never change. I just get the same xrange of 0 to 500 that the original graph outputted. What am I doing wrong here?
来源:https://stackoverflow.com/questions/21369409/dynamically-changing-the-range-of-a-histogram-in-gnuplot