Coverage for python / lsst / utils / plotting / figures.py: 27%

24 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-30 08:37 +0000

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 related to making matplotlib figures.""" 

12 

13from __future__ import annotations 

14 

15__all__ = [ 

16 "get_multiband_plot_colors", 

17 "get_multiband_plot_linestyles", 

18 "get_multiband_plot_symbols", 

19 "make_figure", 

20] 

21 

22from typing import TYPE_CHECKING, Any 

23 

24if TYPE_CHECKING: 

25 from matplotlib.figure import Figure 

26 

27 

28def make_figure(**kwargs: Any) -> Figure: 

29 """Make a matplotlib Figure with an Agg-backend canvas. 

30 

31 This routine creates a matplotlib figure without using 

32 ``matplotlib.pyplot``, and instead uses a fixed non-interactive 

33 backend. The advantage is that these figures are not cached and 

34 therefore do not need to be explicitly closed -- they 

35 are completely self-contained and ephemeral unlike figures 

36 created with `matplotlib.pyplot.figure()`. 

37 

38 Parameters 

39 ---------- 

40 **kwargs : `dict` 

41 Keyword arguments to be passed to `matplotlib.figure.Figure()`. 

42 

43 Returns 

44 ------- 

45 figure : `matplotlib.figure.Figure` 

46 Figure with a fixed Agg backend, and no caching. 

47 

48 Notes 

49 ----- 

50 The code here is based on 

51 https://matplotlib.org/stable/gallery/user_interfaces/canvasagg.html 

52 """ 

53 try: 

54 from matplotlib.backends.backend_agg import FigureCanvasAgg 

55 from matplotlib.figure import Figure 

56 except ImportError as e: 

57 raise RuntimeError("Cannot use make_figure without matplotlib.") from e 

58 

59 fig = Figure(**kwargs) 

60 FigureCanvasAgg(fig) 

61 

62 return fig 

63 

64 

65def get_multiband_plot_colors(dark_background: bool = False) -> dict: 

66 """Get color mappings for multiband plots using SDSS filter names. 

67 

68 Notes 

69 ----- 

70 From https://rtn-045.lsst.io/#colorblind-friendly-plots 

71 

72 Parameters 

73 ---------- 

74 dark_background : `bool`, optional 

75 Use colors intended for a dark background. 

76 Default colors are intended for a light background. 

77 

78 Returns 

79 ------- 

80 plot_colors : `dict` of `str` 

81 Mapping of the LSST bands to colors. 

82 """ 

83 plot_filter_colors_white_background = { 

84 "u": "#1600EA", 

85 "g": "#31DE1F", 

86 "r": "#B52626", 

87 "i": "#370201", 

88 "z": "#BA52FF", 

89 "y": "#61A2B3", 

90 } 

91 plot_filter_colors_black_background = { 

92 "u": "#3eb7ff", 

93 "g": "#30c39f", 

94 "r": "#ff7e00", 

95 "i": "#2af5ff", 

96 "z": "#a7f9c1", 

97 "y": "#fdc900", 

98 } 

99 if dark_background: 

100 return plot_filter_colors_black_background 

101 else: 

102 return plot_filter_colors_white_background 

103 

104 

105def get_multiband_plot_symbols() -> dict: 

106 """Get symbol mappings for multiband plots using SDSS filter names. 

107 

108 Notes 

109 ----- 

110 From https://rtn-045.lsst.io/#colorblind-friendly-plots 

111 

112 Returns 

113 ------- 

114 plot_symbols : `dict` of `str` 

115 Mapping of the LSST bands to symbols. 

116 """ 

117 plot_symbols = { 

118 "u": "o", 

119 "g": "^", 

120 "r": "v", 

121 "i": "s", 

122 "z": "*", 

123 "y": "p", 

124 } 

125 return plot_symbols 

126 

127 

128def get_multiband_plot_linestyles() -> dict: 

129 """Get line style mappings for multiband plots using SDSS filter names. 

130 

131 Notes 

132 ----- 

133 From https://rtn-045.lsst.io/#colorblind-friendly-plots 

134 

135 Returns 

136 ------- 

137 plot_linestyles : `dict` of `str` 

138 Mapping of the LSST bands to line styles. 

139 """ 

140 plot_line_styles = { 

141 "u": "--", 

142 "g": (0, (3, 1, 1, 1)), 

143 "r": "-.", 

144 "i": "-", 

145 "z": (0, (3, 1, 1, 1, 1, 1)), 

146 "y": ":", 

147 } 

148 

149 # [SP-2200]: Restored to using parametric values. 

150 # To avoid matplotlib v3.10 bug (see DM-49724), 

151 # manually iterate over `patches` object returned 

152 # by `plt.hist` when using histtype='step': 

153 # _, _, patches = plt.hist() 

154 # linestyle = plot_line_styles[band] 

155 # for patch in patches: 

156 # patch.set_linestyle(linestyle) 

157 # It seems the bug will be fixed in matplotlib v.3.10.2, 

158 # see DM-49724[TODO] 

159 return plot_line_styles