Samples, isosamples, and how they affect contour lines - gnuplot

Contour lines generated by gnuplot can look pretty strange (unexpected) if you do not set both samples and isosamples to appropriate values. I struggled for hours to find out how exactly set samples and set isosamples affect the appearance of contour lines, however, all I observed is that setting both to sufficiently large values will generate good-looking contours. Still, I want to understand how exactly this works.
What is the difference between set samples and set isosamples in the context of contour lines?
How does set samples affect the generation of contour lines?
How does set isosamples affect the generation of contour lines?
For example, consider the following simple case:
unset surface
set contour
set cntrparam levels discrete 10, 20
set samples 250, 2
set isosamples 2, 250
set view map
splot x**2 + y**2
To generate correct contour lines, it appears you need to set the first parameter of samples and the second parameter of isosamples to sufficiently large values. However, setting the second parameter of samples and the first parameter of isosamples to the smallest possible value does no harm. This is not exactly intuitive. So how does this work?

First, a discussion on what samples and isosamples where designed to do. This is best when viewing the actual plot and not the contour map.
samples is used to set the number of function evaluations along an axis in the range being plotted. For splot (3-D images), you can control samples in both independent directions x and y. Here is a sample where the x-direction only has 4 evaluations and the y-direction has 200:
reset
set xrange [-10:10]
set yrange [-10:10]
set xlabel 'X'
set ylabel 'Y'
set samples 4,200
splot x**2+y**2
In the following 2 images note that along the x-direction, the function is only evaluated 4 times and straight lines are drawn between them. Along the y-direction, it is evaluated 200 times and it looks like 'smooth' curves. Note that in both directions, exactly 10 lines are drawn. I will get to that below while discussing isolines.
Looking toward x-axis:
Looking toward y-axis:
So, 10 lines in each axis direction are drawn because the default value for isolines in both x and y directions is 10. We can change this at will. Lets increase samples in both directions for a nice smooth curves but demonstrate isosamples.
reset
set xrange [-10:10]
set yrange [-10:10]
set xlabel 'X'
set ylabel 'Y'
set samples 200,200
set isosamples 4,12
splot x**2+y**2
Note the nice smooth curves in both directions. Along the x-axis, there are only 4 points where, along the y-axis, the surface curves are drawn and along the y-axis, there are 12 points where surface curves are drawn parallel to the x-axis.
The above examples demonstrate the primary purpose of set samples and set isosamples. They only indirectly affect contours. In your case, you are only interested in the contour map without even displaying the surface plot. A clue as to how gnuplot plots contours is in its explanation of how contours can be drawn with discreet data.
Gromacs:
In order to draw contours, the data should be organized as "grid data". In
such a file all the points for a single y-isoline are listed, then all the
points for the next y-isoline, and so on. A single blank line (a line
containing no characters other than blank spaces and a carriage return and/or
a line feed) separates one y-isoline from the next.
From this explanation, gnuplot seems to prefer to pick an x value and draw a y-isoline. We can deduce then that when gnuplot draws contours from functions, that it picks x values and draws y-isolines. Therefore having many x samples and many y isolines draws hi resolution contours while the y samples and the x isolines are irrelevant (if you are not drawing the surface).

Related

Editing y axis range in Gnuplot

I have a plot with exponential y axis range. I'm using multiplot command by inserting two images in one row. So due to this wide y axis range I'm loosing some space which I could use it to show my plots in a better way. I want basically something like this
How could i do this? I think for doing this I have do some math operations in the y axis range. Also what is the most convenient command to insert ( xE-10) at top left of the plot.
reset
set terminal epslatex size 16cm,18cm color colortext
set output new.tex
set key off
set format $%g$
set title "sinx"
set ylabel "[kNm]"
plot 1000000*sin(x)
This is not my exact code but it looks similar to this. The plot I have presented is a part of the multiplot code and I use 7 input files with time series data of 300 seconds at a time step of 0.02. The point I want to edit the y axis range (use some mathtematical expressions) and also include the term ( xE-10 ) on the top of the plot something like this
You can manually add the exponent with a set label .... For instance, the following function takes large values within the given interval:
plot[0:50] exp(x)
We can place the "x 10^21" manually in the desired place after dividing the plotted quantity by it:
set label 1 "{/Symbol \264} 10^{21}" at graph 0,1.025 left
plot[0:50] exp(x)/1e21
You have to be careful with the exact placement of the exponent since it might lie outside the plotting area, in which case you should lower the top margin with set tmargin .... Also, to use the "times" symbol, you need to pass the enhanced option to your terminal. With the epslatex terminal, you can use latex syntax: $\times 10^{21}$.

How to properly handle 3D data and create heatmaps in gnuplot 4.6?

I have temperature data from eight different nonequidistant depths over several days and I would like to plot it as heatmap. As far as I know I have two options. I can use "with image" but that assumes equidistant points so that makes just 8 pixel thin strip or I can use pm3d which need proper grid(I am not sure what proper grid is). I found that dgrid3d can make the grid for me but that erases any dependency on depth for some reason. Can someone please explain this behavior to me?
Here is the code and the data I use.
https://docs.google.com/file/d/0B2TfUgjtGRVReV9ocFVvM1BKd2M
set timefmt "%Y-%m-%d %H:%M:%S"
set xdata time
set view map
set dgrid3d
splot './Data/MO-03t_.txt' every ::2 u 1:3:4 with pm3d
the dgrid3d does not plot your data. Instead, something like two-dimensional splines are calculated from your data. Then, gnuplot creates a (by default) 10x10 matrix, and fills it with values interpolated from the splines. This matrix is then plotted.
But I have an other solution for you:
set timefmt "%Y-%m-%d %H:%M:%S"
set xdata time
# for the nice colors
set pm3d
# For debugging: place ticmarks only vor values with data
set ytics (0.02, 0.04, 0.06, 0.11, 0.31, 0.41, 0.61, 0.91)
plot './Data/MO-03t_.txt' every ::2 u 1:3:(1800):($3<0.2?.005:0.05):4 w boxxyerrorbars fs solid lc palette
The boxxerrorbars ... lc palette takes using <x>:<y>:<deltaX><deltaX>:<z>
The delta values are half the width of the boxes, i.e. it means plot a box from x-deltaX;y-deltaY to x+deltaX;y+deltaY for each datapoint.
Here, the width of the boxes is 3600 seconds, the height is either 0.1 or 0.01, depending on the y-value.
Here is the result:
However, you can still improve the height of the boxes.
And instead of 4+1 columns, you can also pass 6+1 columns with
using <x>:<y>:<xlow>:<ylow>:<xhigh>:<yhigh>:<z>
Here, the borders of the boxes are given as absolute values, not as distance from the x-y-value.

Column and row name formatting in gnuplot

I prepared a code to graph my script's csv output. They are column separated values.
I used code below to do that:
#!/usr/bin/gnuplot -persist
set datafile separator ":"
set terminal png nocrop font small size 640,480
set output 'mychart.png'
set style data histograms
set title "some graph title"
set xlabel "some x asis name"
set ylabel "some ya axis name"
plot "/home/username/Desktop/test.csv" using 2:xticlabels(1) notitle
This will draw the graph successfully. However I have two problems here. One in x axis, there are big numerical values such as 50000000, 6000000 so graph shows them in exponential format such as 5E+9 6E+8. But I would like to see full number. Second, in y axis, there are almost 20 alphanumeric values (some network parameter names) and each of them with at least 25-30 character. So they won't show up properly. Is there a way to write them with a smaller font and an angle?
Thanks
To change the format of the numbers shown on the y-axis, use e.g.
set format y '%.0f'
The labels of the xtics can be rotated (rotate by 45), aligned differently (right) and they can use a different font size (font), e.g.
set xtics right rotate by 45 font ',8'
See also rotating and justifying tics in gnuplot.
You may need to adjust the bottom border to have enough space for the labels, e.g. with
set bmargin 6
BTW: You should use the pngcairo terminal (available since version 4.4), which produced much nicer graphs than the png terminal.

Creating a microphone polar pattern plot in gnuplot

I would like to create a microphone polar pattern plot that has a scale of -20 (in the center) out to +5 in steps of 5. I have found similar code but nothing that allows for the scales to be negative.
Multiple patterns will then need to added to the plot covering a few different frequencies, I have degree values (0-360) and corresponding dB values (-25 - +5).
This is what the plot should look like (though with slightly different scales):
The closest gnuplot I have found to this is here: How to get a radial(polar) plot using gnu plot?
Perhaps this could be modified to suit my needs?
I would also like 0 degrees to be found at the top of the plot rather than on the right.
I am new to using gnuplot so I am not particularly familiar with its code, therefore it has been difficult for me to modify the code with any great success (so far anyway).
So you want to plot a polar function, e.g. r(theta) = 1 + sin(theta).
Plotting the function is quite easy, just do
set polar
plot 1+sin(t)
A simple polar grid can be plotted with
set grid polar
but that has the raxis and the rtics on a different position than where you wanted. It is not problem to specify custom labels. But angular labels aren't supported, so you need to set them manually. And the border and the other axes and tics must be unset.
To get the very same image as you showed, use the following script:
set terminal pngcairo size 700,600 font ',10'
set output 'cardioid.png'
set angle degree
set polar
set size ratio 1
set tmargin 3
set bmargin 3
set style line 11 lc rgb 'gray80' lt -1
set grid polar ls 11
unset border
unset xtics
unset ytics
r=1
set rrange [0:r]
set rtics 0.166 format '' scale 0
set label '0°' center at first 0, first r*1.05
set label '180°' center at first 0, first -r*1.05
set label '90°' right at first -r*1.05, 0
set label '270°' left at first r*1.05, 0
set for [i=1:5] label at first r*0.02, first r*((i/6.0) + 0.03) sprintf("%d dB", -30+(i*5))
unset raxis
plot 0.5*(1+sin(t)) linewidth 2 t ''
With the result:
That includes some offsets for the labels, which depend on the terminal, the canvas size and the font size. So you may need to adapt them.
I had to increase the top and bottom margins a bit (here by 3 character heights) in order to have enough space for the angular labels. They aren't included in the automatic margin calculations, because the don't belong to an axis.
Unfortunately Christoph's answer is wrong.
You can see that if you check where the plot curve crosses the 5db circle.
What should be plotted is
20*log10(A+B*cos(t))
where A+B = 1 and A - B determines the (nominal) directivity pattern.
The first diagram seems to be for A=B=0.5 which makes for a cardioid pattern.

Gnuplot fat lines overlap with axis

Heyho!
I need to make some lines with gnuplot. However due to the small size of the plots, I want to make those lines fat. However they overlap now with the axis. and into the normal picture. How can I cut those lines of so they dont do it?
Thanks!
If you are plotting data, you can adjust the overall x and y ranges so that the lines don't overlap with the borders:
set xrange [xmin:xmax]
set yrange [ymin:ymax]
If you are plotting functions, you can also cut off lines by adjusting the range when you plot:
plot [xmin:xmax][ymin:ymax] ...
This is a little more tricky because the function will automatically go to the edge of the plot anyway. If this is the case, you can specify a larger range for a blank line:
plot [-2:2][-2:2] NaN notitle, [-1:1] x**2 title 'x^2'

Resources