gnuplot: same grid intervals, round lable values, draw lines

Viewed 75

few questions on gnuplot:

  1. How to set the same intervals of grid tics (same grid step) both in value and in pixels on both axes? Tics interval is set manually, but autoscale method is wanted (if different initial data has different max values) with condition that step and min-max will be the same for x and y. If it possible in gnuplot. Maybe request is very special.
  2. How to round label values? (if data is 1.5000 , label is 1.5000, but want: 1 1.5 2 2.5 etc, floor($1) and int($1) doesn't work?).
  3. Is it possible to insert a line connecting points with the max distance of 0,0 (as in the example picture) (maybe there should be a condition, if x^2+y^2 = max, then draw a line)
  4. How to highlight with a bold line those grid lines that intersect 0,0? Or draw separate lines (done, but maybe better way exists)?

Example:

[example1

My graph:

my graph

set terminal windows 0 position 20,20
set terminal windows size 800,800

set xtics 0.25
set ytics 0.25

set grid xtics ytics

set size ratio -1 # now length of axes on monitor are ~ 17.5 cm and 18.5 cm, not square
#set size square

set style circle radius graph 0.25/2

maxv=1
set yrange [-maxv:maxv]
set xrange [-maxv:maxv]

set ylabel 'Y' font ", 11"
set xlabel 'X' font ", 11"

filename='file1.txt'

set arrow from 0, graph 0 to 0, graph 1 lc 'gray' nohead


plot 0 lc 'gray' notitle, \
filename using ($2-$3):($4-$5):(floor($1)) \
with labels tc lt 1 point pt 7 offset char 1,1 notitle, \
filename using ($2-$3):($4-$5) w l lc black notitle

version 5.2

data:

-6.0    -10.14  -10.10  14.92   14.86
-5.5    -16.75  -16.70  25.60   25.50
-5.0    -20.62  -20.53  30.65   30.49
-4.5    -26.56  -26.42  38.00   37.74
-4.0    -27.80  -27.55  38.25   37.84
-3.5    -33.33  -32.94  43.80   43.19
-3.0    -33.96  -33.37  44.04   43.19
-2.5    -38.02  -37.20  46.64   45.85
-2.0    -38.97  -38.07  44.53   44.18
-1.5    -30.61  -29.97  34.93   35.03
-1.0    -22.60  -22.00  24.24   24.22
-0.5    -7.65   -7.25   5.70    5.47
0.0      2.87    2.84    -3.67   -3.66
0.5      17.62   17.21   -19.79  -19.57
1.0      17.37   16.77   -17.00  -16.95
1.5      30.11   29.50   -32.04  -32.20
2.0      34.69   33.99   -40.26  -40.04
2.5      38.21   37.44   -45.19  -44.37
3.0      34.26   33.67   -42.68  -41.83
3.5      26.69   26.31   -34.85  -34.24
4.0      30.04   29.79   -41.45  -41.03
4.5      20.62   20.48   -28.99  -28.74
1 Answers
  1. You set the same interval of tics and grid via set xtics 0.25 and set ytics 0.25 as you did. If you want the unit of x and y axes to have the same length (independent of the terminal aspect ratio) you set size ratio -1 as you did. You can easily check, e.g. if you set set xrange[-2:2]. Check help size. If your graph on the screen has 17.5 cm x 18.5 cm then this is a peculiarity of your monitor. Then you should compensate by 17.5/18.5 = 0.9459, i.e. try set size ratio -0.9459 (or maybe the inverse -1.0571, depending what is your height and your width).

  2. You can format labels via sprintf(), e.g. sprintf("%.1f",value) or maybe "%.1g". Check help sprintf and help format_specifiers.

  3. From your example, I assume you don't want only the maximum, but the maxima in quadrant 2 and 4, correct? You can extract the maxima during the first plot. For this you define the functions dist(dx,dy) and getDmax(dx,dy) the latter will store the line indices for the maximum values in quadrants 2 and 4 in the variables idxMax2 and idxMax4, respectively. In the third plot command you plot the data as vectors from the origin but only for those two indices.

  4. Check help xzeroaxis.

You can get help for most of the keywords if you type in gnuplot console: help <your keyword>.

Update:

  • show x- and y-label
  • labels on max. distance and angle added (check help atan2)

Script: (I used wxt terminal)

### find max distance from origin
reset session

set terminal wxt size 800,800 background "black"
set size ratio -1
set border lc "white"

$Data <<EOD
-6.0    -10.14  -10.10  14.92   14.86
-5.5    -16.75  -16.70  25.60   25.50
-5.0    -20.62  -20.53  30.65   30.49
-4.5    -26.56  -26.42  38.00   37.74
-4.0    -27.80  -27.55  38.25   37.84
-3.5    -33.33  -32.94  43.80   43.19
-3.0    -33.96  -33.37  44.04   43.19
-2.5    -38.02  -37.20  46.64   45.85
-2.0    -38.97  -38.07  44.53   44.18
-1.5    -30.61  -29.97  34.93   35.03
-1.0    -22.60  -22.00  24.24   24.22
-0.5    -7.65   -7.25   5.70    5.47
0.0      2.87    2.84    -3.67   -3.66
0.5      17.62   17.21   -19.79  -19.57
1.0      17.37   16.77   -17.00  -16.95
1.5      30.11   29.50   -32.04  -32.20
2.0      34.69   33.99   -40.26  -40.04
2.5      38.21   37.44   -45.19  -44.37
3.0      34.26   33.67   -42.68  -41.83
3.5      26.69   26.31   -34.85  -34.24
4.0      30.04   29.79   -41.45  -41.03
4.5      20.62   20.48   -28.99  -28.74
EOD

maxv=1
set xlabel "X" tc "white"
set xrange [-maxv:maxv]
set xtics 0.25
set ylabel "Y" tc "white"
set yrange [-maxv:maxv]
set ytics 0.25
set grid xtics ytics lt 1 lc "white" dt 3
set yzeroaxis lt 1 lc "white" lw 1.8 dt 3
set xzeroaxis lt 1 lc "white" lw 1.8 dt 3

set style fill empty border
set obj 1 circle at 0,0  size 0.25 fc "grey"
set style arrow 1 lc "red" lw 2 dt 2 nohead

set key noautotitle
set angles degrees
idxMax2 = idxMax4 = NaN
dmax2   = dmax4   = 0
dist(dx,dy) = sqrt(dx**2 + dy**2)
getDmax(dx,dy) = dx<0 && dy>0 ? (d=dist(dx,dy),d>dmax2?(dmax2=d,idxMax2=$0):0) : \
                 dx>0 && dy<0 ? (d=dist(dx,dy),d>dmax4?(dmax4=d,idxMax4=$0):0) :NaN

plot $Data u (dx=$2-$3):(dy=$4-$5,getDmax(dx,dy),dy) w l lc "magenta", \
        '' u ($2-$3):($4-$5):(sprintf("%.1f",$1)) w labels tc "white" point pt 7 lc "white" offset char 1,1, \
        '' u ($0==idxMax2 || $0==idxMax4 ? 0 : NaN):(0):($2-$3):($4-$5) w vectors as 1, \
        '' u (f=($0==idxMax2 || $0==idxMax4), dx=($2-$3), dy=($4-$5), \
              f ? dx/2 : NaN):(f ? dy/2 : NaN):(sprintf("%.3f\n%.1f°",dist(dx,dy),atan2(dy,dx))) \
              w labels tc "red" font ",12" offset 3,2
### end of script

Result:

enter image description here

Related