I am trying to use gnuplot to fill between a curve and the x axis for a certain xrange. For example I would like to fill between the line f(x)=x when x<5.
Example code:
set xrange [-10:10]
set yrange [-10:10]
set samples 100
plot x with filledcurves above x1=5
When this is plotted in Gnuplot 5.0 it does not show any fill.
I can try the reverse of:
plot x with filledcurves below x1=5
This gets closer because it fills below the line f(x)=x when x<5, however, it also shades the area above when x>5. There is also no way to limit it to above the x axis as well.
Any assistance would be appreciated. Thanks.
It seems there is no "direct" way to do it. Christoph gives a solution here.
Based on his code I came up with the following:
set xrange [-10:10]
set yrange [-10:10]
filter_lt(x,xmax) = ((x < xmax) ? x : 1/0)
f(x) = x
xmax = 5
plot '+' using (filter_lt($1,xmax)):(f($1)) with filledcurves x1 lt 1 notitle,\
'' using 1:(f($1)) with lines lw 2 lt 1 title 'f(x)=x'
Related
I have this data1.dat data that I'd plot using gnuplot.
x y
0.007 3.09216
0.008 3.60607
0.009 5.86643
then I tried to plot this with this plot script
set terminal svg size 400,300 enhanced fname 'arial'
set output 'out2.png'
set xlabel 'diameter'
set ylabel 'number density'
set title 'density number plot'
plot "data1.dat" using 3:4 title "" with lines
and the output in the x axis was like this
how to convert the number data on the x axis so it only shows only for example 7 or 8 only? this is an example and probably my data would getting bigger, so i can't change the data on data1.dat one by one in the x column, thanks in advance
Apparently, gnuplot's tic algorithm doesn't take the length of the ticlabels into account. That's why the numbers are overlapping.
You can do the following:
set the xtic increment manually to avoid overlap, e.g. set xtic 0.0005
multiply your values by a factor, e.g. 1000, especially if you have meters you can display millimeters, i.e. ($1*1000) which is identical to (column(1)*1000).
Code:
### avoid overlap of tic labels
reset session
$Data <<EOD
x y
0.007 3.09216
0.008 3.60607
0.009 5.86643
EOD
set ytics 1
set multiplot layout 3,1
set xlabel 'diameter / m'
plot $Data using 1:2 w l notitle
set xlabel 'diameter / m'
set xtic 0.0005
plot $Data using 1:2 w l notitle
set xlabel 'diameter / mm'
set xtics 0.2
plot $Data using ($1*1000):2 w l notitle
unset multiplot
### end of code
Result:
I'm trying to plot a 1D heatmap using two columns of data (x value and y value) in gnuplot. The linegraph plotted using my data is like this:
Linegraph:
However after some trying I can only achieve this:
What I've got:
And what I want to get is something like this. (Only example)
What I want:
The gnuplot script that I use is as follows:
set view map
set size ratio 0.2
unset ytics
unset key
splot 'test.dat' u 1:(1):2 palette
Could anyone help please?
So you want to use the y axis as a fake dimension in order to increase the width of your second line plot?
Sure, this is e.g. possible with boxxyerror with explicit ymin and ymax errors that fill the yrange.
set xr [-10:10]
set yr [0:1]
xspacing = 0.1
plot '+' u 1:(0.5):($1-xspacing):($1+xspacing):(0):(1):(sin($1)) w boxxyerror lc palette
In your case replace the sin(x) with the respective column of your data. With the special file '+' the x-width has no effect, but in your case you might need to play around with a proper xspacing in order to avoid white gaps between the points.
I would do it like this:
unset key
set xrange noextend
set offset 0,0,graph .05,graph .05
set palette cubehelix negative
plot 'foo.dat' using 0:3 with lines lc "black", \
'foo.dat' using 0:(70):3 with lines lc palette lw 10
I am searching for a long time on the net. But no use. Please help or try to give some ideas how to achieve this.
I would like to plot the finite potential well for 1D Schrodinger equation and so I build the stepwise rectangular function first and then plot it with 'filledcurves below'. Here is my code:
Terminal type set to 'aqua'
gnuplot> set style fill transparent solid 0.5 noborder
gnuplot> set xrange [0:5]
gnuplot> set yrange [0:0.8]
gnuplot> p(x) = x<= 2? 0.5: x>= 3? 0.5: 0
gnuplot> plot p(x) w filledcurves below lc rgb "gray"
gnuplot> set samples 500
gnuplot> replot
I want the regions below 0.5 in [0:2]&[3:5] were all filled with gray color, and no color for (2,3) coz I set the value to 0.
and here's the plot I got, however, it's not the result that I was expecting.
image
I would be more than grateful for any ideas you provide. Big thanks!
Plotting with filledcurves below doesn't do what you need. You must give an explicite reference to the x1 axis to get the desired result:
set style fill transparent solid 0.5 noborder
set xrange [0:5]
set yrange [0:0.8]
p(x) = x<= 2? 0.5: x>= 3? 0.5: 0
set samples 500
plot p(x) w filledcurves below x1 lc rgb "gray"
I am plotting two graphs using gnuplot. First plot is actual data and the second one is the fitting of the data.
The script I used for plotting this is shown here below:
#!/usr/bin/gnuplot
reset
set terminal png enhanced
set terminal pngcairo enhanced color dashed
set output 'msd-maltoLyo12per-225ns.png'
##########################################
set macros
labelSIZE="font 'Arial,24'"
ticFONT="font 'Arial,16"
set key font 'Arial,14'
set key spacing 1.5 samplen 5
##########################################
set xrange [0:225]
set yrange [0:11000]
set xtic #ticFONT
set ytic #ticFONT
set xtics out nomirror
set ytics out nomirror
##############################
set style line 1 lt 1 lc rgb "red" lw 2.0
set style line 2 lt 2 lc rgb "blue" lw 2.0
set style line 3 lt 3 lc rgb "coral" lw 2.0
set style line 4 lt 4 lc rgb "green" lw 2.0
set style line 5 lt 5 lc rgb "black" lw 2.0
##############################
f(x)=a+b*x
fit [120:225] f(x) 'diff-xy-maltoLyo12per.dat' via a,b
plot 'diff-xy-maltoLyo12per.dat' using 1:2 with lines linestyle 1 title "{/Symbol b}Mal-C_{12}", f(x) lw 3.0 lc rgb 'black'
Here I plot the fitting graph from 1 to 120 as shown . Also I want to plot the same graph from range 120 to 225 as in the picture .
Now I want a single plot which contain the two black lines and the red line.
How can I achieve this?
Thanks in advance.
Working with the script you already have, you can use two functions to fit in the different ranges separately, and then use a conditional plot that selects one if x < 120 and the other one if x > 120:
f1(x)=a1+b1*x
fit [0:120] f1(x) 'diff-xy-maltoLyo12per.dat' via a1,b1
f2(x)=a2+b2*x
fit [120:225] f2(x) 'diff-xy-maltoLyo12per.dat' via a2,b2
f(x) = x < 120 ? f1(x) : f2(x)
plot 'diff-xy-maltoLyo12per.dat' using 1:2 with lines linestyle 1 title "{/Symbol b}Mal-C_{12}", f(x) lw 3.0 lc rgb 'black'
Now, the way I would go about this, would be to generate a special fitting function, whose parameters would give me the point at which the slope changes as a result of the fitting itself. Say you call that point x0 (for which the value of the function is y0), the slope at the left of it is m1 and the slope at the right m2. Then the function at the left has the form m1*(x-x0)+y0 and the function at the right has the form m2*(x-x0)+y0. The overall function can be defined in gnuplot as:
f(x) = x < x0 ? m1*(x-x0)+y0 : m2*(x-x0)+y0
and you can fit f(x) "data" via x0, m1, m2, y0. You can also generate this function without the condition using a step function:
f(x) = m1*(x-x0)*(sgn(x0-x)+1)/2 + m2*(x-x0)*(sgn(x-x0)+1)/2 + y0
After you fit, for which you might need to provide some initial values, you can print x0 and you'll get the best value (that should be close to 120 in your case, as you know) for the position of the change in slope.
I have a set of points "data" defining a curve that I want to plot with bezier smooth.
So I want to fill the area below that curve between some pairs of x values.
If I only had one pair of x values it's not that difficult because I define a new set of data and plot it with filledcu. Example:
The problem is that I want to do that several times in the same plot.
Edit: Minimal working example:
#!/usr/bin/gnuplot
set terminal wxt enhanced font 'Verdana,12'
set style fill transparent solid 0.35 noborder
plot 'data' using 1:2 smooth sbezier with lines ls 1
pause -1
Where the structure of 'data' is:
x_point y_point
And I realized that my problem is that in fact I can't fill not even one curve, it seems to be filled because the slope is almost constant there.
To fill parts below a curve, you must use the filledcurves style. With the option x1 you fill the part between the curve and the x-axis.
In order to fill only parts of the curve, you must filter your data, i.e. give the x-values a value of 1/0 (invalid data point) if they are outside of the desired range, and the correct value from the data file otherwise. At the end you plot the curve itself:
set style fill transparent solid 0.35 noborder
filter(x,min,max) = (x > min && x < max) ? x : 1/0
plot 'data' using (filter($1, -1, -0.5)):2 with filledcurves x1 lt 1 notitle,\
'' using (filter($1, 0.2, 0.8)):2 with filledcurves x1 lt 1 notitle,\
'' using 1:2 with lines lw 3 lt 1 title 'curve'
This fills the range [-1:0.5] and [0.2:0.8].
To give a working example, I use the special filename +:
set samples 100
set xrange [-2:2]
f(x) = -x**2 + 4
set linetype 1 lc rgb '#A3001E'
set style fill transparent solid 0.35 noborder
filter(x,min,max) = (x > min && x < max) ? x : 1/0
plot '+' using (filter($1, -1, -0.5)):(f($1)) with filledcurves x1 lt 1 notitle,\
'' using (filter($1, 0.2, 0.8)):(f($1)) with filledcurves x1 lt 1 notitle,\
'' using 1:(f($1)) with lines lw 3 lt 1 title 'curve'
With the result (with 4.6.4):
If you must use some kind of smoothing, the filter may affect the data curve differently, depending on the filtered part. You can first write the smoothed data to a temporary file and then use this for 'normal' plotting:
set table 'data-smoothed'
plot 'data' using 1:2 smooth bezier
unset table
set style fill transparent solid 0.35 noborder
filter(x,min,max) = (x > min && x < max) ? x : 1/0
plot 'data-smoothed' using (filter($1, -1, -0.5)):2 with filledcurves x1 lt 1 notitle,\
'' using (filter($1, 0.2, 0.8)):2 with filledcurves x1 lt 1 notitle,\
'' using 1:2 with lines lw 3 lt 1 title 'curve'