xtics label with conditions using gnuplot - plot

I am interested in labeling the xtics with strings from a column in a file.
The file is written in the following manner:
Index Name Status Value
1 Ver1 with 0.3
2 Ver1 without 0.25
3 Ver2 with 0.35
4 Ver3 with 0.27
The datas shall be plotted with a conditioned plot
plot file u (strcol(3) eq "with"?$1:1/0):($4) w p pt 7 notitle
The xtics shall be labeled with the data contained in column(2). If all values are used this can be done by xticlabel(2). But I only want to use the filtered data to get a plot like:
|
| x x x
|
----------------------------
ver1 ver2 ver3
The questions is: How can I label the xtics using only the filtered values?
Thanks in advance!

You could impose the same condition in xticlabel as well, i.e., to use column 2 if required or pass an undefined value instead:
plot 'file.dat' u \
(strcol(3) eq "with"?$1:1/0):4:xticlabel((strcol(3) eq "with")?strcol(2):1/0) \
w p pt 7 notitle

Related

Using gnuplot to plot points with color and x-marks

I have been trying this for a few hours but I am not getting anywhere.
What I am trying to do is this:
I have a solution from a simulation with x and y values and a value for each point.
I am trying to plot the data using gnuplot. I want the values in between my data points to be interpolated using color and the points themself shall be marked with a dot, a "x" or sth. like that similar to this (except for the round border and those labels inside):
I have been trying to get a very basic example going. My data file looks like this:
1 1 0.1
1 2 0.3
1 3 0.6
2 1 0.5
2 2 0.7
2 3 0.9
3 1 0.2
3 2 0.8
3 3 0.7
and my gnuplot input like this:
set terminal postscript eps enhanced color font 'Helvetica,10'
set output './production/image1.eps'
set palette gray
set title "Titel"
#set xrange [1:4]
#set yrange [0:10]
set format y "%.1f"
set format x "%.1f"
set xlabel "x-Achse [Einheit]"
set ylabel "y-Achse [Einheit]" rotate by 90
set view map
set pm3d at b map
set pm3d interpolate 2,2
set dgrid3d 50,50,2
splot "inputDatei.dat" u 1:2:3 linecolor palette
The result looks like this:
There are a few issues with this which i cannot resolve:
there is a label in the rip right "inputDatai.dat" u 1:2:3". I tried splot ... label "" but this didnt solve the issue
the interpolation doesnt seem to work. this is visible with a smaller grid
the data points are not highlighted. I tried using splot ... with points but this would only display points at EVERY grid corner which is obviously way too much. Also the input data might not be "regular" but points can be anywhere.
I am very happy if someone could help me with this.
Greetings,
Finn
To your questions:
use unset key before the plot command or splot ... notitle within the plot command
what "does not work" mean? Please explain.
I guess that's similar to here (Superimposing vectors, dgrid3d and pm3d in gnuplot for 3D plot). You have to switch off interpolating for the highlighted points. I'm not sure whether this can be done within a plot command, so you have to plot the interpolated data into a separate datablock via set table and then switch off interpolation.
Code:
### interpolate data with highlighted datapoints
reset session
$Data <<EOD
1 1 0.1
1 2 0.3
1 3 0.6
2 1 0.5
2 2 0.7
2 3 0.9
2.5 2.5 0.1
3 1 0.2
3 2 0.8
3 3 0.7
EOD
set size square
set view map
set pm3d at b
set pm3d interpolate 2,2
set dgrid3d 50,50,2
set table $DataInterpolated
splot $Data u 1:2:3
unset table
unset dgrid3d
set palette grey
set xrange [0.9:3.1]
set yrange [0.9:3.1]
splot $DataInterpolated u 1:2:3 w pm3d palette notitle, \
$Data u 1:2:3 w p pt 1 lw 2 lc rgb "red" notitle
### end of code
Result:

Line plot in GnuPlot where line width is a third column in my data file?

I have a datafile that has three columns :
1 1.0 1
2 1.5 2
3 0.0 3
4 1.2 2.5
5 1.0 1
6 1.1 5
where the first column is my X value, the second column is my Y value, and the third column is the line width. I'd like for each line segment to be plotted according to the third column line width.
I tried:
plot 'file1.dat' using 1:2:3 with lines lw var
But I get undefined variable: var error.
Is this possible in gnuplot?
Thanks.
If you define column 3 as the linewidth between points n and n+1 (so the value of col. 3 of the row will be ignored) you can cheat:
stat 'file1.dat'
n=STATS_records
plot for [i=0:0] 'file1.dat' using 1:2:(alma=$3) every ::i::i w l lc 1 lw 1
plot for [i=0:n-1] 'file1.dat' using 1:2:(alma=$3) every ::i::i+1 w l lc 1 lw alma notitle
OR
plot 'file1.dat' u 0:1
n=GPVAL_DATA_X_MAX
plot for [i=0:0] 'file1.dat' using 1:2:(alma=$3) every ::i::i w l lc 1 lw 1
plot for [i=0:n] 'file1.dat' using 1:2:(alma=$3) every ::i::i+1 w l lc 1 lw alma notitle
You need the first plot for[i=0:0] to 'initialize' variable 'alma'.
stat 'varwidth.dat' nooutput
n=STATS_records; prex=0; prey=0; SC=2 # How y-axis is expanded relative to the x-axis
plot for [i=0:n-1] for [try=0:1] '' using 1:((try==0?dx=$1-prex:1),(try==0?sl=($2-prey)/(dx==0?1:dx):1),(try==0?prex=$1:1),(try==0?prey=$2:1),$2+(w=$3/80*sqrt(1+(SC*sl)**2))/2):($2-w/2) every ::i::i+1 w filledcurves lc 1 notitle
This produces the correct line width (as opposed to the “line height”, as in the answer to a related question). I have no clue how to make the “lines” match where they join (seems to be impossible without support from inside gnuplot).
(This assumes that the data in the question is in a file varwidth.dat.)
As mentioned by #Christoph in the comments, you have N points (rows) and N linewidth values, but only N-1 connecting lines.
So, you have to decide which linewidth value should be applied to which line. Which one to skip? The first one or the last one? Here, the last one is skipped.
It's basically, a shortened version of #Tom Solid's solution. Actually, you can get the initial value already during the stats command.
As #Joce mentioned and #Ilya Zakharevich is suggesting in his answer, if you do not fear some extra effort you can also draw tapered lines.
Data:
# x y linewidth
1 1.0 1
2 1.5 2
3 0.0 3
4 1.2 2.5
5 1.0 1
6 1.1 5
Script: (works with gnuplot 4.6.0, March 2012)
### workaround for variable linewidth
reset
FILE = "SO/SO37925489.dat"
stats FILE u ((N=$0)==0?Lw0=$3:0) nooutput
set grid
plot for [i=0:N] FILE u 1:2:(Lw0=$3) every ::i::i+1 w l lw Lw0 lc rgb "red" notitle
### end of script
Result:

take the last data column with AWK to create a linepoints gnuplot

I am working on gnuplot linepoints to create a comulative and normal distribution graph. I have created a file to provide the information to both graphs.
I got a problem when I was trying to plot the last data.
Here is the my script to create the second graph.
plot.plt
set term pos eps
set style data linespoints
set style line 1 lc 8 lt -1
set size 1,1
set yr [0:20]
set key below
set grid
set output 'output.eps'
plot "<awk '{i=i+$3; print $1,i}' data.dat" smooth cumulative t 'twitter' ls 1
data.dat
5.0 1 0.10
9.0 5 0.20
13.0 7 0.30
14.0 1 0.20
15.0 9 0.20
I want to create x axis with the first column and y axis with the last column. so the y axis range must between 0 to 1. which part should I change? thanks
Using smooth cumulative is enough, no need for awk. You are doing the same operation twice, once with gnuplot and once with awk. Simply do
plot 'data.dat' using 1:3 smooth cumulative

3-Dimensional Plot in GnuPlot where color is a fourth column in my data file?

I have a datafile that looks like this:
1 2 3 0.5
2 8 9 0.2
3 4 78 0.4
6 5 7 0.01
9 9 9 0.3
10 12 18 0.9
6 8 4 1
I would like to do a graph like this
http://2.bp.blogspot.com/-378_rAaSSVU/UzU0gnGcr9I/AAAAAAAABnU/P1GwP9RKBkM/s1600/gnuplot.png
Where the 4th column is the color.
I tried - obviously incorrect because I do not use the fourth column but I failed to find anything in the documentation:
set dgrid3d 30,30
set view 60,45
set hidden3d
dataFile='prova.dat'
set palette defined (0 "blue", 0.5 "white", 1 "pink")
set pm3d
splot dataFile u 1:2:3 with pm3d
Is somethings like that possible?
Using only pm3d you can use a fourth column to select a color independent of the z-value. Together with dgrid3d this is not directly possible, because the gridding is not performed on the color column.
You can use a workaround: First you plot the gridded z-value to one file, then the gridded color values to a second file and as last point you disable dgrid3d, merge the two temporary files on-the-fly and plot their values:
set dgrid3d 30,30
dataFile='prova.dat'
set table dataFile.'.grid'
splot dataFile u 1:2:3
unset table
set table dataFile.'.color'
splot dataFile u 1:2:4
unset table
set view 60,45
set hidden3d
set palette defined (0 "blue", 0.5 "white", 1 "pink")
set autoscale cbfix
set pm3d
unset dgrid3d
set ticslevel 0
splot sprintf('< paste %s.grid %s.color', dataFile, dataFile) u 1:2:3:7 with pm3d notitle
Note, that paste is a command line tool for Unix-like operation systems. For a similar solution for windows, you can e.g. write a small Python script paste.py (see my answer to Get ratio from 2 files in gnuplot for a possible implementation). Then you must run the wgnuplot_pipes.exe binary file and the splot command becomes
splot sprintf('< python paste.py %s.grid %s.color', dataFile, dataFile) u 1:2:3:7 with pm3d notitle
Of course, for this you must have python installed and the python binary must be available via the PATH environment variable.

gnuplot: how to plot sum of three data sets

I have a data in file which I would like to plot using gnuplot. In the file, there are 3 data sets separated by two blank lines so that gnuplot can differentiate between the data sets by 'index'. I can plot three data sets separately via 'index' option of 'plot' command.
However, I am not sure how can I plot the data which is sum of 2nd column of all three data sets?
Note: all three data sets have same x data, i.e. 1st column
To do this the simplest thing would be to change your file format. Gnuplot manipulates columns pretty well. Since you are sharing the x data, you can change the file format to have four columns (assuming you are just plotting (x,y) data):
<x data> <y1 data> <y2 data> <y3 data>
and use a command like
plot 'data.dat' using 1:2 title 'data 1', \
'' u 1:3 t 'data 2', \
'' u 1:4 t 'data 3', \
'' u 1:($2+$3+$4) t 'sum of datas'
The dollar signs inside the parens in the using column specification allow you to add/subtract/perform other functions on columnar data.
This way your data file will also be smaller since you won't repeat the x data.
#Youjun Hu, never say that there is "no way" to do something with gnuplot. Most of the cases there is a way with gnuplot only, sometimes maybe not obvious or sometimes a bit cumbersome.
Data: SO16861334.dat
1 11
2 12
3 13
4 14
1 21
2 22
3 23
4 24
1 31
2 32
3 33
4 34
Code 1: (works with gnuplot 4.6.0, needs some adaptions for >=4.6.5)
In gnuplot 4.6.0 (version at the time of OP's question) there were no datablocks and no plot ... with table. The example below only works for 3 subdatasets, but could be adapted for other numbers. However, arbitrary large number of subdatasets will be difficult with this approach.
### calculate sum from 3 different (sub)datasets, gnuplot 4.6.0
reset
FILE = "SO16861334.dat"
stats FILE u 0 nooutput
N = int(STATS_records/STATS_blocks) # get number of lines per subblock
set table FILE."2"
plot FILE u 1:2
set table FILE."3"
x1=x2=y1=y2=NaN
myValueX(col) = (x0=x1,x1=x2,x2=column(col), r=int($0-2)/N, r<1 ? x0 : r<2 ? x1 : x2)
myValueY(col) = (y0=y1,y1=y2,y2=column(col), r<1 ? y0 : r<2 ? y1 : y2)
plot FILE."2" u (myValueX(1)):(myValueY(2))
unset table
set key top left
set offset graph 0.1, graph 0.1, graph 0.2, graph 0.1
plot for [i=0:2] FILE u 1:2 index i w lp pt 7 lc i+1 ti sprintf("index %d",i), \
FILE."3" u 1:2 every ::2 smooth freq w lp pt 7 lc rgb "magenta" ti "sum"
### end of code
Code 2: (works with gnuplot>=5.0.0)
This code works with arbitrary number of subdatasets.
### calculate sum from 3 different (sub)datasets, gnuplot>=5.0.0
reset
FILE = "SO16861334.dat"
set table $Data2
plot FILE u 1:2 w table
unset table
set key top left
set offset graph 0.1, graph 0.1, graph 0.2, graph 0.1
set colorsequence classic
plot for [i=0:2] FILE u 1:2 index i w lp pt 7 lc i+1 ti sprintf("index %d",i), \
$Data2 u 1:2 smooth freq w lp pt 7 lc rgb "magenta" ti "sum"
### end of code
Result: (same result for Code1 with gnuplot 4.6.0 and Code2 for gnuplot 5.0.0)

Resources