2

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 plot x**2 naive approach

But I would rather the grid lines intersection to happen always at the middle of two dashes, like this

grid lines intersecting appropriately

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.

texdditor
  • 105
  • 1
  • 1
  • 9

2 Answers2

3

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:

  1. your first and last tics are on the borders
  2. 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:

enter image description here

theozh
  • 22,244
  • 5
  • 28
  • 72
  • Awesome! This does the job. It's a shame it doesn't work on a qt terminal. So first things first, should we file a bug? Where? The same code should give the same results independently of the terminal. – texdditor Apr 12 '19 at 20:13
  • probably, you can tweak it somehow for the qt terminal, but I haven't been successful so far. You're right, I also hoped that this will work on other terminals as well. If you want to file a bug report, you can do this here: https://sourceforge.net/p/gnuplot/bugs/ maybe we'll get some response from the developers. – theozh Apr 12 '19 at 20:33
  • On another note, this doesn't work using ```pngcairo``` or most useful terminals. Specifically, pngcairo doesn't accept the ```dt (length, space, length, space)``` syntax. It may not be a bug, it should though. It's pretty annoying. – texdditor Apr 13 '19 at 00:56
  • Turns out I'm wrong. It does accept it, but there proportions are far from obvious. – texdditor Apr 13 '19 at 01:27
2

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. enter image description here

Ethan
  • 13,715
  • 2
  • 12
  • 21
  • I may be having a problem, because, in my case, running that code the resulting plot has both vertical and horizontal lines solid. Somehow the second command of grid overrides the previous one. – texdditor Apr 11 '19 at 08:07
  • 1
    Try `set grid x y mx my lt -1 lc "black" lw 1 , lt -1 lc bgnd lw 16` to see the effect @Ethan described. – user8153 Apr 11 '19 at 19:41
  • Oh, that did the trick. Now I get it, it's superimposing white lines over the black ones. – texdditor Apr 12 '19 at 00:03
  • Nice, didn't know about lt -1 (default border lt) and the colourspec "bgnd". – Karl Apr 12 '19 at 18:42