Coverage for python / lsst / analysis / tools / actions / plot / elements / scatterElement.py: 68%

20 statements  

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

1# This file is part of analysis_tools. 

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 

22from __future__ import annotations 

23 

24__all__ = ("ScatterElement",) 

25 

26from typing import TYPE_CHECKING 

27 

28from lsst.pex.config import Field 

29 

30from ....interfaces import PlotElement 

31 

32if TYPE_CHECKING: 

33 from matplotlib.axes import Axes 

34 

35 from lsst.analysis.tools.interfaces import KeyedData 

36 

37 

38class ScatterElement(PlotElement): 

39 """Configuration options for scatter plot plot elements. 

40 

41 Attributes 

42 ---------- 

43 xKey : `~lsst.pex.config.Field` 

44 X-axis data vector key. If None, the index of the values are used. 

45 valsKey : `~lsst.pex.config.Field` 

46 Y-axis data vector key. Plotted against the index if no xKey is given. 

47 color : `~lsst.pex.config.Field` 

48 Color. 

49 marker : `~lsst.pex.config.Field` 

50 Point marker. 

51 linestyle : `~lsst.pex.config.Field` 

52 Linestyle. 

53 linewidth : `~lsst.pex.config.Field` 

54 Linewidth. 

55 markersize : `~lsst.pex.config.Field` 

56 Markersize. 

57 """ 

58 

59 xKey = Field[str]( 

60 doc="X-axis data vector key. If None, the index of the values are used.", 

61 optional=True, 

62 ) 

63 valsKey = Field[str]( 

64 doc="Y-axis data vector key. Plotted against the index if no xKey is given.", 

65 default="value", 

66 ) 

67 color = Field[str]( 

68 doc="Color", 

69 optional=True, 

70 ) 

71 marker = Field[str]( 

72 doc="Point marker", 

73 optional=True, 

74 ) 

75 linestyle = Field[str]( 

76 doc="Linestyle", 

77 optional=True, 

78 ) 

79 linewidth = Field[float]( 

80 doc="Linewidth", 

81 optional=True, 

82 ) 

83 markersize = Field[float]( 

84 doc="Markersize", 

85 optional=True, 

86 ) 

87 

88 def __call__(self, data: KeyedData, ax: Axes, **kwargs) -> KeyedData: 

89 """Plot y versus x as lines and/or markers. 

90 

91 Parameters 

92 ---------- 

93 data : `~lsst.analysis.tools.interfaces.KeyedData` 

94 Keyed data containing the data to plot. 

95 ax : `~matplotlib.axes.Axes` 

96 Axes to plot on. 

97 

98 Returns 

99 ------- 

100 data : `~lsst.analysis.tools.interfaces.KeyedData` 

101 Data used for plotting. 

102 """ 

103 self._validateInputs(data) 

104 ax.plot( 

105 data[self.xKey] if self.xKey is not None else range(len(data[self.valsKey])), # type: ignore 

106 data[self.valsKey], # type: ignore 

107 color=self.color if self.color is not None else None, 

108 marker=self.marker if self.marker is not None else ".", 

109 linestyle=self.linestyle if self.linestyle is not None else None, 

110 linewidth=self.linewidth if self.linewidth is not None else None, 

111 markersize=self.markersize if self.markersize is not None else None, 

112 ) 

113 

114 return data 

115 

116 def _validateInputs(self, data: KeyedData) -> None: 

117 """Validate inputs. 

118 

119 Parameters 

120 ---------- 

121 data : `~lsst.analysis.tools.interfaces.KeyedData` 

122 Data to plot. 

123 """ 

124 if self.xKey is not None and len(data[self.xKey]) != len(data[self.valsKey]): # type: ignore 

125 raise ValueError("X and Y vector inputs must be the same length.")