Coverage for tests/test_matplotlib_figures.py: 98%

85 statements  

« prev     ^ index     » next       coverage.py v7.17.0, created at 2026-11-03 00:27 +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# This program is free software: you can redistribute it and/or modify 

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

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

12# (at your option) any later version. 

13# 

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

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

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

17# GNU General Public License for more details. 

18# 

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

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

21 

22import unittest 

23 

24import lsst.utils.tests 

25from lsst.utils.plotting import ( 

26 accent_color, 

27 divergent_cmap, 

28 galaxies_cmap, 

29 galaxies_color, 

30 get_band_dicts, 

31 get_multiband_plot_colors, 

32 get_multiband_plot_linestyles, 

33 get_multiband_plot_symbols, 

34 make_figure, 

35 mk_colormap, 

36 set_rubin_plotstyle, 

37 sso_cmap, 

38 sso_color, 

39 stars_cmap, 

40 stars_color, 

41) 

42 

43try: 

44 from matplotlib import rcParams 

45 from matplotlib.backends.backend_agg import FigureCanvasAgg 

46 from matplotlib.figure import Figure 

47except ImportError: 

48 Figure = None 

49 

50 

51@unittest.skipIf(Figure is None, "matplotlib is not available.") 

52class MakeFigureTestCase(unittest.TestCase): 

53 """Tests for make_figure.""" 

54 

55 def testMakeFigure(self): 

56 with lsst.utils.tests.getTempFilePath(".png") as tmpFile: 

57 fig = make_figure(figsize=(10, 6), dpi=300) 

58 

59 self.assertIsInstance(fig, Figure) 

60 self.assertIsInstance(fig.canvas, FigureCanvasAgg) 

61 self.assertEqual(fig.get_figwidth(), 10) 

62 self.assertEqual(fig.get_figheight(), 6) 

63 self.assertEqual(fig.get_dpi(), 300) 

64 

65 bands = ["u", "g", "r", "i", "z", "y"] 

66 

67 ax = fig.add_subplot(111) 

68 for dark_background in True, False: 

69 colors = get_multiband_plot_colors(dark_background=dark_background) 

70 symbols = get_multiband_plot_symbols() 

71 linestyles = get_multiband_plot_linestyles() 

72 

73 ax = fig.add_subplot(111) 

74 for band in bands: 

75 ax.plot( 

76 [0, 1], 

77 [0, 1], 

78 c=colors[band], 

79 marker=symbols[band], 

80 linestyle=linestyles[band], 

81 ) 

82 

83 fig.savefig(tmpFile) 

84 

85 

86@unittest.skipIf(Figure is None, "matplotlib is not available.") 

87class PublicationPlotsTestCase(unittest.TestCase): 

88 """Tests for publication_plots.""" 

89 

90 def testMplStyle(self): 

91 # Set the plot style 

92 set_rubin_plotstyle() 

93 # Confirm that the settings took effect by checking one of them 

94 self.assertEqual(rcParams["errorbar.capsize"], 3.0) 

95 # Set the plot style with the "lowres" kwarg 

96 set_rubin_plotstyle(lowres=True) 

97 # Confirm that the settings took effect by checking dpi 

98 self.assertEqual(rcParams["figure.dpi"], 100.0) 

99 

100 def testMultibandPlotColors(self): 

101 bands_dict = get_band_dicts() 

102 self.assertEqual(bands_dict["colors"]["r"], "#B52626") 

103 self.assertEqual(bands_dict["colors_black"]["r"], "#ff7e00") 

104 self.assertEqual(bands_dict["symbols"]["r"], "v") 

105 self.assertEqual(bands_dict["line_styles"]["r"], "-.") 

106 

107 def testStarColors(self): 

108 with lsst.utils.tests.getTempFilePath(".png") as tmpFile: 

109 fig = make_figure() 

110 ax = fig.add_subplot(111) 

111 xs = [0, 1] 

112 ys = [0, 1] 

113 for cmap in [stars_cmap(), stars_cmap(single_color=True)]: 

114 ax.hexbin(xs, ys, cmap=cmap) 

115 ax.axhline(0, color=stars_color()) 

116 fig.savefig(tmpFile) 

117 

118 def testGalaxyColor(self): 

119 with lsst.utils.tests.getTempFilePath(".png") as tmpFile: 

120 fig = make_figure() 

121 ax = fig.add_subplot(111) 

122 xs = [0, 1] 

123 ys = [0, 1] 

124 for cmap in [galaxies_cmap(), galaxies_cmap(single_color=True)]: 

125 ax.hexbin(xs, ys, cmap=cmap) 

126 ax.axhline(0, color=galaxies_color()) 

127 fig.savefig(tmpFile) 

128 

129 def testDivergentColor(self): 

130 with lsst.utils.tests.getTempFilePath(".png") as tmpFile: 

131 fig = make_figure() 

132 ax = fig.add_subplot(111) 

133 xs = [0, 1] 

134 ys = [0, 1] 

135 ax.hexbin(xs, ys, cmap=divergent_cmap()) 

136 ax.axhline(0, color=accent_color()) 

137 fig.savefig(tmpFile) 

138 

139 def testSSOColor(self): 

140 with lsst.utils.tests.getTempFilePath(".png") as tmpFile: 

141 fig = make_figure() 

142 ax = fig.add_subplot(111) 

143 xs = [0, 1] 

144 ys = [0, 1] 

145 for cmap in [sso_cmap(), sso_cmap(single_color=True)]: 

146 ax.hexbin(xs, ys, cmap=cmap) 

147 ax.axhline(0, color=sso_color()) 

148 fig.savefig(tmpFile) 

149 

150 def testMkColormap(self): 

151 mk_colormap(["#fde725", "#21918c", "#440154"]) 

152 mk_colormap(["#fde725", "#5ec962", "#21918c", "#3b528b"]) 

153 mk_colormap(["#fde725", "#5ec962", "#21918c", "#3b528b", "#440154"])