I'm puzzled about the behaviour of my plotting script. I want to plot multiple files. In the last file i want different border settings. So i came up with an if statement. The script looks like:
labeltitles = "0.01 0.02 0.05 0.1 0.15 0.1885 0.2 0.25"
outnames = "0p01 0p02 0p05 0p1 0p15 0p1885 0p2 0p25"
do for [i=1:2] {
set border 4 + 1 ## top (4) + bottom (1)
if (i = words(labeltitles)) {set border 8 + 4 + 1} ## right (8) + top (4) + bottom (1)}
set xlabel 'z = '.word(labeltitles,i)
set out word(outnames,i).'.eps'
plot 'data.dat' u (column(i+1)):1 w l lt 1 lw 7 lc rgb '#444444'
}
When i run this script, only the last plot gets outputted. If i comment the if statement all plots get outputted. I also tried to add the else statement, but nothing changes.
A rather classical programming error: You have an assignment inside the if-condition instead of a comparison (==). It must be
if (i == words(labeltitles)) {set border 8 + 4 + 1}
Related
I have a text file with 3 columns defining 3D points.
I want to paint every point in 3D and an ellipsoid centered in every point. I discard using the
set parametric
way because I need to iterate my text file.
So i think in doing something like this:
gnuplot
reset
set xrange [-5:5]
set yrange [-5:5]
set zrange [-5:5]
Rx = 1
Ry = 1
Rz = 1
fx(u,v) = column(2) + Rx*cos(u)*cos(v)
fy(u,v) = column(1) + Ry*sin(u)*cos(v)
fz(u,v) = column(3) + Rz*sin(v)
iMax = 200
splot "file.txt" using ($2):($1):($3) title "Input " with points ps 2 pt 7,\
for [i=0:iMax] "file.txt" u (fx(2*pi*i/iMax, pi*i/iMax)):(fy(2*pi*i/iMax, pi*i/iMax)):(fz(2*pi*i/iMax, pi*i/iMax)) notitle with points ps 2 pt 7
But the only think I can get is this strange and heavy (I know that they are a lot of iterations per row, but maybe there is another approach) pattern
Any help? Thank you.
There is something wrong for the mathematic point of view? Using something like this Im perfectly able to plot spheres, but without parsing data:
set parametric
R = 1
set urange [-pi/2:pi/2]
set vrange [0:2*pi]
splot R*cos(u)*cos(v),R*cos(u)*sin(v),R*sin(u) w l lc rgb "yellow"
I assume you want to plot the 2D surfaces of 3D ellipsoids. But the plot command has only a loop over i which is only 1D. This can not give a 2D surface. It might be possible to nest another 1D loop to get this approach to work.
I would suggest something else. Before plotting, you can store the center coordinates into a gnuplot array. Then you loop over this array and plot a sphere/ellipsoid using parametric mode.
This might be a starting point:
# This is the file with the center coordinates.
datafile = "ellipses.dat"
# The "stats" command stores the number of rows in the STATS_records variable.
stats datafile nooutput
num_rows = STATS_records
# Generate arrays which will contain the center coordinates of the ellipsoids.
array centers_x[num_rows]
array centers_y[num_rows]
array centers_z[num_rows]
# Read the center coordinates into the prepared arrays.
# I "misuse" the stats command. The "using" expression in parenthesis executes
# the respective commands and returns the value after the last comma: row + 1.
# This return value is not needed anywhere.
row = 1
stats datafile using (centers_x[row]=$1, \
centers_y[row]=$2, \
centers_z[row]=$3, \
row = row + 1) nooutput
# Output into an image file.
set terminal pngcairo
set output "ellipsoids.png"
# Set parameters for ellipsoids.
Rx = 0.1
Ry = 0.1
Rz = 0.7
# Use parametric mode for plotting.
set parametric
set urange [-pi/2:pi/2]
set vrange [0:2*pi]
# Finally plot:
splot datafile using 1:2:3 title "Input " with points ps 2 pt 7, \
for [i=1:num_rows] centers_x[i] + Rx*cos(u)*cos(v), \
centers_y[i] + Ry*cos(u)*sin(v), \
centers_z[i] + Rz*sin(u) notitle
Please doublecheck x, y, and z: I was not that careful. This is the result:
I have used this example data:
1 2 3
2 2 4
2 3 4
3 3 3
3 4 5
Arrays are available starting with gnuplot 5.2. For older versions, please search the internet for workarounds.
With a set of data files. I would like to performs series of operations on each file (such as fitting) and stack the resulting curves continiously along with my analysis (to see how each curves fit on the bigger picture). I wrote the following code snippet
reset
PATH = 'XRP_'
nmin = 1
nmax = 20
f(x) = log10(x); h(x) = a*x + b
name(i) = sprintf(PATH.'%04d/data_main_ddnls_twod_mlce.dat', i)
set xrange [0:7]
start = 0
set fit
do for [i=nmin:nmax]{
fit [4:] h(x) name(i) using (f($1)):(f($4)) via a, b
if (start==0){
plot name(i) using (f($1)):(f($4)) w l title sprintf("%04d", i)
} else {
replot name(i) using (f($1)):(f($4)) w l title sprintf("%04d", i)
}
start = start + 1
pause -1
}
# Add the slope
replot (1./5.)*x + 0.5 lc 'black' lw 3 dt 2
unset fit
# pause -1
Instead of stacking all the previous curves + the current one, it plots only the current curve i-times (see loop of code). For instance, after 10 iterations it plots only the 10th datafile, 10 times (see legends on picture)
How can I fix this?
The reason your plot behaves the way it does, and example (1) from theozh does also, is that "replot f(x)" acts by tacking ", f(x)" onto the end of the previous plot command. By putting it in a loop you are basically creating the successive commands
plot f(x,i)
plot f(x,i), f(x,i)
plot f(x,i), f(x,i), f(x,i)
...
Yes the value of i might change each time, but nevertheless each plot command produces multiple copies of the same thing.
Alternative solution: I don't normally recommend multiplot mode for creating a single output, but in this case it may be the best option.
# force identical margins even if the range changes
set margins screen 0.1, screen 0.9, screen 0.1, screen 0.9
# ... same prelimary stuff as shown in the question
# revised loop using multiplot rather than replot
set multiplot
do for [i=nmin:nmax]{
fit [4:] h(x) name(i) using (f($1)):(f($4)) via a, b
plot name(i) using (f($1)):(f($4)) w l \
title sprintf("%04d", i) at screen 0.9, screen 1.0 - 0.02*i
unset tics
}
unset multiplot
Note that you cannot use auto-generated title placement because each of the multiplot iterations will put the title in the same place. So instead we use the form "title foo at ". Similarly it is better to turn off tic generation after the first pass so that you don't redraw the tics and labels each time through the loop.
Indeed, a strange behaviour which I also would not have expected. See the minimal examples below.
Version 1: basically your attempt. Not the expected result. I also don't know
why.
Version 2: the expected result. Basically the same but not in a loop.
Version 3: the expected result, although in a loop but using eval.
Not very satisfying but at least some solution. Hopefully, others will have better solutions or explanations.
### plotting in a loop
reset session
set colorsequence classic
# Version 1
set title "Version 1"
do for [i=1:5] {
if (i==1) { plot x**i }
else { replot x**i noautoscale }
}
pause -1
# Version 2
set title "Version 2"
plot x**1
replot x**2 noautoscale
replot x**3 noautoscale
replot x**4 noautoscale
replot x**5 noautoscale
pause -1
# Version 3
set title "Version 3"
do for [i=1:5] {
if (i==1) { cmd = sprintf("plot x**%d",i) }
else { cmd = sprintf("replot x**%d noautoscale",i) }
eval cmd
}
### end of code
I have these two blocks of data in the same file. Both represents a set of measurements that I want to fit then using a single script to compare each other. I know that it would be easier separate in two files and than fit each one separately but I'll have more than two blocks and it would be boring. Someone know how should I do it?.
I tried to use:
f(x) = a*x^b
f1(x) = a1*x^b1
fit f(x) "temp.dat" i 0 u 1:2:4 via a,b, f1(x) "temp.dat" i 1 u 1:2:4 via a1,b1
p f(x), "temp.dat" i 0 u 1:2:4 w yer, f1(x), "temp.dat" i 1 u 1:2:4
Thks
1 100 2.13048e-09 0.2 2.4178e-11
2 140 1.51668e-09 0.2 1.69698e-11
3 180 1.18001e-09 0.2 1.35081e-11
4
5 100 1.41599e-09 0.3 1.62087e-11
6 140 1.02526e-09 0.3 1.16511e-11
7 180 8.1794e-10 0.3 9.50745e-12
Note that your data file blocks should be separated by two blank lines in order to use the index option. Otherwise, with only one blank line, you need to use every.
That said, what you want to achieve can be done with eval and a do for loop:
do for [i=0:1] {
eval sprintf("f%i(x) = a%i + b%i * x", i, i, i)
eval sprintf("fit f%i(x) 'temp.dat' i %i via a%i, b%i", i, i, i, i)
}
plot "temp.dat" i 0, f0(x), "temp.dat" i 1, f1(x)
i'm having some problems with gnuplot
I have to draw a cdf function and i'm interested in the values of variable x when F(x) is equal to 0.1 and 0.9
How can I tell Gnuplot to show me on the x axis the value corresponding to a given value on the y value (in my example those values are 0.1 and 0.9)
thanks
You're basically asking gnuplot to solve an equation. In your particular case, actually two equations: F(x)=0.1 and F(x)=0.9. As far as I know this cannot be done, but I might be wrong. What you can do if you simply want a graphical solution, is make a conditional plot, and ask that when F(x) is very close to 0.1 0.9, gnuplot plots something other than the function.
For example, assume f(x)=x^2 and you want to know "graphically" for which x f(x)=0.1. Then you can request the value abs(f(x) - 0.1) be small, for example < 0.01. Then tell gnuplot to go to zero (just an example!) if this is the case, otherwise plot f(x)=x^2:
f(x)=x**2
set xrange [-2:2]
set samples 1000
plot abs(f(x)-1) < 0.01 ? 0 : f(x)
Which yields:
The two peaks that go to zero mark graphically on the x axis the solution to the equation f(x)=0.1. Of course, you need gnuplot to sample this point in order to see a peak. Thus you need to play with set samples and set xrange.
From your question it is not clear whether you have a function F(x) as expression or just a x,y-data file. I assume that your function is monotonic increasing in x and y.
Two solutions come to my mind:
via simple linear interpolation
via curve fitting
Let's create some test data. For this, let's assume your function is known (as expression) and something like this (check help norm): F(x) = a*norm(b*x + c)
Let's take a = 1; b = 0.8; c = -4. In the example below, sampling will be only 8, just for illustration purpose.
You can easily set samples 200 and you will get the same results as for the curve fitting method below. From gnuplot 5.0 on, you could write the data into a datablock instead of a file on disk.
Data: SO22276755.dat
0 3.16712e-05
1.42857 0.002137
2.85714 0.043238
4.28571 0.283855
5.71429 0.716145
7.14286 0.956762
8.57143 0.997863
10 0.999968
Script 1: (basically works for gnuplot 4.6.0, March 2012)
### interpolate x-values
reset
FILE = "SO22276755.dat"
yis = '0.10 0.90'
yi(n) = real(word(yis,n))
xis = ''
xi(n) = real(word(xis,n))
Interpolate(yi) = (x1-x0)/(y1-y0)*(yi-y0) + x0
getXis(xis) = xis.(n=words(xis), n<words(yis) ? yi=real(word(yis,n+1)) : 0, \
y0<=yi && y1>=yi ? sprintf(" %g",Interpolate(yi)) : '')
set key left top noautotitle
set grid x,y
plot x1=y1=NaN FILE u (x0=x1,x1=$1):(y0=y1,y1=$2,xis=getXis(xis),y1) \
w l lc rgb "blue" ti "data", \
'+' u (xi=xi(int($0+1))):(yi=yi(int($0+1))):\
(sprintf("(%.4g|%.4g)",xi,yi)) every ::0::1 \
w labels point pt 7 lc rgb "red" right offset -1,0 ti "interpolated"
### end of script
Result:
Script 2: (basically works for gnuplot>=4.6.0, March 2012)
With this approach you are fitting your known function F(x) to constant lines, i.e. your desired values 0.1 and 0.9. For this, a file will be created (could be a datablock for gnuplot>=5.0) and it will basically look like this SO22276755.fit:
0 0.1
1 0.1
0 0.9
1 0.9
### interpolate x-values
reset
F(x) = a*norm(b*x+c) # function
a = 1
b = 0.8
c = -4
yis = '0.10 0.90'
yi(n) = real(word(yis,n))
xis = ''
xi(n) = real(word(xis,n))
set key left top noautotitle
set grid x,y
# create fit levels file
LEVELS = "SO22276755.fit"
set table LEVELS
set samples 2
plot for [i=1:words(yis)] '+' u (yi(i))
unset table
xmin = 0
xmax = 10
set xrange[xmin:xmax]
set samples 100
xis = ''
do for [i=1:words(yis)] {
xi = (xmin+xmax)*0.5 # set start value
fit F(xi) LEVELS u 1:2 index i-1 via xi
xis = xis.sprintf(" %g",xi)
}
plot F(x) w l lc rgb "web-green" ti "F(x)", \
'+' u (xi=xi(int($0+1))):(yi=yi(int($0+1))):(sprintf("(%.4g|%.4g)",xi,yi)) \
every ::0::1 w labels point pt 7 lc rgb "red" righ offset -1,0 ti "fitted"
### end of script
Result:
I am currently plotting some data using gnuplot with rowstacked histograms.
The problem is, that the colors start to repeat after 9 different colors have been chosen. One can see this also happening in the official gnuplot examples (see http://gnuplot.sourceforge.net/demo/histograms.html - Example 4 & 5)
Is there any way to tell gnuplot to use more different colors?
There is no fully automated way to do this, but you can define as many line styles as you want with set style line ... and then use them. Here, I just use a simple iteration to define several colors:
do for [i=1:20] {
set style line i linecolor rgb hsv2rgb(0.05*(i-1), 1, 1)
}
set style data histograms
set style histogram rowstacked
set style fill solid border -1
set boxwidth 0.75
plot for [i=1:20] 'mydata.dat' u 2 ls i t 'ls '.i
The data file contains just the values
1 0.1
2 0.2
3 0.3
Note, that the hsv2rgb function is defined only since 5.0. For earlier version you can use the following user-defined function to get the same functionality:
rgb2int(r,g,b) = int(255*r)*2**16 + int(255*g)*2**8 + int(255*b)
hsv2rgb(h,s,v) = (s == 0 ? rgb2int(v,v,v) : (HSV_h = h*6.0, HSV_i = int(floor(HSV_h)), HSV_f = HSV_h - HSV_i, HSV_p = v*(1.0 - s), HSV_q = v*(1.0-s*HSV_f), HSV_t = v*(1.0-s*(1.0-HSV_f)), (HSV_i%6 == 0 ? rgb2int(v,HSV_t,HSV_p) : (HSV_i%6 == 1 ? rgb2int(HSV_q,v,HSV_p) : (HSV_i%6 == 2 ? rgb2int(HSV_p,v,HSV_t) : (HSV_i%6 == 3 ? rgb2int(HSV_p,HSV_q,v) : (HSV_i%6 == 4 ? rgb2int(HSV_t,HSV_p,v) : rgb2int(v,HSV_p,HSV_q))))))))
To make it easier, you could put this code into a configuration file, or a third script hsv2rgb.gp and include it with load 'hsv2rgb.gp' before using the function.
Output with 4.6.3 is:
There is something a little more automated than Christoph's answer. You can use a color palette:
set palette rgb 7,5,15
unset colorbox
plot 'immigration.dat' using 2:xtic(1) title columnheader(2), \
for [i=3:22] '' using i lt palette frac i/22. title columnheader(i)
The trick is, to define frac based on the loop counter i. Be sure to devide by a float (in this case 22. to match the example) to get the right fractions of the color palette.
Or, for example 4:
plot 'immigration.dat' using (100.*$2/$24):xtic(1) t column(2), \
for [i=3:23] '' using (100.*column(i)/column(24)) lt palette frac i/23.\
title column(i)
Now, you only have to decide on a suitable color palette.