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

空扰寡人 提交于 2019-12-24 00:29:16

问题


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.


回答1:


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.




回答2:


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:



来源:https://stackoverflow.com/questions/55625024/how-to-make-dashed-grid-lines-intersect-making-crosshairs-in-gnuplot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!