Gnuplot does not plot (blank) a contour map - 2d

I am new to gnuplot and trying to plot a contour plot with xyz data which is in text file.
I have tried many different methods but it gives only blank plot.
DATA is in Google Drive : https://drive.google.com/open?id=1x-NAD9Vs8wyv9QbDgjcaT9SujHlHChAd
set contour base
set pm3d
unset surface
set view map
set xrange [1000:4000]
set yrange [0.2:0.395]
set zrange [0:40000]
splot "relax.txt" using 1:2:3
Followed error message:
Warning: Single isoline (scan) is not enough for a pm3d plot.
Hint: Missing blank lines in the data file? See 'help pm3d' and FAQ.

The problem is that gnuplot requires an empty line after each (matrix) row (e.g. when column 1 value changes).
So you can insert the empty lines yourself manually or with an external tool or let gnuplot do this. You plot your data into a dummy-table (a datablock) and then you print it to another datablock and insert an empty line when the value of column 1 changes. A bit cumbersome but it works. Requires gnuplot >=5.2.
Code:
### contour plot, addding empty lines to raw data
reset session
set contour base
set pm3d
set cntrparam level 10
unset surface
set view map
set size 0.9,0.9 # shrink the size a little otherwise colorbar numbers will be outside canvas
### insert empty lines everytime when column 1 changes its value
set table $Dummy # initialize a table (datablock) named $Dummy
plot "relax.txt" u 1:2:3 with table # plot datafile into a datablock $Dummy
unset table # close the table
set print $Data # set print output to datablock $Data
check="" # initialize your comparison variable to empty string
do for [i=1:|$Dummy|] { # loop the datablock $Dummy by lines
if (check ne word($Dummy[i],1)) { print "\n" } # comparison: if values are not equal insert a line
print $Dummy[i] # print current line to datablock $Data
check = word($Dummy[i],1) # assign latest value to variable "check"
}
set print # close the datablock $Data
splot $Data u 1:2:3 w l notitle
### end of code
Result:

Related

How to get the values of the axes of plot image without x/y tics using GNUPlot?

I want to make a PNG image of my plot but would like to overlay this on a map. To do this we need the boundary coordinates and a simple png without any axes or tic marks ending up in the image. I tried turning of the ticmarks by unset xtics / ytics but then the value of GPVAL_X_MAX becomes the same as GPVAL_DATA_X_MAX. Is there any way to find the min/max values of the plot without the using the tics?
Example:
plot [-10:10] sin(x),atan(x),cos(atan(x));
show variables all;
Gives GPVAL_Y_MIN = -1.50, GPVAL_Y_MIN=-1.47...
Whereas:
unset xtics;
unset ytics;
plot [-10:10] sin(x),atan(x),cos(atan(x));
show variables all;
gives GPVAL_Y_MIN = -1.47, GPVAL_Y_MIN=-1.47...
edit:
Fixed, I finally just removed all style/layout of the tics and scaled their size down to zero. This way GPVAL_X_MIN etc retain the value of the actual boundaries instead of the min/max vals of the dataset.
A simple brute force solution would be to plot twice. The first time with tics enabled, just to store the value of the variables you're after, such as GPVAL_DATA_Y_MIN. You can then plot a second time with tics disabled.
set terminal pngcairo
set output 'plot.png'
plot [-10:10] sin(x),atan(x),cos(atan(x));
y_min = GPVAL_Y_MIN
data_y_min = GPVAL_DATA_Y_MIN
unset xtics
unset ytics
set output 'plot.png'
replot
print sprintf("GPVAL_Y_MIN = %f", y_min)
print sprintf("GPVAL_DATA_Y_MIN = %f", data_y_min)
Output:
GPVAL_Y_MIN = -1.500000
GPVAL_DATA_Y_MIN = -1.471128

How to plot an histogram and a constant line from different files

I'm new to gnuplot and I have a problem:
I have 2 different text files, the first one (file1.txt) is something like this:
Switch,Port,BPS
S1,1,5464091.33
S1,3,5465677.33
S2,2,5463298.00
S2,3,5462729.67
S3,1,5461340.67
S3,3,5461772.33
and I plot "file1.txt" with an histogram:
plot avg_file using 3:xticlabels(stringcolumn(1)."-".stringcolumn(2)) title columnheader
this works fine.
Now I have file2.txt which contains a single value:
AVG_BPS
4844714.81
I would like to plot this value as a constant horizontal line over the previous graph (histogram).
This is my complete script:
# Terminal definition (PNG image)
set term png
# Settings
set datafile separator ","
set output 'myplot.png'
set style data histogram
set style histogram cluster gap 1
set style fill solid border -1
set boxwidth 1
# Graph and Axis titles
set title "BPS"
set xlabel "Switch-Port"
set ylabel "bits-per-second"
# Plot
plot "file1.txt" using 3:xticlabels(stringcolumn(1)."-".stringcolumn(2)) title columnheader
Here is myplot.png:
Thanks
probably the best is to do a stats:
stats 'file2.txt' u 1
hor_value=STATS_min
and then add to your plot:
plot "file1.txt" using 3:xticlabels(stringcolumn(1)."-".stringcolumn(2)) title columnheader, hor_value
or alternatively put a line on top (before the last plot):
set arrow nohead from graph 0, hor_value to graph 1, hor_value front

Plotting multiple plots on same page using gnuplot; trying to avoid text on the plot

I am trying to plot multiple plots on the same page of gnuplot postscript output. Although, I like the way the plots are looking, what can I do to avoid the text on the plots? (See the code below, and the plot too). If you cannnot see the text on the plot, it says: "'../binsam_Omidi.dat' using 5:($1==1?$6:1/0)"
reset
set term postscript eps color size 10,10
set output "../Results/phasespace_bins.ps"
set multiplot layout 4,5 # engage multiplot mode
set size square
set size ratio -1
plot '../binsam_Omidi.dat' using 5:($1==1?$6:1/0) w p ps 1 pt 7
set size 0.4,0.4 # set the size of the second plot in plot units
set origin 0.15,0.5 # set the origin for the second plot
plot '../binsam_Omidi.dat' using 5:($1==1?$6:1/0) w p ps 1 pt 7
unset multiplot
Thanks!
That is the legend (key) entry of the plot. You can specify your own title with the title option, like
plot x title 'my title'
If you don't specify your own title, the code of the plot command is used. This is what you see.
To have no key entry for a single plot use notitle or title ''. If you don't want to have any key at all use unset key.

Scaling of multiplot while using variables to define the column

I am getting data from a script stuff.pl and want to plot dynamically into one graph. So I am going to use a loop in Gnuplot (v4.6 patchlevel 3) which leads me to following problem:
Using the file TEST.gp:
xCol=2; yCol=3
set term x11 1
plot '< stuff.pl' u xCol:yCol
xCol=4; yCol=5
set term x11 1
set autoscale y
replot '< stuff.pl' u xCol:yCol
pause 1
and run it via gnuplot TEST.gp my graph isn't scaled proper. The plot is just showing the second graph (scales to its values).
If I use
plot '< stuff.pl' u 2:3
replot '< stuff.pl' u 4:5
, which should behave the same imo, the scaling works.
I am not understanding this behaviour.
The replot calls the previous plot command and then adds another plot. In the previous plot command the variables haven't been replaced. When the replot calls the previous plot command, the last values of xCol and yCol are used for both plots!
You can either use two different variables:
xCol1 = 2; yCol1 = 3
plot '< stuff.pl' u xCol1:yCol1
xCol2 = 4; yCol2 = 5
replot '< stuff.pl' u xCol2:yCol2
or you can use macros, which are replaced
set macros
cols='2:3'
plot '< stuff.pl' u #cols
cols='4:5'
replot '< stuff.pl' u #cols

gnuplot smooth frequency with conditional formatting

Creating a normiles stack diagram works perfectly fine, now I want to use a different color for the boxes fitting in a specific range.
For this, I adopted the following:
set palette maxcolors 2
set palette defined ( 0 '#C0504D', 1 '#00B059')
plot dataFileCity using (rounded(stringcolumn(1) eq city ? $2 : NaN)):
(100 / (bin_width * STATS_records)):($2 > 1300 ? 0 : 1)
smooth frequency with boxes palette
If column 2 has a value highter than 1300 I would like to have a different color.
Which is based on:
Normalized histograms in gnuplot with added function plot
And
Color bars in different colors for some specific values in Gnuplot
However, I am afriad that the smooth frequency makes the thing not work. How can I pass the value such that is creates the a different color?
I know this is nearly 8 years old, but I had the same issue and based on Christoph's comment above I was able to figure out a way.
Below is the graph I wanted:
However selecting certain rows only by way of the ternary and NaN does not play nice with smooth freq, and my histogram was wrong (seemed bins were drawn over one another and frequencies weren't as high as they should've been).
This did not work:
plot \
'filename' using ($1 >= 0 ? $1 : NaN) notitle smooth freq with boxes fillcolor rgb "green", \
'filename' using ($1 < 0 ? $1 : NaN) notitle smooth freq with boxes fillcolor rgb "red"
In the manual for gnuplot 5.4.2, this section describes an experimental feature which, combined with set table, allowed me to achieve the above graph.
[EXPERIMENTAL] To select only a subset of the data points for tabulation you can provide an input filter condition (if ) at the end of the command. Note that the input filter may reference data columns that are not part of the output. This feature may change substantially before appearing in a released version of gnuplot.
plot <file> using 1:2:($4+$5) with table if (strcol(3) eq "Red")
-- p207 gnuplot v5.4.2 manual
So the approach is:
Use set table $my_data_block_green to set the next plot command to output to the $my_data_block_green data block. We'll create one data block for each colour and this is the first.
Use plot <file> with table if (<condition_for_green>) to write to the green data block only rows matching <condition_for_green>.
Use set table $my_data_block_red (as in point 1).
Use plot <file> with table if (<condition_for_red>) to write to the red data block only rows matching <condition_for_red>.
Cease writing plot commands to tables with unset table.
Plot as normal, referencing the data blocks instead of <file>.
Relevant code (not the full code for graph above):
set table $db1
plot <filename> using 7:(1) with table if ($7 >= 0)
set table $db2
plot <filename> using 7:(1) with table if ($7 < 0)
unset table
plot \
'$db1' using $1:.. .. fillcolor rgb "green", \
'$db2' using $1:.. .. fillcolor rgb "red"
Hope that saves someone a few mins.
Adding to #TKF's answer... There is no need to split the smooth freq data into two tables.
Instead, plot it into one table and set the color via lc rgb variable and by defining an appropriate function.
The following example works for gnuplot>=5.2, with some modifications also with earlier versions.
Code:
### histogram with split colors
reset session
# create some random test data
set print $Data
do for [i=1:2000] {
print sprintf("%g",int(invnorm(rand(0))*100))
}
set print
stats $Data u 1 nooutput
xmin = STATS_min
xmax = STATS_max
N = 20
myWidth = (xmax-xmin)/N
bin(col) = myWidth*floor(column(col)/myWidth)+myWidth/2.
set boxwidth myWidth
set key noautotitle
set style fill solid 0.3
set grid x,y
set table $Histo
plot $Data u (bin(1)) smooth freq
unset table
myColor(col) = column(col)<0 ? 0xff0000 : 0x00cc00
plot $Histo u 1:2:(myColor(1)) w boxes lc rgb var
### end of code
Result:

Resources