Coverage for /usr/lib/python3/dist-packages/matplotlib/axes/_subplots.py: 24%

63 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2025-06-14 15:25 +0200

1import matplotlib as mpl 

2from matplotlib import cbook 

3from matplotlib.axes._axes import Axes 

4from matplotlib.gridspec import SubplotSpec 

5 

6 

7class SubplotBase: 

8 """ 

9 Base class for subplots, which are :class:`Axes` instances with 

10 additional methods to facilitate generating and manipulating a set 

11 of :class:`Axes` within a figure. 

12 """ 

13 

14 def __init__(self, fig, *args, **kwargs): 

15 """ 

16 Parameters 

17 ---------- 

18 fig : `matplotlib.figure.Figure` 

19 

20 *args : tuple (*nrows*, *ncols*, *index*) or int 

21 The array of subplots in the figure has dimensions ``(nrows, 

22 ncols)``, and *index* is the index of the subplot being created. 

23 *index* starts at 1 in the upper left corner and increases to the 

24 right. 

25 

26 If *nrows*, *ncols*, and *index* are all single digit numbers, then 

27 *args* can be passed as a single 3-digit number (e.g. 234 for 

28 (2, 3, 4)). 

29 

30 **kwargs 

31 Keyword arguments are passed to the Axes (sub)class constructor. 

32 """ 

33 # _axes_class is set in the subplot_class_factory 

34 self._axes_class.__init__(self, fig, [0, 0, 1, 1], **kwargs) 

35 # This will also update the axes position. 

36 self.set_subplotspec(SubplotSpec._from_subplot_args(fig, args)) 

37 

38 def get_subplotspec(self): 

39 """Return the `.SubplotSpec` instance associated with the subplot.""" 

40 return self._subplotspec 

41 

42 def set_subplotspec(self, subplotspec): 

43 """Set the `.SubplotSpec`. instance associated with the subplot.""" 

44 self._subplotspec = subplotspec 

45 self._set_position(subplotspec.get_position(self.figure)) 

46 

47 def get_gridspec(self): 

48 """Return the `.GridSpec` instance associated with the subplot.""" 

49 return self._subplotspec.get_gridspec() 

50 

51 def label_outer(self): 

52 """ 

53 Only show "outer" labels and tick labels. 

54 

55 x-labels are only kept for subplots on the last row (or first row, if 

56 labels are on the top side); y-labels only for subplots on the first 

57 column (or last column, if labels are on the right side). 

58 """ 

59 self._label_outer_xaxis(check_patch=False) 

60 self._label_outer_yaxis(check_patch=False) 

61 

62 def _label_outer_xaxis(self, *, check_patch): 

63 # see documentation in label_outer. 

64 if check_patch and not isinstance(self.patch, mpl.patches.Rectangle): 

65 return 

66 ss = self.get_subplotspec() 

67 label_position = self.xaxis.get_label_position() 

68 if not ss.is_first_row(): # Remove top label/ticklabels/offsettext. 

69 if label_position == "top": 

70 self.set_xlabel("") 

71 self.xaxis.set_tick_params(which="both", labeltop=False) 

72 if self.xaxis.offsetText.get_position()[1] == 1: 

73 self.xaxis.offsetText.set_visible(False) 

74 if not ss.is_last_row(): # Remove bottom label/ticklabels/offsettext. 

75 if label_position == "bottom": 

76 self.set_xlabel("") 

77 self.xaxis.set_tick_params(which="both", labelbottom=False) 

78 if self.xaxis.offsetText.get_position()[1] == 0: 

79 self.xaxis.offsetText.set_visible(False) 

80 

81 def _label_outer_yaxis(self, *, check_patch): 

82 # see documentation in label_outer. 

83 if check_patch and not isinstance(self.patch, mpl.patches.Rectangle): 

84 return 

85 ss = self.get_subplotspec() 

86 label_position = self.yaxis.get_label_position() 

87 if not ss.is_first_col(): # Remove left label/ticklabels/offsettext. 

88 if label_position == "left": 

89 self.set_ylabel("") 

90 self.yaxis.set_tick_params(which="both", labelleft=False) 

91 if self.yaxis.offsetText.get_position()[0] == 0: 

92 self.yaxis.offsetText.set_visible(False) 

93 if not ss.is_last_col(): # Remove right label/ticklabels/offsettext. 

94 if label_position == "right": 

95 self.set_ylabel("") 

96 self.yaxis.set_tick_params(which="both", labelright=False) 

97 if self.yaxis.offsetText.get_position()[0] == 1: 

98 self.yaxis.offsetText.set_visible(False) 

99 

100 def _make_twin_axes(self, *args, **kwargs): 

101 """Make a twinx axes of self. This is used for twinx and twiny.""" 

102 if 'sharex' in kwargs and 'sharey' in kwargs: 

103 # The following line is added in v2.2 to avoid breaking Seaborn, 

104 # which currently uses this internal API. 

105 if kwargs["sharex"] is not self and kwargs["sharey"] is not self: 

106 raise ValueError("Twinned Axes may share only one axis") 

107 twin = self.figure.add_subplot(self.get_subplotspec(), *args, **kwargs) 

108 self.set_adjustable('datalim') 

109 twin.set_adjustable('datalim') 

110 self._twinned_axes.join(self, twin) 

111 return twin 

112 

113 

114subplot_class_factory = cbook._make_class_factory( 

115 SubplotBase, "{}Subplot", "_axes_class") 

116Subplot = subplot_class_factory(Axes) # Provided for backward compatibility.