axis_offset Subroutine

public pure subroutine axis_offset(lo, hi, k, off, txt)

Find matplotlib ScalarFormatter offset of linear time axis.

Follows _compute_offset (matplotlib 3.10 ticker.py) but from view limits, not tick positions: highest decade where lo and hi agree, used only if it saves at least four digits. Text in largest of yr to Gyr where offset is at least 1 if ticks are in one of those, else tick unit (offset, docs/commands.md).

Arguments

Type IntentOptional Attributes Name
real(kind=real64), intent(in) :: lo

Lower view limit, in tick unit.

real(kind=real64), intent(in) :: hi

Upper view limit, in tick unit.

integer(kind=int32), intent(in) :: k

UNAME index of the tick unit.

real(kind=real64), intent(out) :: off

Offset in tick unit, 0 if none applies.

character(len=*), intent(out) :: txt

Offset as matplotlib shows it (+... and unit label), blank if none applies.


Source Code

   pure subroutine axis_offset(lo, hi, k, off, txt)
      !! Find matplotlib `ScalarFormatter` offset of linear time axis.
      !!
      !! Follows `_compute_offset` (matplotlib 3.10 `ticker.py`) but from view limits, not tick
      !! positions: highest decade where `lo` and `hi` agree, used only if it saves at least four
      !! digits. Text in largest of `yr` to `Gyr` where offset is at least 1 if ticks are in one of
      !! those, else tick unit (`offset`, docs/commands.md).
      ! Arguments
      real(real64), intent(in) :: lo
         !! Lower view limit, in tick unit.
      real(real64), intent(in) :: hi
         !! Upper view limit, in tick unit.
      integer(int32), intent(in) :: k
         !! `UNAME` index of the tick unit.
      real(real64), intent(out) :: off
         !! Offset in tick unit, 0 if none applies.
      character(len=*), intent(out) :: txt
         !! Offset as matplotlib shows it (`+...` and unit label), blank if none applies.
      real(real64) :: amin, amax, sgn, oommax, oom, n
      character(len=24) :: digits
      integer(int32) :: u, q

      off = 0.d0; txt = ''
      if (lo == hi .or. (min(lo, hi) <= 0.d0 .and. max(lo, hi) >= 0.d0)) return
      amin = min(abs(lo), abs(hi)); amax = max(abs(lo), abs(hi))
      sgn = sign(1.d0, min(lo, hi))
      oommax = real(ceiling(log10(amax)), real64)
      oom = oommax
      do while (floordiv(amin, 10.d0**oom) == floordiv(amax, 10.d0**oom))
         oom = oom - 1.d0
      end do
      oom = oom + 1.d0
      ! straddling a multiple of a large power of ten (relative to the span)
      if ((amax - amin)/10.d0**oom <= 1.d-2) then
         oom = oommax
         do while (floordiv(amax, 10.d0**oom) - floordiv(amin, 10.d0**oom) <= 1.d0)
            oom = oom - 1.d0
         end do
         oom = oom + 1.d0
      end if
      n = floordiv(amax, 10.d0**oom)
      if (n < 1.d3) return
      off = sgn*n*10.d0**oom

      ! the offset is n x 10^q in unit u
      write (digits, '(i0)') int(n, int64)
      u = k; q = int(oom, int32)
      if (k >= IYR .and. k <= IGYR) then
         do u = IGYR, IYR, -1
            if (len_trim(digits) - 1 + q + 3*(k - u) >= 0) exit
         end do
         u = max(u, IYR)
         q = q + 3*(k - u)
      end if
      txt = merge('+', '-', sgn > 0.d0)//trim(fmt_number(trim(digits), q))//' '//trim(ULABEL(u))
   end subroutine axis_offset