gnuplot 2D polar plot with heatmap from 3D dataset - possible? - gnuplot

I'd like to take a plot that I have done as a surface in 3D using cartesian coordinates and view it as a heatmap in 2D in POLAR coordinates. The reason for this is I am obtaining data on a system in that coordinate system. I've only found some related examples, but am getting stuck trying to get it to work with my data. I am currently using the matrix format, and I can reformat the data set if this would help get the chart working.
The data is SPL measurements taken on a loudspeaker. The microphone is positioned at a fixed distance away (e.g. fixed radius) and measurements are made at every 10 degrees around the entire loudspeaker horizontally. The SPL measurement is obtained as a function of frequency, 20Hz to 20kHz.
I would like to use gnuplot to create a 2D polar plot. Frequency would be plotted as the radius, the angle around the loudspeaker would be the angle, and the "height" would be the SPL level. This should generate a surface, however, I would like to create a heat map pm3d and then view that from above (e.g. view 0,0) or as a 2D plot. I also need to add contour lines that show intervals of SPL and superimpose that on the heat map.
I found something similar for cartesian coordinates here:
http://gnuplot-tricks.blogspot.com/2009/07/maps-contour-plots-with-labels.html
When I tried this approach using polar coordinates for the final 2D plot, I got an error message that the "with image" option is not supported for polar plots. Can someone try this or confirm this?
I have been able to plot my polar data as a heatmap+contour lines in 3D using splot and view from directly above (set view 0.0). If I first convert my existing polar coordinate data into cartesian coordinates I will probably get something like what is shown in this web page:
how to create a 3d polar graph with gnuplot
I could view this from above, too, but I would like to add in the polar grid and have labels for the angle and radius. Would I have to do this manually or could I use multiplot to overlay a 2D grid and the 3D plot viewed from 0.0?
I am not sure how to approach this. Any advice about what direction to take would be appreciated.
-Charlie

The image plotting works only for equally distributed rectangular grids, just like any bitmap image is arranged. Otherwise you must use splot with pm3d.
The set grid polar works only for 2D, so you must use multiplot to overlay your heatmap with the polar grid. Here a, quite lengthy, example to show you how it might work:
reset
set terminal pngcairo size 800,800
set output '3d-polar.png'
set lmargin at screen 0.05
set rmargin at screen 0.85
set bmargin at screen 0.1
set tmargin at screen 0.9
set pm3d map
unset key
set multiplot
# plot the heatmap
set parametric
set isosamples 500
unset border
unset xtics
unset ytics
set angles degree
r = 6
set urange[0:r] # radius
set vrange[0:360] # angle
set xrange[-r:r]
set yrange[-r:r]
set colorbox user origin 0.9,0.1 size 0.03,0.8
splot u*cos(v), u*sin(v), (cos(v)*besj0(2*u))**2
# now plot the polar grid only
set style line 11 lc rgb 'white' lw 2
set grid polar ls 11
set polar
set rrange[0:r]
unset raxis
set rtics format '' scale 0
unset parametric
set for [i=0:330:30] label at first (r+0.35)*cos(i), first (r+0.35)*sin(i)\
center sprintf('%d', i)
plot NaN w l
unset multiplot
The result is:
And now some details about some tricks:
In order to get a square size, you can't use set size ratio 1, because the margins differ for the 2D and 3D plots, even if you would specify some absolute margins. Therefore, I set a square canvas size (terminal option size 800,800), and set appropriate absolute margins.
You cannot unset rtics because then the grid would disappear.
The grid labels must be set manually.
The colorbox was also set manually because otherwise it would have overlapped with the 0 label.
Plotting NaN does only plot the grid

I took the command file that Christoph posted and played around a bit and managed to get it working for my needs EXCEPT for the labels, which I am still having problems with. The plot is looking like this:
In order to get this result, I had to recalculate the coordinates of my measurement data, changing them from a polar coordinate system (frequency=r, theta=off-axis angle, z=SPL) to a Cartesian one (x,y,z). At the same time I modified the way that the polar grid was presented. Although I wanted a logarithmic polar r-axis, Cartesian coordinates must be used for the pm3d data, so I took the log of the r data before using it to calculate x,y,z coordinates. Also, I knew that the minimum value of the polar log r-axis scale would be 10, and this seems to be set equal to the center of the plot when a logscale polar grid is used. In order for the grid and the surface data to line up properly, I subtracted log10(10) from the r values before using them to calculate the Cartesian coordinates used for the pm3d map. So in total, the equations I used were
r = log10( frequency ) - 1
x = r cos( theta )
y = r sin( theta )
z = SPL
I then used the following command file to plot the data:
reset
set terminal pngcairo size 800,800
set output '3d-polar.png'
set lmargin at screen 0.05
set rmargin at screen 0.85
set bmargin at screen 0.1
set tmargin at screen 0.9
set pm3d map interpolate 20,20
unset key
set multiplot
# plot the heatmap
set cntrparam levels increment 3,-3, -24
set contour surface
set palette rgb 33,13,10 #rainbow (blue-green-yellow-red)
set cbrange [-18:0]
unset border
unset xtics
unset ytics
set angles degree
r = 3.31
set xrange[-r:r]
set yrange[-r:r]
set colorbox user origin 0.9,0.1 size 0.03,0.8
splot 'new_test.dat' using 1:2:3
# now plot the polar grid only
set style line 11 lc rgb 'black' lw 1 lt 0
set grid polar ls 11
set polar
set logscale r 10
set rrange[10:20000]
unset raxis
set rtics format '' scale 0
set rtics (10,20,100,200,1000,2000,10000,20000)
#unset parametric
#set for [i=0:330:30] label at first (r+0.35)*cos(i), first (r+0.35)*sin(i) \
#center sprintf('%d', i)
plot NaN w l
unset multiplot
unset output
The data used to generate the plot only span +/- 30 degrees, so only that sector fills the polar plot. Additional angular data can be used to fill out the plot.
If I can get some help with labels, I can call this "done". I still need to get the labels of angle working. Input on how to label the polar r-axis tics that would be very welcome, too.

Related

4D contour plots in gnuplot

I have 4D data X,Y,Z plus a field. I would like to do a 4d plot of the field restricted to a sphere. I already use splot w pm3d to have the color bar on the sphere indexed by my field, but I think it would be more readable if I could add 3d contour lines.
I would like something like last example of "function plot" (protein orientation) at http://www.originlab.com/index.aspx?go=Products/Origin/Graphing
Is it even possible in gnuplot ? If so, how to do it ?
Gnuplot cannot generate 4d contour plots.
But if I understand correctly, you have a special case, you do not really have 4d data. The z coordinate depends on x and y such that the point is on a sphere. Maybe this can be used to get the contours.
I assume the datafile contains datapoints for the complete sphere, arranged circle by circle from bottom to top of the sphere, each circle in a separate block.
I have tried this:
Separate the points on the upper half from points of the lower half of the sphere, we need this separation for the sign of z.
Plot the contours of the two halfs in two datafiles contour_l.dat and contour_u.dat. This will only plot the x and y coordinates.
Merge the original datafile and the two contour datafiles into one plot. Pythagoras could help reconstructing the z coordinates for the contour datafiles.
This is the script:
set pm3d depthorder interpolate 5,5
set hidden3d front
unset surface
set contour surface
set zrange [0:1.1]
set table "contour_u.dat"
splot "sh.dat" using 1:2:4 w l
unset table
set zrange [-1.1:0]
set table "contour_l.dat"
splot "sh.dat" using 1:2:4 w l
unset table
set surface
unset contour
set xrange [-1.1:1.1]
set yrange [-1.1:1.1]
set zrange [-1.1:1.1]
set xyplane relative 0.0
set terminal pngcairo size 640,640
set output "c.png"
unset key
splot "sh.dat" using 1:2:3:4 w pm3d, \
"contour_u.dat" using 1:2:( sqrt(1.0-($1*$1+$2*$2))):3 w l lc rgb "black",\
"contour_l.dat" using 1:2:(-sqrt(1.0-($1*$1+$2*$2))):3 w l lc rgb "black"
You did not post data, so I have taken one of the spherical harmonics. With Gnuplot 4.6, I get the following result:
As you can see, it is still not perfect. The contour at x=0 should be investigated, and if I remove the "sh.dat" w pm3d line, the image gets very strange.
But at least this approach might be a starting point, one could try to manually play with the contour line datafiles.

Adjusting position of individual plots in gnuplot multiplot

I am using multiplot to plot four graphs. My code is given below
set term postscript eps enhanced color
set pm3d map
set pm3d corners2color c1
set size square
set out 'defect2.eps'
unset colorbox
set colorbox horiz user origin 0.1,0.7 size 0.8,0.02
set cbrange [-1.6:0]
set xtics 25
set ytics 25
set lmargin at screen 0.1
set rmargin at screen 0.9
set multiplot layout 1,4
set xlabel "i" font "Times-italic,20"
set ylabel "j" font "Times-italic,20"
spl 'defect.dat' notitle
unset ylabel
spl 'defect_2.dat' notitle
spl 'defect_3.dat' notitle
spl 'defect_4.dat' notitle
unset multiplot
set out
This produces a following plot
However, individual plots in the picture are not properly placed. I cannot see ylabel for the first plot as it is hidden behind the screen whereas there are large blank gaps between those plots. Is there any way to reduce these gaps, increase size of these plots and show the labels properly?
Thanks in advance.
The Problem is your margin setting. The margin defines the size of the white stripe between the box containing the plot and the border of the window.
This means, tic and axis labels are printet on the Martin area! And your 10% of window width is not enough, so the labels are clipped.
So you can adjust the margins to shift all plots more to the left or adjust label font sites and offsets (Miguel's comment).

chart is flipped when using multiplot

I want to plot two charts (reading the same data points) using multiplot. The purpose is to put the second (smaller) chart in the right of the first (bigger) chart.
The code is
set term post eps enhanced color blacktext size 4,4 solid "Times-Roman" 14
set output 'cla.ps'
set multiplot
# draw bottom and left lines
unset border
unset xtics
unset ytics
set border 3
# increasing the canvas size
set rmargin 50
set tmargin 2
# put the big chart, it will not use the whole space
set origin 0,0
set size 1.2,0.5
plot 'test.txt' u 1 with points lc rgb "black"
# put the small char in the right
set origin 0.6,0.2
set size 0.2,0.2
plot 'test.txt' u 1 with points lc rgb "black"
# always unset multiplot
unset multiplot
Problem is, the second chart is shown in a mirror fashion (horizontal flip). But I didn't such an option. How can I fix that?
That should give you a nice warning explaining that: warning: Terminal canvas area too small to hold plot. Check plot boundary and font sizes.
With set rmargin 50 you set the right margin to be 50 character widths. That applies also to the second plot unless you use e.g. set rmargin -1 to reset it to automatic calculation.
But I don't see any sense to set both the size and the rmargin. Setting the rmargin does not increase the canvas size, like you suggest in your comments.
Here is a working example:
set term post eps enhanced color blacktext size 4,2 solid "Times-Roman" 14
set output 'cla.ps'
set multiplot
# draw bottom and left lines
unset tics
set border 3
# put the big chart, it will not use the whole space
set origin 0,0
set size 0.8,1
plot x**2
# put the small char in the right
set origin 0.75,0.2
set size 0.25,0.3
plot x
# always unset multiplot
unset multiplot
That gives you the output (tested with 4.6.5):

How do I place tic marks along the top borders in a 3d plot?

I'm making some 3D surface plots in Gnuplot and it would be very useful to have tic marks along each border of my plot. In the attached sample plot, there are no tic marks along the top left or top right horizontal borders (borders 256 and 512). In order for vertical grid lines to be drawn on the back vertical planes, I need to have tic marks on these borders. How can I achieve this?
I have not found a way to solve this using the grid and border. However, there is a relatively simple workaround, which is useful only if you do not change your ranges every time you plot your data.
Basically you plot a constant surfaces on the back walls matching the line type and number and position of the grid lines in the x-y plane.
First, set up the ranges. I labeled them, because we will need the numbers later.
xmin=0 ; xmax = 100
ymin=0.01 ; ymax=1000
zmin=0 ; zmax=990
set xrange [xmin:xmax]
set yrange [ymin:ymax]
set zrange [zmin:zmax]
Setting the z axis intersection with x-y plane (ticslevel) and I guessed a view angle to visually match your example. We want to set these before multiplot.
set ticslevel 0.0
set view 60,45,1
Now comes the fun. For this part you have to know the number of grid lines in your x-y plane (same as number of major tics on x and y axes). We will plot the back grid walls first, so they are behind your data/function at the end. Also, I switch off the grid and border for this part, but they should exactly overlap if you leave them in.
set multiplot
unset grid
set border 0
Even though the y-axis has logarithmic scale, the grid is separated linearly (equidistant grid lines). So at this point I want linear scale on y axis. (If you set logscale y before this point, comment it out.) I don't want linear labels on the y-axis, because they are different on logarithmic scale, so I set the format accordingly.
set format y ""
set isosamples 6,9 # - set this to number of tics on y-axis,z-axis
Here is the only manual setting that might change when you plot on a different range. You need to set the numbers of isosamples to the numbers of tics on y-axis,z-axis.
To control how many lines gnuplot is going to use for each surface, we need to set both the isosamples (done above) and the ranges of dummy variables u,v. Notice the line type 0, which is the grid line type.
I plot my first wall at x = xmin:
set parametric
set urange [ymax:ymin]
set vrange [zmax:zmin]
splot xmin,u,v w lines lt 0
Similarly, we do the other wall at y = ymax.
set urange [xmin:xmax]
set vrange [zmin:zmax]
splot u,ymax,v w lines lt 0
unset parametric
Now that I have the walls, I can plot what you already have in the picture. Setting borders, tics, re-enable the y-axis label we disabled before, set the log-scale on y axis (now is a good time) and reset isosamples to the default values.
set xtics mirror
set ytics mirror
set ztics mirror
set grid ytics xtics back
set logscale y
set format y " %g"
set isosamples 10,10
And plot your data/function as you are used to.
splot 'data.txt' w lines
unset multiplot
And we are done....
Possible necessary modification:
I guess your x and y axes will be different from mine, since the reverse is easily achievable by something like view 60,135,1. This also switches x and y though. Your surfaces will then change coordinates.
You can try setting tics there using x2tics and y2tics.
Here is the documentation of Xtics.

Reduce distance between points in splot

I have this gnuplot script
reset
set palette model RGB defined (0 "gray", 0.1 "white", 0.33 "yellow", 0.66 "orange", 1 "red")
set xlabel "x"
set ylabel "y"
set view map
set border 0
unset xtics
unset ytics
splot file_name u 1:2:5:xtic(3):ytic(4) w points ps 5 pt 5 palette
And this is the result:
How can I remove the distance between the points so that I end up with a set of adjacent squares? I want to plot a heatmap with a square for each point in my grid file.
EDIT
The correct way to plot a "grid" heatmap as per #andyras answer is:
set pm3d map
plot file_name u 1:2:5:xtic(3):ytic(4) with image
Which gives this image:
gnuplot offers a third way to plot a heatmap based on connecting points of a 3d surface, rather than grid cells. That is, the x,y coordinates at columns 1,2 are used as corners, or connecting points, of a surface mesh and the colors used in each region are the average of the RGB/HSV values for the 4 defining corners:
set pm3d map
splot file_name u 1:2:5:xtic(3):ytic(4)
I usually go with the options
set pm3d map
plot file_name u 1:2:5:xtic(3):ytic(4) with image
for gridded data (it makes a smaller file if you use vector formats). I suspect your problem may be to do with the fact that you specify a point style and size for your splot. So, you could try setting the pm3d map option and using splot without the point specification, or plot ... with image.

Resources