weird interpolation using filledcurves - gnuplot

I have a data file like this:
1 1 2
2 2 3
3 4 nan
4 5 6
I want to plot it using:
plot "bla" u 1:2:3 w filledcurves, "" u 1:2 w lp, "" u 1:3 w lp
The problem is that the first part totally ignores the 3rd line, even though the nan is only in $3. Even though I have a value (4) in $2, it interpolates and skips it.
How do I make it not ignore that value?
I can make a workaround by replacing the nan by the value that should be there- (3+6)/2 in my case and then it will plot the 4 as well. There are two problems with that - I'll have to write a script that finds nans around the file, and it also plots a point when I'm using w lp as if there is a value there, but there isn't.

You're asking gnuplot's using to do something that it wasn't designed to do. Fortunately, there's a somewhat hacky solution. You need to use the 2-column version of filled curves which considers your data-points to make a closed loop. In this case, you want to plot the stuff from columns 1 and 2 and then the stuff from columns 3 and 1 (in reverse order). e.g.:
1 1
2 2
3 4
4 5
4 6
3 nan
2 3
1 2
If your datafile looks like this, you can plot it as:
plot 'datafile' u 1:2 w filledcurves
Now I'm pretty sure you don't actually want to re-generate your data-files, so the easiest thing is to use unix tools to do it for you:
plot "< sed 'x;1!H;$!d;x' test.dat | awk '{print $1,$3}' | cat test.dat -" u 1:2 w filledcurves
This should work. Note that the ugly sed command can be replaced by tac if you have that installed.

Related

Combining yerrorbars and variable point size

I'm trying to plot the following data file
#x y s err
1 1 0.1 0.2
2 2 0.2 0.2
3 3 0.3 0.2
4 4 0.4 0.2
5 5 0.5 0.2
6 6 0.6 0.2
7 7 0.7 0.2
8 8 0.8 0.2
9 9 0.9 0.2
10 10 1.0 0.2
where the points have a variable size given by column 3 and the errors are given in column 4. I can get
plot "test" u 1:2:3 pt 7 ps variable
plot "test" u 1:2:4 w yerrorbars pt 7
to work independently, giving me this:
But when I try to combine them
plot "test" u 1:2:4:3 w yerrorbars pt 7 ps variable
I get something very strange:
yerrorbars seems to be using column 4 as the y column and column 3 as the yerror column. Even stranger, I get the same output if I try u 1:2:3:4. Is there something wrong with how I'm doing this? I can manually draw the errorbars as vectors, but I'd prefer to use the built-in errorbars style if possible.
gnuplot> help yerrorbars
The `yerrorbars` (or `errorbars`) style is only relevant to 2D data plots.
`yerrorbars` is like `points`, except that a vertical error bar is also drawn.
At each point (x,y), a line is drawn from (x,y-ydelta) to (x,y+ydelta) or
from (x,ylow) to (x,yhigh), depending on how many data columns are provided.
The appearance of the tic mark at the ends of the bar is controlled by
`set errorbars`.
2 columns: [implicit x] y ydelta
3 columns: x y ydelta
4 columns: x y ylow yhigh
An additional input column (4th or 5th) may be used to provide information
such as variable point color.
So in order to provide more than 3 columns and still use a single value for the ydelta, you should be able to do
plot "test" u 1:2:($2-$4):($2+$4):3 w yerrorbars pt 7 ps variable
However, as you point out this doesn't actually work as documented.
Work-around
An alternative is to make two passes; first plot the errorbar lines and suppress the points, second plot the point with the desired properties :
unset key
plot "test" u 1:2:3 with yerrorbars pt 0, \
"" u 1:2:4 with points pt 7 ps variable

gnuplot - printing multiple plots from one dataset in same file as script

Ive this script in gnuplot and I want to print multiple plots from 1 dataset. Ive tried this command but it seems that command needs another sama dataset to execute this command correctly. Do you know how to solve it?
plot '-' using 1:2, '=' using 1:3
1 1 5
2 2 5
3 3 5
e
With '-' you would have to enter the same data again. Check help special-filenames.
You better do:
$Data <<EOD
1 1 5
2 2 5
3 3 5
EOD
plot $Data u 1:2, '' u 1:3

gnuplot - how to plot invisible points so that the plot is only used to mark x-axis

I have the following data in data file test1.txt:
A 36 1
A 35 1
B 48 2
B 37 2
B 15 2
C 36 3
C 25 3
and test2.txt
A 16 1
A 25 1
B 38 2
B 45 2
C 36 3
the plotting is done as:
dat1="test1.txt"
dat2="test2.txt"
plot dat1 u ($3-0.2):2 w p t 'title 1', \
dat2 u ($3+0.2):2 w p t 'title 2', \
dat1 using 3:(0):xticlabels(1):x2ticlabels(1) notitle, \
dat2 using 3:(0):xticlabels(1):x2ticlabels(1) notitle
The whole point is to have the labels from the first column on the x axis. The issue is that the last two parts of plot
dat1 using 3:(0):xticlabels(1):x2ticlabels(1) notitle, \
dat2 using 3:(0):xticlabels(1):x2ticlabels(1) notitle
produce points at y=0. And because I cannot (or I do not know how to) use
plot dat1 u ($3-0.2):2:xticlabels(1) w p t 'title 1'
(since it plots the label at x coordinate $3-0.2 and not at $3, which i require), I am forced to do two more dummy plots, but they produce points and I do not know how to effectively avoid this other than shifting the outside of the range of the plot by e.g. dat1 using 3:(0):xticlabels(-1):x2ticlabels(-1) in the case of plotting only positive values. Is there any good way of solving this?
I don't really understand what you are trying to do with your data, however if you want to make the points over the x axis invisible you can set the line color to full transparency for the last two plots:
dat1 using 3:(0):xticlabels(1):x2ticlabels(1) linecolor "#FFFF0000" notitle, \
dat2 using 3:(0):xticlabels(1):x2ticlabels(1) linecolor "#FFFF0000" notitle
In recent versions of Gnuplot (link) it is possible to add transparency, in this particular case you want to set full transparency to the line color so that the points are invisible. The color format and transparency can be set using the hexadecimal notation #AARRGGBB where AA (alpha) controls how transparent the color is, for full transparency you want FF which is equivalent to 255, notice how my line color is RED but because is completely transparent we don't see it.
Plot with Full Transparency
My understanding of your question is the following: You have some data which you want to shift in x by +/-0.2 but the corresponding tic shouldn't be shifted.
You've been almost there. Of course, there are points at y=0 because you tell gnuplot to do so.
What you mean with "invisible" could of course be also achieved by fully transparent points, but the easier way is probably to "plot" the points at NaN, i.e. nowhere.
Code:
### plotting data with offset and tic without offset
reset session
set colorsequence classic
$Data1 <<EOD
A 36 1
A 35 1
B 48 2
B 37 2
B 15 2
C 36 3
C 25 3
EOD
$Data2 <<EOD
A 16 1
A 25 1
B 38 2
B 45 2
C 36 3
EOD
set offsets 0.5,0.5,0.5,0.5
set link x2
set x2tics
plot $Data1 u ($3-0.2):2 w p pt 7 t 'title 1', \
$Data2 u ($3+0.2):2 w p pt 7 t 'title 2', \
$Data1 using 3:(NaN):xtic(1):x2tic(1) notitle, \
$Data2 using 3:(NaN):xtic(1):x2tic(1) notitle
### end of code
Result:

gnuplot missing data with expression evaluation

I want to use the plot command in gnuplot with expression evaluation, i.e.
plot "-" using ($1):($2) with lines
1 10
2 20
3 ?
4 40
5 50
e
But I want it to ignore the missing data "?" in such a way that it connects the line (and doesn't break it between 2 and 4).
I tried set datafile missing "?",
but in agreement with the online-help it does not connect the lines. The following would, but I cannot use expression evaluation:
plot "-" using 1:2 with lines
1 10
2 20
3 ?
4 40
5 50
e
Any ideas how to connect the lines and use expression evaluation?
Two column data
If you set up a data file Data.csv
1 10
2 20
3 ?
4 40
5 50
you can plot your data with connected lines using
plot '<grep -v "?" Data.csv' u ($1):($2) w lp
More than two column data
For more than two columns you can make use of awk.
With a data file Data.csv
1 10 1
2 20 2
3 ? 3
4 40 ?
5 50 5
you can run a script over the data file for each plot like so:
plot "<awk '{if($2 != \"?\") print}' Data.csv" u ($1):($2) w lp, \
"<awk '{if($3 != \"?\") print}' Data.csv" u ($1):($3) w lp
A reference on scripting in gnuplot can be found here. The awk user manual here.

How do I indicate labels with different point types in Gnuplot?

Let's say I have data I'd like to be plotted in a file "animals.txt":
cat 5.2 1.0
cat 5.4 1.3
cat 5.2 1.2
dog 3.8 1.1
dog 3.5 1.5
dog 3.6 1.3
giraffe 1.3 9.7
giraffe 1.5 9.0
giraffe 1.4 9.9
I can generate a scatter plot with labels using:
plot "animals.txt" u 2:3:1 w labels
I can also vary the style of each point using something like:
plot "animals.txt" u 2:3 w points pointtype 3
Instead of using labels (which might overlap), is it possible to have the points use different point types or colors for each category? (For instance, "cat" would be in red using pointtype 3, "dog" would be in blue using pointtype 4, etc.)
I could use "lc variable" and replace the labels column with colors, but the file I'm working with has too many different labels for me to do that easily.
I don't think there is a way to do this directly.
However, an easy fix is to use gnuplot's interface to awk. You can then plot 3 separate graphs, one for each animal.
plot "<awk '{ if($1 == \"cat\") print $2,$3 }' animals.dat" u 1:2 w points title "cat", \
"<awk '{ if($1 == \"dog\") print $2,$3 }' animals.dat" u 1:2 w points title "dog", \
"<awk '{ if($1 == \"giraffe\") print $2,$3 }' animals.dat" u 1:2 w points title "giraffe"
Since the only difference between each line is the name of the animal, you could probably script this in a better way, but you get the idea,
Tom

Resources