How to make dashed grid lines intersect making crosshairs in gnuplot? - gnuplot

I'm plotting some data and I want to use dashed grid lines.
Any dashed grid line would suffice, but I prefer a "long dash, short dash, long dash" format.
For example, given the following code
set grid lc rgb "#000000" lt 1 dt (50, 25, 20, 25)
plot x**2
I get this result
But I would rather the grid lines intersection to happen always at the middle of two dashes, like this
If I could make horizontal grid lines different to vertical grid lines and I could add some offset to each one, then I'd imagine there's a way to accomplish this. But I can't seem to do that either.

It looks like gnuplot cannot have two different dashstyles for x-grid and y-grid.
One workaround I see currently is to plot two identical plot on top of each other. One with appropriate x-grid lines and the other with appropriate y-grid lines.
If you want a dash pattern with proportions of (50-25-20-25), this correspond to (25-25-20-25-25-0) or (5-5-4-5-5-0) between two tics.
Furthermore, the dash and gap length numbers, e.g. in dt (50,25,20,25), seem to be in a fixed relation to the graph size. The "empirical" factor is 11 with good approximation (at least for the wxt terminal which I tested under gnuplot 5.2.6).
Edit: actually, the code below gives different results with a qt terminal. And it's not just a different factor. It's more complicated and probably difficult to solve without insight into the source code. So, the fact that the following seems to work with wxt terminal (maybe even just under Windows?) was probably a lucky strike.
With this you can create your dash lines automatically resulting in crosshairs at the intersections of the major grid lines.
Assumptions are:
your first and last tics are on the borders
you know the number of x- and y-intervals
You also need to know the graph size. These values are stored in the variables GPVAL_TERM..., but only after plotting. That's why you have to replot to get the correct values.
This workaround at least should give always crosshairs at the intersection of the major grid lines.
Edit 2: just for "completeness". The factors to get the same (or similar) looking custom dashed pattern on different terminals varies considerably. wxt approx. 11, qt approx. 5.6, pngcairoapprox. 0.25. This is not what I would expect. Furthermore, it looks like the factors slightly depend on x and y as well as graph size. In order to get "exact" crosshairs you might have to tweak these numbers a little further.
Code:
### dashed grid lines with crosshairs at intersections
reset session
TERM = "wxt" # choose terminal
if (TERM eq "wxt") {
set term wxt size 800,600
FactorX = 11. # wxt
FactorY = 11. # wxt
}
if (TERM eq "qt") {
set term qt size 800,600
FactorX = 5.58 # qt
FactorY = 5.575 # qt
}
if (TERM eq "pngcairo") {
set term pngcairo size 800,600
set output "tbDashTest.png"
FactorX = 0.249 # pngcairo
FactorY = 0.251 # pngcairo
}
set multiplot
set ticscale 0,0
Units = 24 # pattern (5,5,4,5,5,0) are 24 units
# set interval and repetition parameters
IntervalsY = 10
RepetitionsY = 1
IntervalsX = 4
RepetitionsX = 3
# initial plot to get graph size
plot x**2
gX = real(GPVAL_TERM_YMAX-GPVAL_TERM_YMIN)/IntervalsY/Units/FactorY/RepetitionsY
gY = real(GPVAL_TERM_XMAX-GPVAL_TERM_XMIN)/IntervalsX/Units/FactorX/RepetitionsX
# first plot with x-grid lines
set grid xtics lt 1 lc rgb "black" dt (gX*5,gX*5,gX*4,gX*5,gX*5,0)
replot
unset grid
# second plot with y-grid lines
set grid ytics lt 1 lc rgb "black" dt (gY*5,gY*5,gY*4,gY*5,gY*5,0)
replot
unset multiplot
set output
### end of code
Result:

Not really. The closest I can think of is
set grid x y mx my
set grid lt -1 lc "black" lw 1 , lt -1 lc bgnd lw 16
set ticscale 1.0, 0.01
set mxtics 4
plot x**2 lw 2
But that leaves the vertical grid lines solid.

Related

Gnuplot: oscilloscope-like line style?

Is it possible in Gnuplot to emulate the drawing style of an analogue oscilloscope, meaning thinner+dimmisher lines on larger amplitudes, like this:?
The effect you see in the oscilloscope trace is not due to amplitude, it is due to the rate of change as the trace is drawn. If you know that rate of change and can feed it to gnuplot as a third column of values, then you could use it to modulate the line color as it is drawn:
plot 'data' using 1:2:3 with lines linecolor palette z
I don't know what color palette would work best for your purpose, but here is an approximation using a function with an obvious, known, derivative.
set palette gray
set samples 1000
plot '+' using ($1):(sin($1)):(abs(cos($1))) with lines linecolor palette
For thickness variations, you could shift the curve slightly up and down, and fill the area between them.
f(x) = sin(2*x) * sin(30*x)
dy = 0.02
plot '+' u 1:(f(x)+dy):(f(x)-dy) w filledcurves ls 1 notitle
This does not allow variable colour, but the visual effect is similar.
Another approach:
As #Ethan already stated, the intensity is somehow proportional to the speed of movement, i.e. the derivative. If you have sin(x) as waveform, the derivative is cos(x). But what if you have given data? Then you have to calculate the derivative numerically.
Furthermore, depending on the background the line should fade from white (minimal derivative) to fully transparent (maximum derivative), i.e. you should change the transparency with the derivative.
Code:
### oscilloscope "imitation"
reset session
set term wxt size 500,400 butt # option butt, otherwise you will get overlap points
set size ratio 4./5
set samples 1000
set xrange[-5:5]
# create some test data
f(x) = 1.5*sin(15*x)*(cos(1.4*x)+1.5)
set table $Data
plot '+' u 1:(f($1)) w table
unset table
set xtics axis 1 format ""
set mxtics 5
set grid xtics ls -1
set yrange[-4:4]
set ytics axis 1 format ""
set mytics 5
set grid ytics ls -1
ColorScreen = 0x28a7e0
set obj 1 rect from screen 0,0 to screen 1,1 behind
set obj 1 fill solid 1.0 fc rgb ColorScreen
x0=y0=NaN
Derivative(x,y) = (dx=x-x0,x0=x,x-dx/2,dy=y-y0,y0=y,dy/dx) # approx. derivative
# get min/max derivative
set table $Dummy
plot n=0 $Data u (d=abs(Derivative($1,$2)),n=n+1,n<=2? (dmin=dmax=d) : \
(dmin>d ? dmin=d:dmin), (dmax<d?dmax=d:dmax)) w table
unset table
myColor(x,y) = (int((abs(Derivative(column(x),column(y)))-dmin)/(dmax-dmin)*0xff)<<24) +0xffffff
plot $Data u 1:2:(myColor(1,2)) w l lw 1.5 lc rgb var not
### end of code
Result:

Gnuplot Vertical Dashed Line

I would like to add a gray dashed vertical line on my graph with GNUPLOT.
My terminal is;
set terminal postscript portrait enhanced color dashed lw 1 "DejaVuSans" 12 size 6,6
set output "pic.ps"
set xrange [30:110]
plot 'smth.txt' w lines ls 7 t ""
I have looked into several ideas around but there seems to no explanation about how to get a dashed gray line vertically at let's say x = 40 (the line gonna be parallel to y-axis)
The key is to use the set arrow command and remove the head to make it a line:
set arrow from 40,0 to 40,100 nohead lc rgb 'red' dt 2
Your graph does not specify any y value, so adjust it to a "larger than needed" value.
For dash type (dt), checkout p.42 of manual. It is terminal-dependent (I didn't tried with postscript), but you can adjust as needed.

Histogram in GNUplot, each bar with a different color

I am trying to make a barplot with GNUplot, were each bar has a different color. I have found out in the manual that it can be done using lc rgbcolor variable.
My data folder looks like this,
ACB 0.106372
ASW 0.10909
BEB 0.110973
CDX 0.106577
CEU 0.102091
CHB 0.108829
CHS 0.110807
CLM 0.108803
My plot script is as follows,
set style histogram gap 2
set autoscale y
set style fill solid
set xtics rotate by 90 offset 0,-1.2
rgb(r,g,b) = 65536 * int(r*10) + 256 * int(g*10) + int(b*10)
set boxwidth .5
set output "Plot.eps"
plot "plot.tsv" using 2:xticlabels(1):(rgb($2,$2,$2)) with boxes lc rgb variable
I have used the rgb function that is shown in the GNUplot manual. But it shows the following error.
plot "plot.tsv" using 2:xticlabels(1):(rgb($2,$2,$2)) with boxes lc rgb variable
^
"#Plot.plt", line 18: x range is invalid
I can't figure out what the error means.
Please help.
Thanks in advance.
You are actually plotting with style "boxes", not "histograms". That probably is what you want, but whereas for histograms the x coordinate is implicit, for boxes you have to provide an x coordinate in the first column even if you're OK with the ordinal numbers 0,1,2,3,...
So
plot "plot.tsv" using 0:2:xticlabels(1):(rgb($2,$2,$2)) with boxes lc rgb variable
Oh, and you probably want to set the fillstyle also so that the colors are visible:
set style fill solid

Exact meaning of dashtype index in the Gnuplot 5.0?

I got a problem in Gnuplot 5.0, the dashtype index, such as ``set dashtype 1 (2,5,2,15)` in the manual.
My question is: what do these numbers in parenthesis exactly mean? I tried to change them to get a feeling, but it would be great to know the exact meaning.
My actual problem is, that simply using dashtype N, gives lines which look very different from the previous 4.6 version, unfortunately. I just try to reproduce the plots which were made in gnuplot 4.6.
Those are numerical pairs
<solid length>,<empty length>
Those lengths are factors for an internal unit length. The dash pattern length further depends on the dashlength terminal option and the line width.
So, having
plot x dt (2,4,2,6)
plots a dash, an empty space twice as long, again a dash of same length and an empty space three times the dash length.
The actual length of the first dash is then
linewidth * terminal_linewidth * solid_length * terminal_dashlength * dash_unit
First example:
set terminal pngcairo size 600,50 dashlength 2 linewidth 1
set output 'dash1.png'
unset border; unset key; unset tics
plot 0 dt (2,4,2,6) lw 10
What might be confusing is, that some terminals like qt or wxt use rounded line ends by default (terminal option round), which are applied to every single dash, which distorts the actually given dash lengths:
set terminal pngcairo size 600,50 round dashlength 2 linewidth 1
set output 'dash2.png'
unset border; unset key; unset tics
plot 0 dt (2,4,2,6) lw 10
Terminal option square extends each dash by one line width at each end:
set terminal pngcairo size 600,50 square dashlength 2 linewidth 1
set output 'dash2.png'
unset border; unset key; unset tics
plot 0 dt (2,4,2,6) lw 10
In this last example, the final dash lengths are (4,2,4,4).
To get the behavior of the first example (exact dash length) also by default with other terminals, use the butt terminal option.
from: http://www.gnuplot.info/gnuplot_cvs.pdf page 37
set dashtype 1 (2,5,2,15)
means: define stype 1 as:
solid 2
empty 5
solid 2
empty 15
(== == )*

How to add symbols representing point types in y labels

I want to add the symbols representing point types to y and y2 labels.
Here is the test code:
set key center top;
set ylabel "x";
set y2label "x^2";
plot x w lp pt 5 pi 5, x**2 w lp pt 7 pi 5 axes x1y2;
It gives:
I want the following:
Is there anyway to achieve this in Gnuplot?
I am using Gnuplot 4.6.5.
Thanks.
One option would be to use set label ... point pt 7 lt 2 to place a dot somewhere. But that needs a lot of tweaking to get the correct position. And that position would need to be adapted manually depending on the font, font size, canvas size etc.
If only the point shape is important, you can use the respective glyph from the unicode block: geometric shapes. This is also from where I copied the symbols to the script. You must of course also use a font which includes the respective glyphs (e.g. Arial Unicode or DejaVu) and a proper terminal (e.g. pdfcairo, pngcairo, wxt are all fine):
set terminal pngcairo enhanced font "DejaVu" size 600,300
set output 'square-circle.png'
set key center top
set encoding utf8
set ylabel "x {/*0.8 ■}"
set y2label "x^2 {/*0.8 ●}"
plot x w lp pt 5 pi 5, x**2 w lp pt 7 pi 5 axes x1y2;
Result with 4.6.5 is:

Resources