How to plot a surface defined by parts in gnuplot - gnuplot

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.

Related

Can I plot 1D heatmap with gnuplot?

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

Gnuplot: variable linecolor for 3D-Vector plots

In 2D plots, it is no problem to use 4th column for supplying coloring of arrows in a vector-style plot:
set parametric
set trange [0:2*pi]
plot '+' using (cos($1)):(sin($1)):(0):(1):($1) w vectors lc palette
works as expected. However, if I pass to 3D-plots,
splot '+' using (cos($1)):(sin($1)):(0):(0):(1):(0):($1) w vectors lc palette
yields only black arrows, although the colorbox shows a realistic cbrange. Other colored 3D-plots such as
splot '+' using (cos($1)):(sin($1)):(0):(sprintf("%.2f", $1)):($1) w labels tc palette
also work as expected.
Question: Why is this?
*Gnuplot version: 5.0 patchlevel 3.

plotting two histograms, one w/ and one w/o error bars in one plot

I have the below script, which works fine when I have a third column in the second data set. Now I want to get the first histogram being drawn w/ error bars, and the second w/o. I can remove the :3 from the second plot command but gnuplot will complain about not enough data specified for the second histogram. If I remove set style histogram errorbars ... but that would disable the error bars on the first histogram, too. Is there a way to plot two histograms in the same figure, where one doesn't have error bars.
set xlabel ""
set ylabel ""
set boxwidth 0.9 absolute
set style fill solid 1.00 border -1
set style histogram errorbars gap 1
set style data histograms
set yrange [-1.746917959031165368e-01:3.668527713965446857e+00]
unset key
set datafile commentschar "#"
plot '-' using 2:3:xtic(1) title "onehist",\
'-' using 2:3:xtic(1) title "otherhist"
-3.583733737468719482e-01 1.073847990483045578e-02 1.073847990483045578e-02
-3.382162153720855713e-01 2.274234220385551453e-02 1.329828426241874695e-02
2.261839509010314941e-01 2.859487235546112061e-01 8.173441886901855469e-02
e
-1.164875924587249756e-01 4.266476333141326904e-01
-9.633044153451919556e-02 5.953223109245300293e-01
-7.617329061031341553e-02 6.151663661003112793e-01
-5.601614341139793396e-02 9.624376893043518066e-01
e
I'm not sure if it is possible to do this generally, but you can draw your histograms without the errorbars and then add them afterwards with an additional plot command.
plot '-' using 2:xtic(1) title 'onehist',\
'-' using ($0-0.2):2:3 with yerrorbars lc 'black' pt 0, \
'-' using 2:xtic(1) title 'otherhist',\
I'm not entirely sure how to determine the range of the actual bars, so the error bars are not perfectly centered, but this will place them on your graph as requested.
The additional command uses the yerrorbars style (which is how the histogram bars are drawn) to draw the error bars.
However, this isn't the best way to draw histograms. Gnuplot will treat the x-axis as a category with values 0, 1, 2, 3, etc. Therefore, even though you have different x values in both of your lists above, they will become superimposed over each other (and the second plot will change the x-axis values set by the first).
For your example, I would recommend using the boxes and boxerrorbars style.
set style fill solid
set boxwidth 0.01
plot '-' using 1:2:3 with boxerrorbars, '-' u 1:2 with boxes
or if you need the error bars to be a different color, draw them separately
plot '-' using 1:2 with boxes,\
'-' using 1:2:3 with yerrorbars lc 'black' pt 0,\
'-' u 1:2 with boxes

gnuplot: plot and splot matrix on the same graph

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):

gnuplot: max and min values in a range

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)

Resources