Menu

#575 Allow negative numbers in log-plot

open
nobody
None
5
2024-06-28
2024-06-22
No

Stage 1 If the range of the axis is fully negative, draw it mapping numbers to position as -log(-x).

Stage 2 If the range contains numbers of both signs:

  • map negatives as -C-log(-x) and positives as log(x).
  • Choose C so that the negative part is positioned to the left of the positive one with a small gap (∼¹⁄₁₅ of the total width).
  • The gap corresponds to a strip on the plot. Fill this strip with gray.

Thanks!

Discussion

  • Ilya Zakharevich

    Forgot a couple of points:

    • Do not draw the axis corresponding to value=0.
    • The gray region should be a bit more narrow than the minimal distance between datapoints it separates, to live a tiny gap between the curves/points and this “undefined region”.
    • Sometimes it is tricky to put ticks/labels on a log-axis (see #2717. With both signs present, two continuous regions may become too narrow for a sane placement of ticks/labels.¹⁾

    ¹ ⁾ There is one trick: if one chooses C above so that major ticks are positioned on the same grid, one could use the algorithms of #2717 “on the resulting pseudo-range”.

     
  • Ethan Merritt

    Ethan Merritt - 2024-06-28

    I am not sure I understand correctly.
    A sketched example might help.
    Would this be different from what you do already by defining a nonlinear axis?

    #
    # Use nonlinear mapping to plot
    #       y > C   : log(y)
    #       y < -C  : -log(-y)
    #    -C < y < C : NaN
    #
    
    C = 1.0
    f(y) = (y>C) ? log(y) : (y<-C) ? -log(-y) : 0
    g(y) = y > log(C) ? exp(y) : y < -log(C) ? exp(-y) : NaN
    g(y) = y > log(C) ? exp(y) : NaN
    set nonlinear y via f(y) inv g(y)
    set obj 1 rectangle from -C, graph 0 to C, graph 1 fs solid noborder fc "gray25" back
    
    set sample 1000
    set key left Left reverse
    set tics nomirror
    
    set xrange [-50 : 50]
    set yrange [  * : 50]
    
    plot x title "f(x) = x on +/- logscale y axis"
    
     
  • Ilya Zakharevich

    Thanks! Your picture would be OK with

    • The graph drawn on top of the gray zone (since this zone is “a kind of a glorified y-axis!);
    • the right half being in the log-scale;
    • the left half being in the “negated” log-scale (equally-separated …, -100, -10, -1, -0.1, …)
    • the ranges of the left-half and the right-half (and — probably — also the position of the gray zone) being automatically-deduced from the logarithms of maximal-positive, minimal-positive, maximal-negative and minimal-negative values of x.
     
    • Ethan Merritt

      Ethan Merritt - 2024-06-28

      OK. Here is a more polished version of the same script using a nonlinear x axis rather than y axis.

      #
      # Use nonlinear mapping to plot
      #       x > C   : log(x)
      #       x < -C  : -log(-x)
      #    -C < x < C : NaN
      #
      
      C = 1.9
      ε = 1.e-7
      f(x) = (x>C) ? log(x) : (x<-C) ? -log(-x) : NaN
      g(x) = x > log(C) ? exp(x) : x < -log(C) ? -exp(-x) : NaN
      set nonlinear x via f(x) inv g(x)
      set obj 1 rectangle from -(C+ε), graph 0 to (C+ε), graph 1
      set obj 1 fs solid noborder fc "gray25" back
      
      set sample 1000
      set key left Left reverse
      set tics nomirror
      
      set xrange [-50 : 50]
      set xtics 10
      set xtics add (-2,2)
      set xtics add (-5,5)
      
      plot x title "f(x) = x on +/- logscale x axis", keyentry "C = 1.9"
      
       

      Last edit: Ethan Merritt 2024-06-28
  • Ilya Zakharevich

    Sorry: I see now where you are confused by what I wrote!

    In your picture, the log scale on the horizontal axis makes perfect sense to the right of 2. Likewise to the left of ‒2. However, it is ‘broken’ between ‒2 and 2. “Make this part the gray area!”

    In other words: make the gray strip vertical going somewhere in between of -2 and 2. Does it make more sense now?

     

Log in to post a comment.