From 26fb5fc07b536876d673a2926e860a856d2f37b3 Mon Sep 17 00:00:00 2001
From: "Michele.Maris" <michele.maris@inaf.it>
Date: Tue, 23 Apr 2024 18:11:36 +0200
Subject: [PATCH] u

---
 src/yapsut/graphics.py | 31 ++++++++++++++++++++++---------
 1 file changed, 22 insertions(+), 9 deletions(-)

diff --git a/src/yapsut/graphics.py b/src/yapsut/graphics.py
index 923b50e..ed94fe1 100644
--- a/src/yapsut/graphics.py
+++ b/src/yapsut/graphics.py
@@ -1,6 +1,3 @@
-import matplotlib.pyplot as plt
-import numpy as np
-
 class ImshowXT :
    """ class to handle an improved version of plt.imshow """
    def __init__(self,matr,x_values,y_values,**kwargs) :
@@ -13,17 +10,25 @@ class ImshowXT :
       self._im=plt.imshow(matr,**kwargs)
       self._xv=x_values
       self._yv=y_values
-   def set_xaxis(self,major_values,major_format) :
-      """ format the x axis for a give list of tick values and a formatting string """
+   def set_xaxis(self,major_values,major_format,major_at_nearestPixel=False) :
+      """ format the x axis for a give list of major tick values and a formatting string 
+          major_at_nearestPixel = True places each major_value at the nearest pixel (default False)
+      """
       from matplotlib import pyplot as plt
       import numpy as np
+      from scipy.interpolate import interp1d
       self._mxv=major_values
       self._mxf=major_format
       #
       v=self._xv
-      t=np.array([np.argmin(np.abs(k-v)) for k in major_values])
       t1=np.arange(len(v))
       #
+      if major_at_nearestPixel :
+         t=np.array([np.argmin(np.abs(k-v)) for k in major_values])
+      else :
+         itp=interp1d(v,t1,fill_value='extrapolate')
+         t=itp(major_values)
+      #
       plt.xticks(t,np.interp(t,t1,v))
       tt=plt.gca().get_xticklabels()
       for ik in range(len(tt))  :
@@ -31,17 +36,25 @@ class ImshowXT :
          o1=major_format%o
          tt[ik].set_text(o1)
       plt.gca().set_xticklabels(tt)
-   def set_yaxis(self,major_values,major_format) :
-      """ format the y axis for a give list of tick values and a formatting string """
+   def set_yaxis(self,major_values,major_format,major_at_nearestPixel=False) :
+      """ format the y axis for a give list of major tick values and a formatting string 
+          major_at_nearestPixel = True places each major_value at the nearest pixel (default False)
+      """
       from matplotlib import pyplot as plt
       import numpy as np
+      from scipy.interpolate import interp1d
       self._myv=major_values
       self._myf=major_format
       #
       v=self._yv
-      t=np.array([np.argmin(np.abs(k-v)) for k in major_values])
       t1=np.arange(len(v))
       #
+      if major_at_nearestPixel :
+         t=np.array([np.argmin(np.abs(k-v)) for k in major_values])
+      else :
+         itp=interp1d(v,t1,fill_value='extrapolate')
+         t=itp(major_values)
+      #
       plt.yticks(t,np.interp(t,t1,v))
       tt=plt.gca().get_yticklabels()
       for ik in range(len(tt))  :
-- 
GitLab