Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0f7770a

Browse files
committed
Style fixes.
... mostly found by pylint. It did catch two tests in test_colorbar with the same name, one shadowing the other...
1 parent 0290a7e commit 0f7770a

File tree

10 files changed

+144
-271
lines changed

10 files changed

+144
-271
lines changed

lib/matplotlib/_constrained_layout.py

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -99,44 +99,42 @@ def do_constrained_layout(fig, renderer, h_pad, w_pad,
9999
100100
"""
101101

102-
''' Steps:
103-
104-
1. get a list of unique gridspecs in this figure. Each gridspec will be
105-
constrained separately.
106-
2. Check for gaps in the gridspecs. i.e. if not every axes slot in the
107-
gridspec has been filled. If empty, add a ghost axis that is made so
108-
that it cannot be seen (though visible=True). This is needed to make
109-
a blank spot in the layout.
110-
3. Compare the tight_bbox of each axes to its `position`, and assume that
111-
the difference is the space needed by the elements around the edge of
112-
the axes (decorations) like the title, ticklabels, x-labels, etc. This
113-
can include legends who overspill the axes boundaries.
114-
4. Constrain gridspec elements to line up:
115-
a) if colnum0 != colnumC, the two subplotspecs are stacked next to
116-
each other, with the appropriate order.
117-
b) if colnum0 == colnumC, line up the left or right side of the
118-
_poslayoutbox (depending if it is the min or max num that is equal).
119-
c) do the same for rows...
120-
5. The above doesn't constrain relative sizes of the _poslayoutboxes at
121-
all, and indeed zero-size is a solution that the solver often finds more
122-
convenient than expanding the sizes. Right now the solution is to compare
123-
subplotspec sizes (i.e. drowsC and drows0) and constrain the larger
124-
_poslayoutbox to be larger than the ratio of the sizes. i.e. if drows0 >
125-
drowsC, then ax._poslayoutbox > axc._poslayoutbox * drowsC / drows0. This
126-
works fine *if* the decorations are similar between the axes. If the
127-
larger subplotspec has much larger axes decorations, then the constraint
128-
above is incorrect.
129-
130-
We need the greater than in the above, in general, rather than an equals
131-
sign. Consider the case of the left column having 2 rows, and the right
132-
column having 1 row. We want the top and bottom of the _poslayoutboxes to
133-
line up. So that means if there are decorations on the left column axes
134-
they will be smaller than half as large as the right hand axis.
135-
136-
This can break down if the decoration size for the right hand axis (the
137-
margins) is very large. There must be a math way to check for this case.
138-
139-
'''
102+
# Steps:
103+
#
104+
# 1. get a list of unique gridspecs in this figure. Each gridspec will be
105+
# constrained separately.
106+
# 2. Check for gaps in the gridspecs. i.e. if not every axes slot in the
107+
# gridspec has been filled. If empty, add a ghost axis that is made so
108+
# that it cannot be seen (though visible=True). This is needed to make
109+
# a blank spot in the layout.
110+
# 3. Compare the tight_bbox of each axes to its `position`, and assume that
111+
# the difference is the space needed by the elements around the edge of
112+
# the axes (decorations) like the title, ticklabels, x-labels, etc. This
113+
# can include legends who overspill the axes boundaries.
114+
# 4. Constrain gridspec elements to line up:
115+
# a) if colnum0 != colnumC, the two subplotspecs are stacked next to
116+
# each other, with the appropriate order.
117+
# b) if colnum0 == colnumC, line up the left or right side of the
118+
# _poslayoutbox (depending if it is the min or max num that is equal).
119+
# c) do the same for rows...
120+
# 5. The above doesn't constrain relative sizes of the _poslayoutboxes
121+
# at all, and indeed zero-size is a solution that the solver often finds
122+
# more convenient than expanding the sizes. Right now the solution is to
123+
# compare subplotspec sizes (i.e. drowsC and drows0) and constrain the
124+
# larger _poslayoutbox to be larger than the ratio of the sizes. i.e. if
125+
# drows0 > drowsC, then ax._poslayoutbox > axc._poslayoutbox*drowsC/drows0.
126+
# This works fine *if* the decorations are similar between the axes.
127+
# If the larger subplotspec has much larger axes decorations, then the
128+
# constraint above is incorrect.
129+
#
130+
# We need the greater than in the above, in general, rather than an equals
131+
# sign. Consider the case of the left column having 2 rows, and the right
132+
# column having 1 row. We want the top and bottom of the _poslayoutboxes
133+
# to line up. So that means if there are decorations on the left column
134+
# axes they will be smaller than half as large as the right hand axis.
135+
#
136+
# This can break down if the decoration size for the right hand axis (the
137+
# margins) is very large. There must be a math way to check for this case.
140138

141139
invTransFig = fig.transFigure.inverted().transform_bbox
142140

lib/matplotlib/_layoutbox.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -623,29 +623,21 @@ def match_margins(boxes, levels=1):
623623

624624

625625
def seq_id():
626-
'''
627-
Generate a short sequential id for layoutbox objects...
628-
'''
629-
630-
global _layoutboxobjnum
631-
632-
return ('%06d' % (next(_layoutboxobjnum)))
626+
"""Generate a short sequential id for layoutbox objects."""
627+
return '%06d' % next(_layoutboxobjnum)
633628

634629

635630
def print_children(lb):
636-
'''
637-
Print the children of the layoutbox
638-
'''
631+
"""Print the children of the layoutbox."""
639632
print(lb)
640633
for child in lb.children:
641634
print_children(child)
642635

643636

644637
def nonetree(lb):
645-
'''
646-
Make all elements in this tree none... This signals not to do any more
647-
layout.
648-
'''
638+
"""
639+
Make all elements in this tree None, signalling not to do any more layout.
640+
"""
649641
if lb is not None:
650642
if lb.parent is None:
651643
# Clear the solver. Hopefully this garbage collects.

lib/matplotlib/contour.py

Lines changed: 29 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -132,18 +132,14 @@ def clabel(self, levels=None, *,
132132
A list of `.Text` instances for the labels.
133133
"""
134134

135-
"""
136-
NOTES on how this all works:
137-
138-
clabel basically takes the input arguments and uses them to
139-
add a list of "label specific" attributes to the ContourSet
140-
object. These attributes are all of the form label* and names
141-
should be fairly self explanatory.
142-
143-
Once these attributes are set, clabel passes control to the
144-
labels method (case of automatic label placement) or
145-
`BlockingContourLabeler` (case of manual label placement).
146-
"""
135+
# clabel basically takes the input arguments and uses them to
136+
# add a list of "label specific" attributes to the ContourSet
137+
# object. These attributes are all of the form label* and names
138+
# should be fairly self explanatory.
139+
#
140+
# Once these attributes are set, clabel passes control to the
141+
# labels method (case of automatic label placement) or
142+
# `BlockingContourLabeler` (case of manual label placement).
147143

148144
self.labelFmt = fmt
149145
self._use_clabeltext = use_clabeltext
@@ -1496,12 +1492,8 @@ def _contour_args(self, args, kwargs):
14961492

14971493
def _check_xyz(self, args, kwargs):
14981494
"""
1499-
For functions like contour, check that the dimensions
1500-
of the input arrays match; if x and y are 1D, convert
1501-
them to 2D using meshgrid.
1502-
1503-
Possible change: I think we should make and use an ArgumentError
1504-
Exception class (here and elsewhere).
1495+
Check that the shapes of the input arrays match; if x and y are 1D,
1496+
convert them to 2D using meshgrid.
15051497
"""
15061498
x, y = args[:2]
15071499
kwargs = self.ax._process_unit_info(xdata=x, ydata=y, kwargs=kwargs)
@@ -1513,39 +1505,34 @@ def _check_xyz(self, args, kwargs):
15131505
z = ma.asarray(args[2], dtype=np.float64)
15141506

15151507
if z.ndim != 2:
1516-
raise TypeError("Input z must be a 2D array.")
1517-
elif z.shape[0] < 2 or z.shape[1] < 2:
1518-
raise TypeError("Input z must be at least a 2x2 array.")
1519-
else:
1520-
Ny, Nx = z.shape
1508+
raise TypeError(f"Input z must be 2D, not {z.ndim}D")
1509+
if z.shape[0] < 2 or z.shape[1] < 2:
1510+
raise TypeError(f"Input z must be at least a (2, 2) shaped array, "
1511+
f"but has shape {z.shape}")
1512+
Ny, Nx = z.shape
15211513

15221514
if x.ndim != y.ndim:
1523-
raise TypeError("Number of dimensions of x and y should match.")
1524-
1515+
raise TypeError(f"Number of dimensions of x ({x.ndim}) and y "
1516+
f"({y.ndim}) do not match")
15251517
if x.ndim == 1:
1526-
15271518
nx, = x.shape
15281519
ny, = y.shape
1529-
15301520
if nx != Nx:
1531-
raise TypeError("Length of x must be number of columns in z.")
1532-
1521+
raise TypeError(f"Length of x ({nx}) must match number of "
1522+
f"columns in z ({Nx})")
15331523
if ny != Ny:
1534-
raise TypeError("Length of y must be number of rows in z.")
1535-
1524+
raise TypeError(f"Length of y ({ny}) must match number of "
1525+
f"rows in z ({Ny})")
15361526
x, y = np.meshgrid(x, y)
1537-
15381527
elif x.ndim == 2:
1539-
15401528
if x.shape != z.shape:
1541-
raise TypeError("Shape of x does not match that of z: found "
1542-
"{0} instead of {1}.".format(x.shape, z.shape))
1543-
1529+
raise TypeError(
1530+
f"Shapes of x {x.shape} and z {z.shape} do not match")
15441531
if y.shape != z.shape:
1545-
raise TypeError("Shape of y does not match that of z: found "
1546-
"{0} instead of {1}.".format(y.shape, z.shape))
1532+
raise TypeError(
1533+
f"Shapes of y {y.shape} and z {z.shape} do not match")
15471534
else:
1548-
raise TypeError("Inputs x and y must be 1D or 2D.")
1535+
raise TypeError(f"Inputs x and y must be 1D or 2D, not {x.ndim}D")
15491536

15501537
return x, y, z
15511538

@@ -1563,9 +1550,10 @@ def _initialize_x_y(self, z):
15631550
will give the minimum and maximum values of x and y.
15641551
"""
15651552
if z.ndim != 2:
1566-
raise TypeError("Input must be a 2D array.")
1553+
raise TypeError(f"Input z must be 2D, not {z.ndim}D")
15671554
elif z.shape[0] < 2 or z.shape[1] < 2:
1568-
raise TypeError("Input z must be at least a 2x2 array.")
1555+
raise TypeError(f"Input z must be at least a (2, 2) shaped array, "
1556+
f"but has shape {z.shape}")
15691557
else:
15701558
Ny, Nx = z.shape
15711559
if self.origin is None: # Not for image-matching.

lib/matplotlib/legend.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,25 +74,18 @@ def artist_picker(self, legend, evt):
7474
return self.legend.contains(evt)
7575

7676
def finalize_offset(self):
77-
loc_in_canvas = self.get_loc_in_canvas()
78-
79-
if self._update == "loc":
80-
self._update_loc(loc_in_canvas)
81-
elif self._update == "bbox":
82-
self._update_bbox_to_anchor(loc_in_canvas)
83-
else:
84-
raise RuntimeError("update parameter '%s' is not supported." %
85-
self.update)
77+
update_method = cbook._check_getitem(
78+
{"loc": self._update_loc, "bbox": self._bbox_to_anchor},
79+
update=self._update)
80+
update_method(self.get_loc_in_canvas())
8681

8782
def _update_loc(self, loc_in_canvas):
8883
bbox = self.legend.get_bbox_to_anchor()
89-
9084
# if bbox has zero width or height, the transformation is
91-
# ill-defined. Fall back to the defaul bbox_to_anchor.
85+
# ill-defined. Fall back to the default bbox_to_anchor.
9286
if bbox.width == 0 or bbox.height == 0:
9387
self.legend.set_bbox_to_anchor(None)
9488
bbox = self.legend.get_bbox_to_anchor()
95-
9689
_bbox_transform = BboxTransformFrom(bbox)
9790
self.legend._loc = tuple(_bbox_transform.transform(loc_in_canvas))
9891

lib/matplotlib/tests/test_animation.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import weakref
66

77
import numpy as np
8-
from pathlib import Path
98
import pytest
109

1110
import matplotlib as mpl

lib/matplotlib/tests/test_colorbar.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ def test_colorbar_scale_reset():
505505
assert cbar.ax.yaxis.get_scale() == 'linear'
506506

507507

508-
def test_colorbar_get_ticks():
508+
def test_colorbar_get_ticks_2():
509509
with rc_context({'_internal.classic_mode': False}):
510510

511511
fig, ax = plt.subplots()

0 commit comments

Comments
 (0)