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:
Related
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 have a script that generates a gnuplot file, I have edited so I have 2 sets of data in the same plot. One is multiplied by -1 to change color. However, only one set appears in the plot, the last one stated. The script is the following.
set arrow from graph 0,first 0.61237, graph 0 to graph1,first0.61237,0 nohead front lt -1
set arrow from graph 0,first 1.67303, graph 0 to graph 1,first 1.67303,0 nohead front lt -1
set arrow from graph 0,first 2.53906, graph 0 to graph 1,first 2.53906,0 nohead front lt -1
set arrow from graph 0,first 3.24616, graph 0 to graph 1,first 3.24616,0 nohead front lt -1
set arrow from graph 0,first 3.74616, graph 0 to graph 1,first 3.74616,0 nohead front lt -1
set arrow from graph 0,first 4.74616, graph 0 to graph 1,first 4.74616,0 nohead front lt -1
set pm3d at b
unset surf
set view 180,90
set palette defined (-1 "red",0 "white", 1 "blue");
set grid xtics mxtics ytics
unset border
set xrange [-10:40]
set xtics -10,((40+10)/5),40
set mxtics 3
set yzeroax lt 2
set ytics ("L" 0.0,"K" 0.61237,"G" 1.67303,"L" 2.53906,"W" 3.24616,"X" 3.74616,"G" 4.74616)
set cbrange resto
set cbrange[-1:1]
set colorbox
set terminal png enhanced nocrop size 1920,1080
set out "SPF_NO_SO.png"
splot "specfun.pm3d" u ($1*13.60569806):2:(1*$4/476.70750366666666666666),\
"specfun.pm3d" u ($1*13.60569806):2:(1*$3/573.04673900000000000000)
unset out
Sample image:
The script produces the desired plot only for the negative set. If I flip the order I get the blue one.
Here is a sample data:
data_sample
FYI-these arrays are quite big
as #user8153 pointed out, I also don't see a reason why using splot.
And as #Christoph pointed out it's difficult without knowing how the data looks like,
for example, what's in columns $3 and $4?
By the way, with your plot command ...u ($1*13.60569806):2 aren't you plotting the same function twice on top of each other, i.e. that's why you see either the red or the blue?
Nevertheless, I tried to anticipate what you probably want to do. I scaled the second function and assumed something linear for columns $3 and $4. Please clarify.
With the code:
### start code
reset session
# generate some dummy data
undefine $Data
set print $Data append
do for [n=1:5] {
a = rand(0)*10
b = rand(0)*0.1+0.1
c = rand(0)*20
do for [i=1:100] {
print sprintf("%g\t%g\t%g\t%g",i,(a*sin(b*i-c)),cos(b*i),cos(b*3*i))
}
print "" # insert empty line for data curve separation
}
set print
# print $Data
# end generating dummy data
set palette defined (-1 "red",0 "white", 1 "blue")
set cbrange [-1:1]
plot $Data u 1:2:($3-$4) w l lc palette z
### end of code
You'll get the following:
I have a simple data I want to plot as 3D plot (3 columns divided by a comma):
33.26,0.0000001,1
67.02,0.0000010,2
101.64,0.0000100,3
137.53,0.0001000,4
175.06,0.0010000,5
214.59,0.0100000,6
256.47,0.1000000,7
301.09,1.0000000,8
348.78,10.0000000,9
399.92,100.0000000,10
454.87,1000.0000000,11
513.99,10000.0000000,12
577.65,10000.0000000,13
646.22,10000.0000000,14
720.05,10000.0000000,15
799.51,10000.0000000,16
884.96,10000.0000000,17
976.77,10000.0000000,18
1075.29,10000.0000000,19
1180.89,10000.0000000,20
1293.92,10000.0000000,21
1414.77,10000.0000000,22
1431.83,10000.0000000,23
1449.15,10000.0000000,24
1466.97,10000.0000000,25
1485.79,10000.0000000,26
1505.97,10000.0000000,27
1527.88,10000.0000000,28
1551.87,10000.0000000,29
1578.3,10000.0000000,30
1607.56,10000.0000000,31
1639.98,10000.0000000,32
1675.95,10000.0000000,33
1715.82,10000.0000000,34
1759.96,10000.0000000,35
1808.72,10000.0000000,36
1862.49,10000.0000000,37
1921.6,10000.0000000,38
1986.44,10000.0000000,39
2057.35,10000.0000000,40
2134.71,10000.0000000,41
2218.87,10000.0000000,42
2310.2,10000.0000000,43
2409.06,10000.0000000,44
2515.83,10000.0000000,45
I wrote a simple script to plot the above data:
#!/usr/bin/gnuplot
set palette rgbformulae 33,13,10
set datafile separator ","
set terminal postscript eps size 10.5, 5.62 enhanced color font 'Helvetica,20' linewidth 2
set output 'test.eps'
set xlabel "time [s] (no operation)" offset -4, 0, 0
set xtics left offset 0,-0.3 rotate by 45 right
set xrange [0:400]
set ylabel "ranges" offset 2, 0, 0
set ytics left offset 0,-0.5
set zlabel "devices" offset -4, 0, 0
set zrange [0:50]
set autoscale
set title " "
set key inside left top;
set dgrid3d 30,30
set hidden3d
set style line 1 linecolor rgb '00FF00' linetype 4 linewidth 1
splot "data.csv" u 1:2:3 title "" with lines palette
And my output:
As you all can see, the output image (or, I should say), the x,y and z ticks on axis x,y, and z are not enough detailed. It is hard to say that the output image was plotted with this data.
Is there a way that would let me manipulate the x,y, and z ticks, to be taken from file, in some elegant way?
I also would like the image to be more readable with new x,y, and z ticks, so I think that the 10000.0000000 value should appear only once, when it appeared for the first time in data file.
Thank you.
Not exactly an answer to your question, and it is my personal opinion, but you might be interested in the ideas:
The data seems not to be grid data, so I would not use a surface plot of any kind.
Plotting only the datapoints in 3d does not give a useful picture, it is only a single line somewhere in space. I would try to use a 2D plot which contains the height information as color.
I would use a logscale for the y-axis.
This leads to the following script:
set terminal pngcairo
set output 'test.png'
set datafile separator ","
set palette rgbformulae 33,13,10
# Set margins to keep colorbox label inside the picture
set lmargin screen 0.12
set rmargin screen 0.85
set xlabel "time [s] (no operation)"
set ylabel "ranges"
set cblabel "devices"
unset key
set yrange [1e-8:1e5]
set ytics format "1e%+T"
set logscale y
set view map
set cbrange [0:50]
set zrange [0:50]
splot "data.csv" u 1:2:3 w p pt 7 palette ,\
"data.csv" every 5::4 u ($1+0):($2/3):(0):($3 != 30 ? 3 : "") with labels
It also prints the z-labels of some datapoints, skipping 30 for spacing reasons.
This is the result:
I am new to gnuplot. I am trying to superimpose both a contour plot and some data points on the x-y plane.
Data for my contour is given (surface1.txt) here
Data for the single points is given (points1.txt) here
I am trying to run this script:
set multiplot
# plot the contour from the surface1.txt
unset key
set dgrid3d
unset surface
set contour base
# these values need to be set (requirement)
set cntrparam level incremental 0.16, 0.259, 4.47
set view 0,0
unset ztics
splot "surface1.txt" with lines
# plot the points on the x-y plane
unset xtics
unset ytics
splot "points1.txt"
unset multiplot
and I am getting this output:
As you can see, the single points that I am trying to put on the x-y plane also comes as contour, what I need to do is:
display the points from "points1.txt" as single points (not contour)
the plot is zoomed out, I need a full screen image.
move the y-axis ticks from right to left vertical.
remove all the colors. I need a grayscale image.
Please help.
EDIT:
I have also tried this way --
set contour base
set cntrparam bspline
set cntrparam level incremental 0.16, 0.259, 4.47
set view map
set dgrid3d
unset key
splot 'surface1.txt' nosurface with lines, \
'points1.txt' nocontour
and I am getting this plot --
There are several things you must regard:
set dgrid3d interpolates your data and is thought to be used for non-gridded data. There is no option nodgrid3d which allows you to use it only for one plot part. This is what you see in your second attempt: the data for the points is interpolated and a 10x10 grid is generated.
However, you don't need to use this, because you have gridded data, but are missing a few empty lines in your data file. Just insert a single empty line when the x-value changes, like:
...
0.0 0.7999999999999999 2.0477812692428836
0.0 0.8999999999999999 2.3096674656635523
0.0 0.9999999999999999 2.5772908911794614
0.1 0.0 0.8254201558219569
0.1 0.1 1.0350909705482707
0.1 0.2 1.2504990143698247
...
Use the nosurface option for the contours, and nocontours when plotting the points.
A possible script could be:
set contour base
set cntrparam level incremental 0.16, 0.259, 4.47
unset key
set view map
set for [i=1:20] linetype i lc rgb 'black'
set terminal pngcairo dashed size 600,400
set output 'contour-with-points.png'
splot 'surface1.txt' with lines nosurface, 'points1.txt' with points nocontour pt 7
with the result
I used the pngcairo terminal only for this example, for your document you should probably use a vector format, like produced by pdfcairo, postscript, epslatex or similar.
I have a datafile that looks like this:
1 2 3 0.5
2 8 9 0.2
3 4 78 0.4
6 5 7 0.01
9 9 9 0.3
10 12 18 0.9
6 8 4 1
I would like to do a graph like this
http://2.bp.blogspot.com/-378_rAaSSVU/UzU0gnGcr9I/AAAAAAAABnU/P1GwP9RKBkM/s1600/gnuplot.png
Where the 4th column is the color.
I tried - obviously incorrect because I do not use the fourth column but I failed to find anything in the documentation:
set dgrid3d 30,30
set view 60,45
set hidden3d
dataFile='prova.dat'
set palette defined (0 "blue", 0.5 "white", 1 "pink")
set pm3d
splot dataFile u 1:2:3 with pm3d
Is somethings like that possible?
Using only pm3d you can use a fourth column to select a color independent of the z-value. Together with dgrid3d this is not directly possible, because the gridding is not performed on the color column.
You can use a workaround: First you plot the gridded z-value to one file, then the gridded color values to a second file and as last point you disable dgrid3d, merge the two temporary files on-the-fly and plot their values:
set dgrid3d 30,30
dataFile='prova.dat'
set table dataFile.'.grid'
splot dataFile u 1:2:3
unset table
set table dataFile.'.color'
splot dataFile u 1:2:4
unset table
set view 60,45
set hidden3d
set palette defined (0 "blue", 0.5 "white", 1 "pink")
set autoscale cbfix
set pm3d
unset dgrid3d
set ticslevel 0
splot sprintf('< paste %s.grid %s.color', dataFile, dataFile) u 1:2:3:7 with pm3d notitle
Note, that paste is a command line tool for Unix-like operation systems. For a similar solution for windows, you can e.g. write a small Python script paste.py (see my answer to Get ratio from 2 files in gnuplot for a possible implementation). Then you must run the wgnuplot_pipes.exe binary file and the splot command becomes
splot sprintf('< python paste.py %s.grid %s.color', dataFile, dataFile) u 1:2:3:7 with pm3d notitle
Of course, for this you must have python installed and the python binary must be available via the PATH environment variable.