Scaling of multiplot while using variables to define the column - plot

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

Related

Gnuplot does not plot (blank) a contour map

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:

gnuplot - linecolour: do not wrap around

UPDATE
So apparently, the order of options does matter. Wasn't aware of that. Still,
rowi=1
rowf=7
colour=0
plot for [i=0:rowf-rowi+3] filename.'.csv' u ($0+i):2:3:(colour=colour+1):xtic(1) every ::i+1::i+1 w errorbars pt 7 lc var notitle
will start re-using colour after the eight entry where I want a different colour for each of the 10 points plotted.
How do I do that?
In gnuplot, the lc (linecolor) and pt (pointtype) parameters belong to the w (with) clause (see help plot with), so putting notitle between pt 7 and lc var doesn't work. If you move the notitle clause to the end of the plot command line, this will fix the error you are getting. Like this:
plot for [i=0:rowf-rowi+3] filename.'.csv' u ($0+i):2:3:xtic(1) every ::i+1::i+1 w errorbars pt 7 lc var notitle
Without lc var, by default the colours will eventually repeat but you can set the palette to anything you like. See: Gnuplot repeats colors in rowstack histograms

how to improve the following gnu plot?

I have the following gnu plot:
# automobile_input.txt
#
set term png
set output "automobile.png"
#
# Fields in each record are separated by commas.
#
set datafile separator ","
set title "Price versus Curb weight"
set xlabel "Price in dollars"
set ylabel "Curb weight in pounds"
set grid
plot 'x' using 1:2
quit
x is a file containing numbers such as
1,2
0.5,4
etc.
I would like to make a few changes to this plot.
At the top of the plot there is "x using 1:2" and I would like to remove that.
Finally, the most important thing: I would like to add another file, y, in the same format, which will be also plotted on the same plot, only with a different sign and color (instead of pluses in red), for example, blue triangles. I would rather also the pluses be circles.
Omit the data series title by using notitle in your plot line. Adding another curve would be done like this:
plot 'x' using 1:2 notitle, \
'y' using 1:2 notitle
The data series points will adjust automagically. To manually specify the format, you might plot with something like this:
plot 'x' using 1:2 with points pointtype 6 linecolor rgb 'red' title "Data X", \
'y' using 1:2 with points pointtype 8 linecolor rgb 'blue' title "Data Y"
You'll usually see scripts online that abbreviate these command to look like:
plot 'x' w p pt 6 lc rgb 'red' title "Data X", \
'y' w p pt 8 lc rgb 'blue' title "Data Y"

filling under step functions in gnuplot

I am using Gnuplot to draw step functions from an input file:
plot 'myFile' using 1:2 with steps
I want to fill underneath the plot. something like
plot 'myFile' using 1:2 with filledcurves
But Gnuplot fill underneath the diagram by drawing a line between consecutive points.
How can I fill in underneath the step function?
Use the fillsteps plotting style, which is the same as steps, but the area between the curve and y=0 is filled:
set samples 11
set xrange [0:10]
plot '+' with fillsteps fs solid 0.3 noborder lt 1,\
'+' with steps lt 1 lw 4

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