I am trying to plot a contour from 3D data, where the contour will only on the base
set contour base
set cntrparam bspline
unset surface
set view map
splot 'file1.dat' u ($1):($2):($3) w l ls 1 notitle, \
'file2.dat' u ($2):($3):(0) w lp ls 2
The second file is just a line which I want to plot in X-Y plane. However, since the surface is unset, the second plot does not show up. If I remove unset surface I see unwanted lines from the first file. Is there a solution to it?
It probably works (I don't have data to test with) if you use nosurface for the first and nocontour for the second plot:
set contour base
set cntrparam bspline
set view map
splot 'file1.dat' u 1:2:3 ls 1 w l nosurface notitle, \
'file2.dat' u 2:3:(0) w lp ls 2 nocontour
Related
basically I would like to reproduce the filling styles in the following image in gnuplot:
I tried using the common pattern with filledcurves but none of the patterns is similar to those in the picture. Is there a way to get this type of patterns (note the filled area is not solid) in gnuplot ?
Thank you.
Check help filledcurves and set the color to transparent according to the scheme 0xAARRGGBB.
Code:
### semitransparent fill between curves
reset session
# create some random test data
set print $Data1
do for [x=1:100] { print sprintf("%g %g %g %g",x,5*cos(x/7.)+rand(0),rand(0)+0.3,rand(0)+0.3) }
set print
set print $Data2
do for [x=1:100] { print sprintf("%g %g %g %g",x,3*sin(x/9.)+rand(0),rand(0)+0.3,rand(0)+0.3) }
set print
unset key
plot $Data1 u 1:2 w l lc "red", \
'' u 1:($2-$3):($2+$4) w filledcurve lc rgb 0xeeff0000, \
$Data2 u 1:2 w l lc "blue", \
'' u 1:($2-$3):($2+$4) w filledcurve lc rgb 0xee0000ff
### end of code
Result:
Addition: (workaround for vertical dashed "fill")
To be honest, (as in your case) if you have to zoom in very close to see the difference between a semitransparent fill and a "fill" with vertical lines, then I'm not sure whether this is really necessary.
Anyway, here is a workaround for a "fill" of vertical dashed lines. This is realized with vectors and dashed lines dt 1 (=solid), dt 2 (=dashed), dt 3 (=dotted). However, this is not a real fill but requires enough regular (here: 1000) data points to give this impression. It also depends on transparency of the linecolor and the size of the graph. If you don't have enough regular datapoints you could resample your data, however, this is not straightforward in gnuplot (see: Resampling data with gnuplot),
This still doesn't look identical to your example but comes somewhat closer.
Code:
### special "fill" with dashed vertical lines
reset session
# create some random test data
set table $Data1
set samples 1000
plot [1:100] '+' u (x):(3*cos(x/5.)):(rand(0)*0.5+0.3):(rand(0)*0.5+0.3) w table
set table $Data2
set samples 1000
plot [1:100] '+' u (x):(5*sin(x/7.)):(rand(0)*0.5+0.3):(rand(0)*0.5+0.3) w table
set table $Data3
set samples 1000
plot [1:100] '+' u (x):(7*sin(x/9.)):(rand(0)*0.5+0.3):(rand(0)*0.5+0.3) w table
unset table
unset key
plot $Data1 u 1:2 w l lc "red", \
'' u 1:($2-$3):(0):(1) w vectors lc rgb 0xddff0000 dt 1 nohead, \
$Data2 u 1:2 w l lc "green", \
'' u 1:($2-$3):(0):($4+$3) w vectors lc rgb 0xdd00ff00 dt 2 nohead, \
$Data3 u 1:2 w l lc "blue", \
'' u 1:($2-$3):(0):($4+$3) w vectors lc rgb 0xdd0000ff dt 3 nohead
### end of code
Result:
Zoomed-in to show the dashed lines:
I have a script which takes data (formatted in 3 columns x,y,z) and gives a heat map:
set logscale x 10
set yrange [1e-9:2e-8]
set xlabel "x"
set ylabel "y"
set multiplot
plot 'filetest.dat' u 1:2:9 with image
This is a 2D heat map, shown below:
All I want to do is add contours to this plot, at some z values such as -20 to -8 in in intervals of 2. Unfortunately, none of the answers I've found have been able to help me with this. Any help would be greatly appreciated.
Although there are a lot of examples about contour on www.gnuplot.info, I couldn't find your exact case, because the examples are with functions, not with datablocks or data files (well, it should be similar).
The following code does what you're asking for, but the construct '' u 1:2:3:("") w labels for adding labels still looks strange to me and doesn't allow for plotting boxed labels.
In gnuplot console check help contour and help cntrparam.
Code:
### pm3d with contour lines
reset session
set view equal xyz
# create some test data
set samples 40
set isosamples 40
set table $Data
splot '++' u 1:2:($1*$2/2-9)
unset table
set view map
set contour base
set cntrparam levels incremental -20,2,-8
set cntrlabel font ",10"
set xrange[-5:5]
set yrange[-5:5]
splot $Data u 1:2:3 w pm3d notitle, '' u 1:2:3:("") w labels notitle
### end of code
Result:
Addition:
Here is another approach with plot w image instead of splot w pm3d.
Although still not fully satisfying with the white label boxes on top of the contour lines. Adding an offset to the labels will not work for all labels at the same time. I'm not sure whether there is a way to just interrupt the contour lines for the labels.
Code:
### heatmap with contour lines
reset session
set view equal xyz
# create some test data
set samples 40
set isosamples 40
set table $Data
splot '++' u 1:2:($1*$2/2-9)
unset table
set view map
set contour base
set cntrparam levels incremental -20,2,-8
set cntrlabel font ",10"
set xrange[-5:5]
set yrange[-5:5]
set style textbox noborder opaque
# put contour lines in a separate datablock
unset surface
set table $Contour
splot $Data u 1:2:3
unset table
plot $Data u 1:2:3 w image notitle, \
$Contour u 1:2 w l lw 2 lc "black" not, \
'' u 1:2:3 every 40::3 w labels boxed notitle
### end of code
Result:
Addition 2:
Another variation with colored contour lines and key instead of labels. This seems to be a bit cumbersome, I hope there is a simpler solution for this.
Code:
### heatmap with colored contour lines
reset session
set view equal xyz
# create some test data
set samples 40
set isosamples 40
set table $Data
splot '++' u 1:2:($1*$2/2-9)
unset table
set view map
set contour base
set cntrparam levels incremental -20,2,-8
set xrange[-5:5]
set yrange[-5:5]
set style textbox noborder
# put contour lines in a separate datablock
unset surface
set table $Contour
splot $Data u 1:2:3
unset table
# get contour levels unique and in sorted order
set table $Dummy
plot $Contour u 3 w table
unset table
set table $ContourSorted
plot $Dummy u 1 smooth freq
unset table
print $ContourSorted
set key out right Left
plot $Data u 1:2:3 w image notitle, \
for [i=0:*] $Contour u 1:2:3 index i w l lw 2 lc i+1 not, \
for [i=|$ContourSorted|-2:5:-1] $ContourSorted u (NaN):1 w l lw 2 lc |$ContourSorted|-i-1 ti word($ContourSorted[i],1)
### end of code
Result:
I want to plot the contour of the following function:
f(x,y)=y³*b(x)
My data-file looks something like this:
x b(x)
-1 10.123
-0.995 10.112
-0.99 10.100
I am not sure how to make the right splot command as my datafile does not look like (x y z).
Thats my script so far:
reset
f(x,y)=y³*b(x)
set xrange [-6:6]
set yrange [-5:5]
set isosamples 50
set table 'test.dat'
splot 'Data.dat' u 1:(b(x)=$2, f(x,y)) -------------------------?
unset table
set contour base
set cntrparam bspline
set cntrparam levels incremental -0.1,0.02,0.1
unset surface
set table 'contour.dat'
splot 'Data.dat' u 1:(b(x)=$2, f(x,y)) -------------------------?
unset table
reset
set xrange [-6:6]
set yrange [-5:5]
unset key
set palette rgbformulae 33,13,10
plot 'test.dat' with image, 'contour.dat' w l lt -1 lw 1.5
Creating a 3D surface directly from the data in your file will not work because their is no y coordinate data. The program knows how to plot data and it know how to plot functions but you have to choose one or the other.
To treat the plot as a data plot you will need to expand the file to contain x/y/z data (see 'help matrix'). This is probably easier to do outside of gnuplot.
Alternatively you can recast your function b(x) in some analytic form, possibly by using gnuplot's "fit" command and your existing data file. Suppose for example that a quadratic fit to your data is sufficient:
b(x) = C0 + C1*x + C2*x*x + C3*x*x*x
C0=C1=1; C2=C3=0;
fit b(x) 'test.dat' using 1:2 via C0,C1,C2,C3
Now you have analytic forms for both the x and y dependence of the surface to be contoured
f(x,y) = b(x) * y*y*y
set contour base
set cntrparam bspline
set cntrparam levels incremental -0.1,0.02,0.1
unset surface
splot f(x,y)
i am 3d plotting a matrix with some values, and i need to add contour lines to the plot, is there a simple gnuplot command to do this?
I tried the command: "set contour base" but only 1 line came up, i think it should be many lines. See matlab picture
When i plot it in gnuplot i only get 1 contour line in the top left corner.But everything else is correct.
My goal is to get it to look like in matlab like this Matlabplot
I also found this example: see link in comments (dont have enough rep), but i dont understand where i should put in the data values from test.txt
test.txt
test.txt
gnuplot commands
set view map
set yrange [0:30]
set xrange [0:30]
set dgrid3d 100,100,4
set contour base
splot 'test.txt' u 1:2:3 w pm3d
What you are missing is to tell gnuplot where to put the contours. This is done via the set cntrparam levels incr -0.3,0.1,0.5 command which means: start at -0.3 and trace a contour every o.1 up to 0.5.
AFAIK if you want to make contours all black, you have to save the contour lines in a temporary file (here contour.txt).
So your script would be
reset
set contour
unset surface
set cntrparam levels incr -0.3,0.1,0.5
set view map
set xrange [0:30]
set yrange [0:30]
set dgrid3d 100,100,4
set table "contour.txt"
splot 'test.txt'
unset table
unset contour
set surface
set table "dgrid.txt"
splot 'test.txt'
unset table
reset
set pm3d map
unset key
set palette defined (0 '#352a87', 1 '#0363e1',2 '#1485d4', 3 '#06a7c6', 4 '#38b99e', 5 '#92bf73', 6 '#d9ba56', 7 '#fcce2e', 8 '#f9fb0e')
set autoscale fix
set grid
splot 'dgrid.txt' w pm3d, 'contour.txt' w l lc rgb "black"
which gives you this:
Note:
you can get rid of interpolation file (dgrid.txt) if you format a bit your datafile by leaving a blank line after each row (i.e. every 30 datapoints) because they are already mesh-ordered.
This could be done also with a awk script. But I'm too lazy to look into it...
The rest will remain the same and will work as expected.
here is how it should look like :
In which case the script would simply become:
set pm3d map impl
set contour
set style increment user
do for [i=1:18] { set style line i lc rgb "black"}
set cntrparam levels incr -0.3,0.1,0.5
set palette defined (0 '#352a87', 1 '#0363e1',2 '#1485d4', 3 '#06a7c6', 4 '#38b99e', 5 '#92bf73', 6 '#d9ba56', 7 '#fcce2e', 8 '#f9fb0e')
set autoscale fix
splot 'test.txt' w pm3d notitle
with no need of ntermediate file and with better contour since data in not interpolate by gridded:
I've been unable to find a way of producing the plot described in the title, does anyone know how to do this if it's possible? I'd like to plot a surface from a function, and plot points from a file, and have lines drawn between the points and the surface. The link below is an image that contains an example of what I'd like to do, taken from a stackoverflow question for an identical task (but for a different plotting program).
You can do the calculations inside gnuplot's using statement. For the lines I use the vectors plotting style, and with arrowstyle variable (or linecolor variable) you can select different colors depending on the dz value:
set style arrow 1 linecolor rgb 'red' nohead
set style arrow 2 linecolor rgb 'green' nohead
splot f(x,y) with lines, \
'points.dat' using 1:2:(f($1,$2)):(0):(0):(dz=$3-f($1,$2)):(dz < 0 ? 1 : 2) with vectors arrowstyle variable,\
'' using 1:2:3 with points pt 7 ps 5
I'm not sure, which this gives problems with gnuplot 4.6. For your case you can also use linecolor variable or linecolor rgb variable to change the color of the vertical lines:
splot f(x,y) with lines, \
'points.dat' using 1:2:(f($1,$2)):(0):(0):(dz=$3-f($1,$2)):(dz < 0 ? 0xff0000 : 0x00ff00) with vectors nohead linecolor rgb variable lw 5,\
'' using 1:2:3 with points pt 7 ps 5