How to add custom label to Gnuplot graph legend? - gnuplot

In a graph I'm making with gnuplot I draw some grey lines (set arrow command), which represent the physical boundaries of my experiment (i.e., walls)
I would like to know how I can add this information on the legend of the graph, so it says "Walls" and have a grey line next to it.
I thought about creating a new series that contained this information, but I was wondering if it's possible to explicitly add it.

You can't add information directly to the legend. You can, however, either draw the legend explicitly, or plot a line which will not appear within the range of the plot, e.g.
plot [][0:1] 2 lc rgb 'gray' t 'Walls'
Or, if your x and y limits are already set:
...
[set x and y limits here]
...
plot 1e20 lc rgb 'gray' t 'Walls'

Just wanted to note: since plotting a single line tended to mess up a graph of mine, a better solution for me was to plot a single point; but as found in Plotting single points « Gnuplotting, that is kinda difficult (especially if insertion at arbitrary plot legend/key position is needed) - unless redirection is used... This is what worked for me:
plot "filename" using 1:8 \
,\
... # more plot lines here
,\
"<echo '-1 -1'" lc rgb 'white' with points title '---' \
,\
... # more plot lines here

One simple way is to make the name of the data file the legend which you want and then plot that data file.

Related

How to show a grid with two filled plots

I am plotting to datasets with 'fillsteps' one below another and I want the plot two show only the area that is a difference between the two
plot [0:1][0:1] x with fillsteps above fill solid not,x**2 with fillsteps above fill solid lc rgb 'black' not
But the grid obviously gets blocked in this case:
Is there any way to create something like a cross-section between the two areas, show the grid and get rid of those nasty artifacts that are seen below?
Ok, basically you want to fill the area between two curves (either lines or steps) and have a grid on top. The set grid front you found yourself, but let me make another suggestion.
For the first case (lines), i.e. a fill between lines, you can simply use 3 columns (check help filledcurves) and then the area will be filled between the curves:
plot '+' u 1:(f1(x)):(f2(x)) w filledcurves
For the second case (steps), I don't see (yet) such an option with filledsteps. Actually, from your option above I assume you are using gnuplot5.5.
In general, I wouldn't call it a "clean" solution if you plot something and partly have to cover it with something else with background color. What if you want a transparent background? A transparent color which covers something colored has not yet been invented ;-), there is no such "invisible" color. For another example check this.
Furthermore, with fillsteps I can also observe the artifacts of vertical gap lines which you see in your graph, but I don't have a good solution to avoid them.
Hence, my suggestion is to plot only there where you need something to plot. Actually, you can mimic the fillsteps behaviour. It's not obvious how to do it, but not too difficult. While you plot line by line, you remember the previous x-value and function values of f1(x0) and f2(x0) in x0, y0 and y2, respectively. You plot with the plotting style boxxyerror (check help boxxyerror) using x:y:xlow:xhigh:ylow:yhigh.
Script: (works with at least gnuplot>=5.0.0)
### plotting style filledcurves and mimic fillsteps "between"
reset session
f1(x) = x
f2(x) = x**2
set xrange[0:1]
set yrange[0:1]
set key noautotitle
set grid x,y front lw 1.3
set style fill solid 1.0 border
set samples 50
set multiplot layout 2,1
plot '+' u 1:(f1(x)):(f2(x)) w filledcurves
plot x1=y1=y3=NaN '+' u (x0=x1,x1=$1):(y0=y1,y1=f1($1),y2=y3,y3=f2($1)):(x0):(x1):\
(y0):(y2) w boxxy
unset multiplot
### end of script
Result: (download the PNG image and check that the background is transparent).
set grid front
works in this case

How to set a legent without points and duplicate with in the plot?

I would like only the title in the legent without point.
When I plot yerrorbar, there is an error:
duplicated or contradicting arguments in plot options
How to rewrite it?
set key tc variable
plot 'file.txt' with points pt 7 title "A" with yerrorbar
If something is missing in my question, let me know please.
In recent gnuplot versions there is a separate option keyentry (introduced in version 5.2.6).
The documentation says
gnuplot> help keyentry
Normally each plot autogenerates a single line entry in the key. If you need
more control over what appears in the key you can use the `keyentry` keyword
in the `plot` or `splot` command to insert extra lines. Instead of providing
a filename or function to plot, use `keyentry` as a placeholder followed by
plot style information (used to generate a key symbol) and a title.
Example
set key box width 3
plot 'file.txt' with points pt 7 notitle, keyentry title "Some Title"
In older gnuplot versions a work-around is to plot some function that lies entirely outside the plot and give it the desired title. This may generate warning messages such as "all points out of range" but it does place the title in the key. Example below. Note that the pointtype is given as an empty string.
set yrange [0:*]
plot 'file.txt' with points pt 7 notitle, -1 with points pt '' title "Some Title"

Draw boundary on colormap in Gnuplot using second data file

I have an ASCII data file (density.dat) in the format (x y D), where D represents a density-value at the point (x,y), from which I create a colormap:
set pm3d interpolate 2,2 corners2color mean
set view map
splot data_file u (1e9*$1):(1e9*$2):3 with pm3d
I have a second data file (potential.dat) with the same format (x y P), where P represents a potential value which can only have one of two values zero or 1.0 (say). I would like to indicate the boundary between the (three) regions (a straight line with slight perturbations) where the potential is zero and the regions where it is non-zero and overlay it on the first colormap of the density.
I though the approach where I plot the contours from one file on the plot of another file (similar to here), could work but the exported data using the 'set table filename' cannot be used for 'plot filename with image' it seems.
I would greatly appreciate ideas (or solutions..) to tackle this problem.
======== EDIT ==========
Working solution for me:
set contour base
set cntrparam level discrete 1.0
splot density_file u 1:2:3 with pm3d nocontour,\
potential_file u 1:2:3 w l nosurface
In the link you give, with image is used to plot the colour map, not the isolines, these are plotted with lines. I am not sure why you need to go through a table at all, it seems that you can handle everything with splot commands, unless you want to make fancy customisations or have strong constraints with margin control. See the discussion at the top of the post
http://gnuplot-tricks.blogspot.co.uk/2009/07/maps-contour-plots-with-labels.html
You will need to give more details in your post if you want a more precise answer.

How can I plot a smoothed curve as well as the original data?

I want to add markers to some of my plots using pointtype. If I plot data like so:
plot "somedata.txt" w linespoint pointtype 6
or a function like so:
plot cos(x) pointtype 6
I get exactly what I want: a line between and a marker on top of all data points. Right now I want to achieve the same, but after "smoothing" out a dataset using smooth bezier:
plot "somedata.txt" w linespoint pointtype 6 smooth bezier
However pointtype doesn't seem to do anything. I can set linecolor, linewidth and linetype as before, but not pointtype.
Does anyone know of a work-around that can still produce markers on top of a smoothed plot?
I have the same issue that gnuplot does not plot points on top of a smooth curve. I speculate that since gnuplot is plotting a function derived from the data points, it does not bother putting the point markers on top of the original data points
Note that a bezier curve will not necessarily overlay the original data points.
My workaround would involve plotting the data twice in different ways:
plot 'data.txt' with points title 'original data', \
'' smooth bezier title 'smoothed data'
I agree with #andyras. I have this issue a few weeks algo and couldn't find a way to put both, the smoothed curve and the data. Thus, I plotted two series, one with the smoothed curve and another just for the points.
Edit: sorry for adding a new answer. I'm on my phone and couldn't find a way to comment under #andyras answer

How to treat the first line of the data file as column labels in gnuplot?

I have a table like this:
A B C D E F G H I
10 23998 16755 27656 17659 19708 20328 19377 18925
20 37298 33368 53936 41421 44548 40756 40985 37294
I use this command to plot
plot "C:/file.txt" using 1:2 with lines smooth bezier, "C:/file.txt" using 1:3 with lines smooth bezier, ...
However, all the labels come out as the file name. Is it possible for gnuplot to read the first row and label the lines accordingly?
set key autotitle columnhead
plot for [n=2:12] 'vv.csv' u 1:(column(n)) w lines title columnhead(n)
n starts from 2 to skip the header.
I checked the documentation and I don't see a way to do it automatically, but you can manually set a title with
plot "file.txt" using 1:2 title "A" with lines smooth bezier ...
I once wrote a script to plot FM radio station frequencies along an axis from 87MHz to 108MHz, using the names of each radio station as vertical labels. This was not a pure gnuplot solution, the input file is processed with perl with make, but I suggest you have a look at it and see if you can use something like that.
You could also use a gnuplot toolkit such as this one for Python if you want have a lot of data to plot and you want to automate the extraction of the titles.

Resources