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.
Related
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 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.
How can I plot (a 2D plot) a matrix in Gnuplot having such data structure, using the first row and column as a x and y ticks (the first number of the first row is the number of columns) and represent the rest of the values by a colour mapping so it can be seen on a 2D plane ?
4 0.5 0.6 0.7 0.8
1 -6.20 -6.35 -6.59 -6.02
2 -6.39 -6.52 -6.31 -6.00
3 -6.36 -6.48 -6.15 -5.90
4 -5.79 -5.91 -5.87 -5.46
You can plot this data format using matrix nonuniform.
To get a heatmap you can plot either with image (regular grid, no interpolation, one quadrangle for each data point), or splot with pm3d (supports also irregular grids and interpolation, plots one quadrangle for four neighboring data points.
with image
set autoscale xfix
set autoscale yfix
set autoscale cbfix
plot 'data.dat' matrix nonuniform with image notitle
pm3d
set autoscale xfix
set autoscale yfix
set autoscale cbfix
set pm3d map
splot 'data.dat' matrix nonuniform notitle
I saw this graph and only for the curiosity sake was wondering whether it was possible to plot figure with multiple y-axis as in the figure
Many thanks!
As andyras wrote, you can use the second y-axis if you only have two datasets. In this case, you also need to to
set ytics nomirror # remove the tickmarks of the left ayis on the right side
set y2tics # make the right y-axis 'visible'
If you want to plot more than one dataset, I would suggest to use multiplot. You can overlay several independent plots and put a unique offset to the y-axis for each of them.
However, you need to take care that the number of y-tics and y-tick positions is the same.
Plot:
(I did not care about the key here, this still needs adjustment)
Code:
set multiplot
set xrange[0:10]
# We need place to the left, so make the left margin 30% of screen
set lmargin screen 0.3
##### first plot
set ytics 0.4
set yrange[-1.2:1.2]
set ylabel "Voltage" textcolor rgb "red"
plot sin(x)
##### Second plot
set ytics 1
set yrange[-3:3]
set ytics offset -8, 0
set ylabel "Current" offset -8, 0 textcolor rgb "green"
plot 3*cos(x) linecolor 2
##### Third plot
set ytics 0.5
set yrange[-1.5:1.5]
set ytics offset -16, 0
set ylabel "Power" offset -16, 0 textcolor rgb "blue"
plot 3*sin(x)*cos(x) linecolor 3
unset multiplot
Yes, you can have two y axes for free, e.g.
plot x, x**2 axes x1y2
The axes specification lets you put things on x1y1, x2y1, etc. If you want more than two things plotted on the same y axes you have to normalize things yourself:
plot 'data1.dat' using 1:($2/MAX_1), \
'data2.dat' using 1:($2/MAX_2), \
'data3.dat' using 1:($s/MAX_3)
The variables MAX_X can be precalculated by using the stats command in gnuplot 4.6+, or you can put them in manually.
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.