Coverage for python/lsst/utils/plotting/publication_plots.py: 100%

73 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-10-29 01:42 -0700

1# This file is part of utils. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (https://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# Use of this source code is governed by a 3-clause BSD-style 

10# license that can be found in the LICENSE file. 

11"""Utilities for making publication-quality figures.""" 

12 

13__all__ = [ 

14 "accent_color", 

15 "divergent_cmap", 

16 "galaxies_cmap", 

17 "galaxies_color", 

18 "get_band_dicts", 

19 "mk_colormap", 

20 "set_rubin_plotstyle", 

21 "sso_cmap", 

22 "sso_color", 

23 "stars_cmap", 

24 "stars_color", 

25] 

26 

27import numpy as np 

28 

29from . import ( 

30 get_multiband_plot_colors, 

31 get_multiband_plot_linestyles, 

32 get_multiband_plot_symbols, 

33) 

34 

35 

36def set_rubin_plotstyle(lowres=False): # type: ignore 

37 """Set the matplotlib style for Rubin publications. 

38 

39 Parameters 

40 ---------- 

41 lowres : `bool`, optional 

42 If `True`, set figure resolution to lower dpi. 

43 """ 

44 import matplotlib as mpl 

45 from matplotlib import style 

46 

47 style.use("lsst.utils.plotting.rubin") 

48 

49 if lowres: 

50 mpl.rcParams["figure.dpi"] = 100.0 

51 

52 

53def mk_colormap(colorNames): # type: ignore 

54 """Make a colormap from the list of color names. 

55 

56 Parameters 

57 ---------- 

58 colorNames : `list` 

59 A list of strings that correspond to matplotlib named colors. 

60 

61 Returns 

62 ------- 

63 cmap : `matplotlib.colors.LinearSegmentedColormap` 

64 A colormap stepping through the supplied list of names. 

65 """ 

66 from matplotlib import colors 

67 

68 blues = [] 

69 greens = [] 

70 reds = [] 

71 alphas = [] 

72 

73 if len(colorNames) == 1: 

74 # Alpha is between 0 and 1 really but 

75 # using 1.5 saturates out the top of the 

76 # colorscale, this looks good for ComCam data 

77 # but might want to be changed in the future. 

78 alphaRange = [0.2, 1.0] 

79 nums = np.linspace(0, 1, len(alphaRange)) 

80 r, g, b = colors.colorConverter.to_rgb(colorNames[0]) 

81 for num, alpha in zip(nums, alphaRange): 

82 blues.append((num, b, b)) 

83 greens.append((num, g, g)) 

84 reds.append((num, r, r)) 

85 alphas.append((num, alpha, alpha)) 

86 

87 else: 

88 nums = np.linspace(0, 1, len(colorNames)) 

89 if len(colorNames) == 3: 

90 alphaRange = [1.0, 1.0, 1.0] 

91 elif len(colorNames) == 5: 

92 alphaRange = [1.0, 0.7, 0.3, 0.7, 1.0] 

93 else: 

94 alphaRange = np.ones(len(colorNames)) 

95 

96 for num, color, alpha in zip(nums, colorNames, alphaRange): 

97 r, g, b = colors.colorConverter.to_rgb(color) 

98 blues.append((num, b, b)) 

99 greens.append((num, g, g)) 

100 reds.append((num, r, r)) 

101 alphas.append((num, alpha, alpha)) 

102 

103 colorDict = {"blue": blues, "red": reds, "green": greens, "alpha": alphas} 

104 cmap = colors.LinearSegmentedColormap("newCmap", colorDict) 

105 return cmap 

106 

107 

108def divergent_cmap(): # type: ignore 

109 """Make a divergent color map. 

110 

111 Returns 

112 ------- 

113 cmap : `matplotlib.colors.LinearSegmentedColormap` 

114 A colormap stepping from the standard stars color through 

115 gray to the accent color. 

116 """ 

117 cmap = mk_colormap([stars_color(), "#D9DCDE", accent_color()]) 

118 

119 return cmap 

120 

121 

122def stars_cmap(single_color=False): # type: ignore 

123 """Make a color map for stars. 

124 

125 Parameters 

126 ---------- 

127 single_color : `bool`, optional 

128 If `True` use the standard stars color. 

129 

130 Returns 

131 ------- 

132 cmap : `matplotlib.colors.Colormap` 

133 A colormap derived from the standard stars color if 

134 ``single_color`` is `True`, otherwise the seaborn "mako" palette. 

135 """ 

136 import seaborn as sns 

137 from matplotlib.colors import ListedColormap 

138 

139 if single_color: 

140 cmap = mk_colormap([stars_color()]) 

141 else: 

142 cmap = ListedColormap(sns.color_palette("mako", 256)) 

143 return cmap 

144 

145 

146def stars_color() -> str: 

147 """Return the star color string for lines.""" 

148 return "#084d96" 

149 

150 

151def accent_color() -> str: 

152 """Return a contrasting color for overplotting. 

153 

154 Notes 

155 ----- 

156 Black is the best for this but if you need two colors 

157 this works well on blue. 

158 """ 

159 return "#DE8F05" 

160 

161 

162def galaxies_cmap(single_color: bool = False): # type: ignore 

163 """Make a color map for galaxies. 

164 

165 Parameters 

166 ---------- 

167 single_color : `bool`, optional 

168 If `True` use the standard galaxies color. 

169 

170 Returns 

171 ------- 

172 cmap : `matplotlib.colors.LinearSegmentedColormap` or `str` 

173 A colormap derived from the standard galaxies color if 

174 ``single_color`` is `True`, otherwise the colormap name "inferno". 

175 """ 

176 if single_color: 

177 cmap = mk_colormap([galaxies_color()]) 

178 else: 

179 cmap = "inferno" 

180 return cmap 

181 

182 

183def galaxies_color() -> str: 

184 """Return the galaxy color string for lines.""" 

185 return "#961A45" 

186 

187 

188def sso_color() -> str: 

189 """Return the SSO color string for lines.""" 

190 return "#01694c" 

191 

192 

193def sso_cmap(single_color: bool = False): # type: ignore 

194 """Make a color map for solar system objects. 

195 

196 Parameters 

197 ---------- 

198 single_color : `bool`, optional 

199 If `True` use the standard SSO color. 

200 

201 Returns 

202 ------- 

203 cmap : `matplotlib.colors.LinearSegmentedColormap` or `str` 

204 A colormap derived from the standard SSO color if 

205 ``single_color`` is `True`, otherwise the colormap name "viridis". 

206 """ 

207 if single_color: 

208 cmap = mk_colormap([sso_color()]) 

209 else: 

210 cmap = "viridis" 

211 return cmap 

212 

213 

214def get_band_dicts() -> dict: 

215 """Define palettes, from RTN-045. 

216 

217 Returns 

218 ------- 

219 band_dict : `dict` of `dict` 

220 Dicts of colors, colors_black, symbols, and line_styles, 

221 keyed on bands 'u', 'g', 'r', 'i', 'z', and 'y'. 

222 

223 Notes 

224 ----- 

225 This includes dicts for colors (bandpass colors for white background), 

226 colors_black (bandpass colors for black background), plot symbols, and 

227 line_styles, keyed on band (ugrizy). 

228 """ 

229 colors = get_multiband_plot_colors() 

230 colors_black = get_multiband_plot_colors(dark_background=True) 

231 symbols = get_multiband_plot_symbols() 

232 line_styles = get_multiband_plot_linestyles() 

233 

234 return { 

235 "colors": colors, 

236 "colors_black": colors_black, 

237 "symbols": symbols, 

238 "line_styles": line_styles, 

239 }