Advertisement
Abhisek92

cyclic_colormap.py

Nov 22nd, 2022
659
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.05 KB | None | 0 0
  1. from matplotlib.colors import Colormap
  2. from  matplotlib import colormaps
  3.  
  4. class CyclicColormap(Colormap):
  5.     def __init__(self, name: str, v_min=0.0, v_max=1.0, n: int = 256):
  6.         self._colormap = colormaps.get_cmap(name).resampled(lutsize=n)
  7.         self._v_min = v_min
  8.         self._v_max = v_max
  9.         super(CyclicColormap, self).__init__(
  10.             name=f'cyclic_{name}', N=n
  11.         )
  12.  
  13.     def get_range(self):
  14.         return {'v_min': self._v_min, 'v_max': self._v_max}
  15.  
  16.     def set_range(self, v_min=None, v_max=None):
  17.         if v_min is not None:
  18.             self._v_min = v_min
  19.         if v_max is not None:
  20.             self._v_max = v_max
  21.  
  22.     def reversed(self, name=None):
  23.         return CyclicColormap(name=f"{self._colormap.name}_r", n=self.N)
  24.  
  25.     def get_bad(self):
  26.         return self._colormap.get_bad()
  27.  
  28.     def set_bad(self, color='k', alpha=None):
  29.         self._colormap.set_bad(color=color, alpha=alpha)
  30.  
  31.     def get_under(self):
  32.         return self._colormap.get_under()
  33.  
  34.     def set_under(self, color='k', alpha=None):
  35.         self._colormap.set_under(color=color, alpha=alpha)
  36.  
  37.     def get_over(self):
  38.         return self._colormap.get_over()
  39.  
  40.     def set_over(self, color='k', alpha=None):
  41.         self._colormap.set_over(color=color, alpha=alpha)
  42.  
  43.     def is_gray(self):
  44.         return self._colormap.is_gray()
  45.  
  46.     def resampled(self, lut_size):
  47.         self._colormap = self._colormap.resampled(lutsize=lut_size)
  48.         self.N = self._colormap.N
  49.  
  50.     def set_extremes(self, *, bad=None, under=None, over=None):
  51.         self._colormap.set_extremes(bad=bad, under=under, over=over)
  52.  
  53.     def with_extremes(self, *, bad=None, under=None, over=None):
  54.         new_cm = self.copy()
  55.         new_cm.set_extremes(bad=bad, under=under, over=over)
  56.         return new_cm
  57.  
  58.     def __call__(self, x, alpha=None, bytes=False):
  59.         x = ((x - self._v_min) / (self._v_max - self._v_min)) % 1
  60.         return self._colormap(x, alpha, bytes)
  61.  
  62. ## Usage
  63. # cm = CyclicColormap('jet', 0, 360)
  64. # plt.plot(..., cmap=cm)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement