Coverage for pesummary/gw/plots/cmap.py: 100.0%

28 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-05-02 08:42 +0000

1# Copyright (C) 2014-2016 Leo Singer, Charlie Hoy 

2# 

3# This program is free software: you can redistribute it and/or modify 

4# it under the terms of the GNU General Public License as published by 

5# the Free Software Foundation, either version 3 of the License, or 

6# (at your option) any later version. 

7# 

8# This program is distributed in the hope that it will be useful, 

9# but WITHOUT ANY WARRANTY; without even the implied warranty of 

10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

11# GNU General Public License for more details. 

12# 

13# You should have received a copy of the GNU General Public License 

14# along with this program. If not, see <http://www.gnu.org/licenses/>. 

15 

16from matplotlib import cm 

17from matplotlib import colors 

18import numpy as np 

19import pesummary 

20 

21 

22def register_cylon(): 

23 # Read in color map RGB data. 

24 path = pesummary.__file__[:-12] 

25 with open(path + "/gw/plots/cylon.csv") as f: 

26 data = np.loadtxt(f, delimiter=',') 

27 

28 # Create color map. 

29 cmap = colors.LinearSegmentedColormap.from_list("cylon", data) 

30 # Assign in module. 

31 locals().update({"cylon": cmap}) 

32 # Register with Matplotlib. 

33 cm.register_cmap(cmap=cmap) 

34 # Create inverse color map 

35 cmap_r = colors.LinearSegmentedColormap.from_list("cylon_r", data[::-1]) 

36 locals().update({"cylon_r": cmap_r}) 

37 cm.register_cmap(cmap=cmap_r) 

38 

39 

40def unregister_cylon(): 

41 cm.unregister_cmap("cylon") 

42 cm.unregister_cmap("cylon_r") 

43 

44 

45def colormap_with_fixed_hue(color, N=10): 

46 """Create a linear colormap with fixed hue 

47 

48 Parameters 

49 ---------- 

50 color: tuple 

51 color that determines the hue 

52 N: int, optional 

53 number of colors used in the palette 

54 """ 

55 import seaborn 

56 from matplotlib.colors import LinearSegmentedColormap 

57 from matplotlib.colors import rgb_to_hsv, hsv_to_rgb, hex2color 

58 

59 color_hsv = rgb_to_hsv(hex2color(color)) 

60 base = seaborn.color_palette("Blues", 10) 

61 base_hsv = np.array(list(map(rgb_to_hsv, base))) 

62 h, s, v = base_hsv.T 

63 

64 h_fixed = np.ones_like(h) * color_hsv[0] 

65 color_array = np.array(list(map( 

66 hsv_to_rgb, np.vstack([h_fixed, s * color_hsv[1], v]).T))) 

67 return LinearSegmentedColormap.from_list("mycmap", color_array)