From 70e2bd692fbe86ed792c30820179e0c46d1d7c4c Mon Sep 17 00:00:00 2001 From: Roman Yurchak Date: Sun, 25 Nov 2018 09:24:22 +0100 Subject: [PATCH 1/5] Use list comprehension --- lib/matplotlib/_pylab_helpers.py | 5 +---- lib/matplotlib/backend_bases.py | 8 ++++---- lib/matplotlib/backends/backend_cairo.py | 8 ++++---- lib/matplotlib/backends/backend_ps.py | 5 +---- lib/matplotlib/backends/backend_wx.py | 6 ++---- lib/matplotlib/collections.py | 4 +--- lib/matplotlib/legend_handler.py | 6 ++---- lib/mpl_toolkits/mplot3d/art3d.py | 20 +++++++------------- 8 files changed, 22 insertions(+), 40 deletions(-) diff --git a/lib/matplotlib/_pylab_helpers.py b/lib/matplotlib/_pylab_helpers.py index 77bd7d2135b1..45574b525cac 100644 --- a/lib/matplotlib/_pylab_helpers.py +++ b/lib/matplotlib/_pylab_helpers.py @@ -113,10 +113,7 @@ def set_active(cls, manager): Make the figure corresponding to *manager* the active one. """ oldQue = cls._activeQue[:] - cls._activeQue = [] - for m in oldQue: - if m != manager: - cls._activeQue.append(m) + cls._activeQue = [m for m in oldQue if m != manager] cls._activeQue.append(manager) cls.figs[manager.num] = manager diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index ce095ab52214..74903d5d5b62 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -227,10 +227,10 @@ def draw_path_collection(self, gc, master_transform, paths, all_transforms, recommended to use those generators, so that changes to the behavior of :meth:`draw_path_collection` can be made globally. """ - path_ids = [] - for path, transform in self._iter_collection_raw_paths( - master_transform, paths, all_transforms): - path_ids.append((path, transforms.Affine2D(transform))) + path_ids = [ + (path, transforms.Affine2D(transform)) + for path, transform in self._iter_collection_raw_paths( + master_transform, paths, all_transforms)] for xo, yo, path_id, gc0, rgbFace in self._iter_collection( gc, master_transform, all_transforms, path_ids, offsets, diff --git a/lib/matplotlib/backends/backend_cairo.py b/lib/matplotlib/backends/backend_cairo.py index 2be4828becac..39f36f3646f8 100644 --- a/lib/matplotlib/backends/backend_cairo.py +++ b/lib/matplotlib/backends/backend_cairo.py @@ -291,10 +291,10 @@ def draw_path_collection( offsetTrans, facecolors, edgecolors, linewidths, linestyles, antialiaseds, urls, offset_position): - path_ids = [] - for path, transform in self._iter_collection_raw_paths( - master_transform, paths, all_transforms): - path_ids.append((path, Affine2D(transform))) + path_ids = [ + (path, Affine2D(transform)) + for path, transform in self._iter_collection_raw_paths( + master_transform, paths, all_transforms)] reuse_key = None grouped_draw = [] diff --git a/lib/matplotlib/backends/backend_ps.py b/lib/matplotlib/backends/backend_ps.py index 1bdb1cfe3686..6a8b04c902e7 100644 --- a/lib/matplotlib/backends/backend_ps.py +++ b/lib/matplotlib/backends/backend_ps.py @@ -1097,10 +1097,7 @@ def print_figure_impl(fh): ps_renderer.used_characters.values(): if len(chars): font = get_font(font_filename) - glyph_ids = [] - for c in chars: - gind = font.get_char_index(c) - glyph_ids.append(gind) + glyph_ids = [font.get_char_index(c) for c in chars] fonttype = rcParams['ps.fonttype'] diff --git a/lib/matplotlib/backends/backend_wx.py b/lib/matplotlib/backends/backend_wx.py index 27d7b542b8ef..016feaee5a0c 100644 --- a/lib/matplotlib/backends/backend_wx.py +++ b/lib/matplotlib/backends/backend_wx.py @@ -1439,10 +1439,8 @@ def updateAxes(self, maxAxis): def getActiveAxes(self): """Return a list of the selected axes.""" - active = [] - for i in range(len(self._axisId)): - if self._menu.IsChecked(self._axisId[i]): - active.append(i) + active = [idx for idx, ax_id in enumerate(self._axisId) + if self._menu.IsChecked(ax_id)] return active def updateButtonText(self, lst): diff --git a/lib/matplotlib/collections.py b/lib/matplotlib/collections.py index 576b1bdba25e..835945b8434c 100644 --- a/lib/matplotlib/collections.py +++ b/lib/matplotlib/collections.py @@ -1402,9 +1402,7 @@ def get_positions(self): ''' segments = self.get_segments() pos = 0 if self.is_horizontal() else 1 - positions = [] - for segment in segments: - positions.append(segment[0, pos]) + positions = [segment[0, pos] for segment in segments] return positions def set_positions(self, positions): diff --git a/lib/matplotlib/legend_handler.py b/lib/matplotlib/legend_handler.py index 1af296ac8db5..e6c552008155 100644 --- a/lib/matplotlib/legend_handler.py +++ b/lib/matplotlib/legend_handler.py @@ -606,10 +606,8 @@ def create_artists(self, legend, orig_handle, leg_markerline = Line2D(xdata_marker, ydata[:len(xdata_marker)]) self.update_prop(leg_markerline, markerline, legend) - leg_stemlines = [] - for thisx, thisy in zip(xdata_marker, ydata): - l = Line2D([thisx, thisx], [bottom, thisy]) - leg_stemlines.append(l) + leg_stemlines = [Line2D([thisx, thisx], [bottom, thisy]) + for thisx, thisy in zip(xdata_marker, ydata)] for lm, m in zip(leg_stemlines, stemlines): self.update_prop(lm, m, legend) diff --git a/lib/mpl_toolkits/mplot3d/art3d.py b/lib/mpl_toolkits/mplot3d/art3d.py index 74f48a381251..af74e35ed65b 100644 --- a/lib/mpl_toolkits/mplot3d/art3d.py +++ b/lib/mpl_toolkits/mplot3d/art3d.py @@ -189,14 +189,11 @@ def path_to_3d_segment_with_codes(path, zs=0, zdir='z'): """Convert a path to a 3D segment with path codes.""" zs = np.broadcast_to(zs, len(path)) - seg = [] - codes = [] pathsegs = path.iter_segments(simplify=False, curves=False) - for (((x, y), code), z) in zip(pathsegs, zs): - seg.append((x, y, z)) - codes.append(code) + seg, codes = zip( + *[((x, y, z), code) for (((x, y), code), z) in zip(pathsegs, zs)]) seg3d = [juggle_axes(x, y, z, zdir) for (x, y, z) in seg] - return seg3d, codes + return seg3d, list(codes) def paths_to_3d_segments_with_codes(paths, zs=0, zdir='z'): @@ -205,13 +202,10 @@ def paths_to_3d_segments_with_codes(paths, zs=0, zdir='z'): """ zs = np.broadcast_to(zs, len(paths)) - segments = [] - codes_list = [] - for path, pathz in zip(paths, zs): - segs, codes = path_to_3d_segment_with_codes(path, pathz, zdir) - segments.append(segs) - codes_list.append(codes) - return segments, codes_list + segments, codes_list = zip( + *[path_to_3d_segment_with_codes(path, pathz, zdir) + for path, pathz in zip(paths, zs)]) + return list(segments), list(codes_list) class Line3DCollection(LineCollection): From f92887480bc6b5068adc026bbe12cafa16151ed1 Mon Sep 17 00:00:00 2001 From: Roman Yurchak Date: Sun, 25 Nov 2018 09:29:49 +0100 Subject: [PATCH 2/5] Some dict comprehension --- lib/matplotlib/animation.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/animation.py b/lib/matplotlib/animation.py index b6ed8589b9a3..aaf25d573df4 100644 --- a/lib/matplotlib/animation.py +++ b/lib/matplotlib/animation.py @@ -128,10 +128,9 @@ def ensure_not_dirty(self): def reset_available_writers(self): """Reset the available state of all registered writers""" - self.avail = {} - for name, writerClass in self._registered.items(): - if writerClass.isAvailable(): - self.avail[name] = writerClass + self.avail = {name: writerClass + for name, writerClass in self._registered.items() + if writerClass.isAvailable()} self._dirty = False def list(self): From 602cbd1eb82034750d194d6949cd4a4543e8842d Mon Sep 17 00:00:00 2001 From: Roman Yurchak Date: Sun, 25 Nov 2018 21:24:57 +0100 Subject: [PATCH 3/5] Review comments --- lib/matplotlib/legend_handler.py | 4 ++-- lib/mpl_toolkits/mplot3d/art3d.py | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/matplotlib/legend_handler.py b/lib/matplotlib/legend_handler.py index e6c552008155..5d4e55a351da 100644 --- a/lib/matplotlib/legend_handler.py +++ b/lib/matplotlib/legend_handler.py @@ -606,8 +606,8 @@ def create_artists(self, legend, orig_handle, leg_markerline = Line2D(xdata_marker, ydata[:len(xdata_marker)]) self.update_prop(leg_markerline, markerline, legend) - leg_stemlines = [Line2D([thisx, thisx], [bottom, thisy]) - for thisx, thisy in zip(xdata_marker, ydata)] + leg_stemlines = [Line2D([x, x], [bottom, y]) + for x, y in zip(xdata_marker, ydata)] for lm, m in zip(leg_stemlines, stemlines): self.update_prop(lm, m, legend) diff --git a/lib/mpl_toolkits/mplot3d/art3d.py b/lib/mpl_toolkits/mplot3d/art3d.py index af74e35ed65b..3ed61a844996 100644 --- a/lib/mpl_toolkits/mplot3d/art3d.py +++ b/lib/mpl_toolkits/mplot3d/art3d.py @@ -190,8 +190,8 @@ def path_to_3d_segment_with_codes(path, zs=0, zdir='z'): zs = np.broadcast_to(zs, len(path)) pathsegs = path.iter_segments(simplify=False, curves=False) - seg, codes = zip( - *[((x, y, z), code) for (((x, y), code), z) in zip(pathsegs, zs)]) + seg_codes = [((x, y, z), code) for ((x, y), code), z in zip(pathsegs, zs)] + seg, codes = zip(*seg_codes) seg3d = [juggle_axes(x, y, z, zdir) for (x, y, z) in seg] return seg3d, list(codes) @@ -202,10 +202,10 @@ def paths_to_3d_segments_with_codes(paths, zs=0, zdir='z'): """ zs = np.broadcast_to(zs, len(paths)) - segments, codes_list = zip( - *[path_to_3d_segment_with_codes(path, pathz, zdir) - for path, pathz in zip(paths, zs)]) - return list(segments), list(codes_list) + segments_codes = [path_to_3d_segment_with_codes(path, pathz, zdir) + for path, pathz in zip(paths, zs)] + segments, codes = zip(*segments_codes) + return list(segments), list(codes) class Line3DCollection(LineCollection): From 5441e09d95229dcc795b3eae1450420e70af251e Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Sun, 25 Nov 2018 21:25:24 +0100 Subject: [PATCH 4/5] Update lib/matplotlib/collections.py Co-Authored-By: rth --- lib/matplotlib/collections.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/collections.py b/lib/matplotlib/collections.py index 835945b8434c..d8b74d648982 100644 --- a/lib/matplotlib/collections.py +++ b/lib/matplotlib/collections.py @@ -1402,7 +1402,7 @@ def get_positions(self): ''' segments = self.get_segments() pos = 0 if self.is_horizontal() else 1 - positions = [segment[0, pos] for segment in segments] + return [segment[0, pos] for segment in self.get_segments()] return positions def set_positions(self, positions): From 09478a005d8be1ab1ba9a75382f22be303110353 Mon Sep 17 00:00:00 2001 From: Roman Yurchak Date: Sun, 25 Nov 2018 21:26:45 +0100 Subject: [PATCH 5/5] Fix suggestion --- lib/matplotlib/collections.py | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/matplotlib/collections.py b/lib/matplotlib/collections.py index d8b74d648982..2b0141229094 100644 --- a/lib/matplotlib/collections.py +++ b/lib/matplotlib/collections.py @@ -1403,7 +1403,6 @@ def get_positions(self): segments = self.get_segments() pos = 0 if self.is_horizontal() else 1 return [segment[0, pos] for segment in self.get_segments()] - return positions def set_positions(self, positions): '''