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

Skip to content

Commit 3bbc78a

Browse files
authored
Merge pull request #17754 from anntzer/contour2
Small cleanups to contour() code.
2 parents 0ecd097 + 18179b8 commit 3bbc78a

File tree

2 files changed

+25
-24
lines changed

2 files changed

+25
-24
lines changed

lib/matplotlib/cbook/__init__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,14 +248,22 @@ class silent_list(list):
248248
one will get ::
249249
250250
<a list of 3 Line2D objects>
251+
252+
If ``self.type`` is None, the type name is obtained from the first item in
253+
the list (if any).
251254
"""
255+
252256
def __init__(self, type, seq=None):
253257
self.type = type
254258
if seq is not None:
255259
self.extend(seq)
256260

257261
def __repr__(self):
258-
return '<a list of %d %s objects>' % (len(self), self.type)
262+
if self.type is not None or len(self) != 0:
263+
tp = self.type if self.type is not None else type(self[0]).__name__
264+
return f"<a list of {len(self)} {tp} objects>"
265+
else:
266+
return "<an empty list>"
259267

260268

261269
@deprecated("3.3")

lib/matplotlib/contour.py

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -845,10 +845,8 @@ def __init__(self, ax, *args,
845845
if extend_max:
846846
cmap.set_over(self.colors[-1])
847847

848-
if self.filled:
849-
self.collections = cbook.silent_list('mcoll.PathCollection')
850-
else:
851-
self.collections = cbook.silent_list('mcoll.LineCollection')
848+
self.collections = cbook.silent_list(None)
849+
852850
# label lists must be initialized here
853851
self.labelTexts = []
854852
self.labelCValues = []
@@ -869,53 +867,48 @@ def __init__(self, ax, *args,
869867
if self.filled:
870868
if self.linewidths is not None:
871869
cbook._warn_external('linewidths is ignored by contourf')
872-
873870
# Lower and upper contour levels.
874871
lowers, uppers = self._get_lowers_and_uppers()
875-
876872
# Ensure allkinds can be zipped below.
877873
if self.allkinds is None:
878874
self.allkinds = [None] * len(self.allsegs)
879-
880875
# Default zorder taken from Collection
881876
self._contour_zorder = kwargs.pop('zorder', 1)
882-
for level, level_upper, segs, kinds in \
883-
zip(lowers, uppers, self.allsegs, self.allkinds):
884-
paths = self._make_paths(segs, kinds)
885877

886-
col = mcoll.PathCollection(
887-
paths,
878+
self.collections[:] = [
879+
mcoll.PathCollection(
880+
self._make_paths(segs, kinds),
888881
antialiaseds=(self.antialiased,),
889882
edgecolors='none',
890883
alpha=self.alpha,
891884
transform=self.get_transform(),
892885
zorder=self._contour_zorder)
893-
self.axes.add_collection(col, autolim=False)
894-
self.collections.append(col)
886+
for level, level_upper, segs, kinds
887+
in zip(lowers, uppers, self.allsegs, self.allkinds)]
895888
else:
896-
tlinewidths = self._process_linewidths()
897-
self.tlinewidths = tlinewidths
889+
self.tlinewidths = tlinewidths = self._process_linewidths()
898890
tlinestyles = self._process_linestyles()
899891
aa = self.antialiased
900892
if aa is not None:
901893
aa = (self.antialiased,)
902894
# Default zorder taken from LineCollection
903895
self._contour_zorder = kwargs.pop('zorder', 2)
904-
for level, width, lstyle, segs in \
905-
zip(self.levels, tlinewidths, tlinestyles, self.allsegs):
906-
col = mcoll.LineCollection(
896+
897+
self.collections[:] = [
898+
mcoll.LineCollection(
907899
segs,
908900
antialiaseds=aa,
909901
linewidths=width,
910902
linestyles=[lstyle],
911903
alpha=self.alpha,
912904
transform=self.get_transform(),
913-
zorder=self._contour_zorder)
914-
col.set_label('_nolegend_')
915-
self.axes.add_collection(col, autolim=False)
916-
self.collections.append(col)
905+
zorder=self._contour_zorder,
906+
label='_nolegend_')
907+
for level, width, lstyle, segs
908+
in zip(self.levels, tlinewidths, tlinestyles, self.allsegs)]
917909

918910
for col in self.collections:
911+
self.axes.add_collection(col, autolim=False)
919912
col.sticky_edges.x[:] = [self._mins[0], self._maxs[0]]
920913
col.sticky_edges.y[:] = [self._mins[1], self._maxs[1]]
921914
self.axes.update_datalim([self._mins, self._maxs])

0 commit comments

Comments
 (0)