I am trying to plot two types of data in the same graph.
The first is a simple x-y points plot:
plot x
The second is a interpoled matrix, which I can plot with the following commands:
set pm3d map
set pm3d interpolate 0,0
splot "matrixfile" matrix
Both use the same xrange and yrange.
How can I plot them both in the same graph?
In order to combine these two plots, you must use the pseudo-datafile '+' to generate a 1D function with splot. Just using splot x would generate a surface:
set pm3d map
splot "matrixfile" matrix, '+' using 1:1
When using '+' a single column is generated, which samples the xrange.
Here a full example (which uses ++ instead of a data file for demonstration purpose):
set xrange [-5:5]
set yrange [-5:5]
set isosamples 100
set samples 100
unset key
set pm3d map
splot '++' using 1:2:(exp(-($1-$2)**2)), \
'+' using 1:1:(0) with lines
This gives (with 4.6.3):
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 two surfaces with different range. I'm trying to connect them. The surfaces are a cylindrical and a hyperboloid. When I do
gnuplot> splot [-pi:pi][-5:0] 7*cos(u), 7*sin(u), v
gnuplot> replot [-pi:pi][0:1.5] 7*cos(u)*cosh(v), 7*sin(u)*cosh(v), 6*sinh(v)
gnuplot>
I only see the graph with the last range, but I want the resultant surface.
In current gnuplot (version 5.2) the sampling ranges use coordinates u and v, which are distinct from the axis ranges for x and y. To plot multiple surfaces with different ranges you must give the u and v ranges separately for each surface. See for example the 9th plot of the "sampling.dem" demo. An on-line copy is at
http://gnuplot.sourceforge.net/demo_cvs/sampling.html
The commands to generate this plot are
set xrange [1:100]
set yrange [1:100]
set urange [0:100]
set vrange [70:90]
splot '++' using 1:2:($1*25.*sin($2/10)), \
[u=30:70][v=0:50] '++' using 1:2:(u*v), \
[u=40:80][v=30:60] '++' using (u):(v):(u*sqrt(v)) lt 4, \
[u=1:100][v=500:1000] '++' using (90):(u):(v) lt 6
Note that parametric mode is not used.
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.
I'm plotting some data with a different X range and I would like to change yrange according to the maximum and minimum value of the data in the current X range. When I use GPVAL_Y_MAX and GPVAL_Y_MIN, these values correspond to the maximum and minimum of the whole data, not just the data in the range.
For example, I have the following data:
1 3
2 5
3 8
4 20
5 30
I use the following script:
plot 'data.txt' u 1:2;
set xrange [1:3];
replot
set xrange [1:5];
replot
In the first plot I would like to set yrange in [3:8], but in the second plot the yrange sholud be [3:30]. If I use something like
set yrange [GPVAL_Y_MIN:GPVAL_Y_MAX]
GPVAL_Y_MIN and GPVAL_Y_MAX have the same value independently of the xrange.
Any solution?
The variables you want are GPVAL_DATA_Y_MIN and GPVAL_DATA_Y_MAX, which are the y-min/max of the data plotted in a certain range. GPVAL_Y_MIN and GPVAL_Y_MAX are a little less useful generally because they tell you where the edges of the plot border are (in general these values extend a little beyond the GPVAL_DATA... variables because gnuplot leaves a little space between the data and the edge of the plot).
To take advantage of these variables you have to use the range specifiers to the plot command:
plot [1:3] 'data.txt'
set yr [GPVAL_DATA_Y_MIN:GPVAL_DATA_Y_MAX]
replot
...
By the way, the u 1:2 specification is redundant unless you want to remind yourself of which columns you are plotting, since plotting the first two columns as x and y is the gnuplot default. If you don't want to replot to the same output terminal (which is not helpful in some terminals like eps where replotting makes a second page with the same plot), use this command sequence:
set terminal unknown
plot [1:3] 'data.txt'
set terminal <actual output terminal here>
set output 'output.trm'
plot [1:3][GPVAL_DATA_Y_MIN:GPVAL_DATA_Y_MAX] 'data.txt'
Note the use of the range specifier again, this time with a y range specified. This is a little more compact than specifying with set yrange, but makes for a longer line of code.
If you have gnuplot 4.6.0 or higher, you can take advantage of the stats command to avoid replotting. The stats command creates a bunch of handy variables
stats [1:3] 'data.txt'
plot [1:3][stats_min_y:stats_max_y] 'data.txt'
A slightly different command,
stats [1:3] 'data.txt'
plot [stats_min_x:stats_max_x][stats_min_y:stats_max_y] 'data.txt'
Would fill the plot in the x direction based on where the actual data lie. For instance if you had data points at {(1.1, 3), (2, 4), (2.9,5)}, the x range would be set to [1.1:2.9].
Setting the yrange to GPVAL_DATA_Y_MIN:GPVAL_DATA_Y_MAX has the disadvantage of not using gnuplots autoscaling functionality which extends the ranges to the next tic.
In automatic plotting I therefore prefer the following
f(x)=sin(x)>0.5? 1:-1 #example function
set ytics 0.2
plot 1.01*f(x) # dummy plot to set GPVAL_*
set yrange [GPVAL_Y_MIN:GPVAL_Y_MAX]
plot f(x) # actual plot
This also works for data plots of course:
plot 'data.csv' u 1:(1.01*$2)
set yrange [GPVAL_Y_MIN:GPVAL_Y_MAX]
plot 'data.csv' u 1:2
I use it like this to define an x range for a funcion
plot [0:5] sin(10*x) + cos(3*x)
Also, you can set the range before ploting
set xrange [0:5]
plot sin(10*x) + cos(3*x)
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.