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

Skip to content

Commit 3f3065d

Browse files
authored
Merge pull request #10614 from anntzer/stack
Use np.stack instead of list(zip()) in colorbar.py.
2 parents 060ff63 + bed1aa2 commit 3f3065d

File tree

2 files changed

+12
-19
lines changed

2 files changed

+12
-19
lines changed

lib/matplotlib/colorbar.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -557,13 +557,11 @@ def add_lines(self, levels, colors, linewidths, erase=True):
557557
colors = np.asarray(colors)[igood]
558558
if cbook.iterable(linewidths):
559559
linewidths = np.asarray(linewidths)[igood]
560-
N = len(y)
561-
x = np.array([0.0, 1.0])
562-
X, Y = np.meshgrid(x, y)
560+
X, Y = np.meshgrid([0, 1], y)
563561
if self.orientation == 'vertical':
564-
xy = [list(zip(X[i], Y[i])) for i in xrange(N)]
562+
xy = np.stack([X, Y], axis=-1)
565563
else:
566-
xy = [list(zip(Y[i], X[i])) for i in xrange(N)]
564+
xy = np.stack([Y, X], axis=-1)
567565
col = collections.LineCollection(xy, linewidths=linewidths)
568566

569567
if erase and self.lines:

lib/mpl_toolkits/axes_grid1/colorbar.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -565,10 +565,11 @@ def _add_solids(self, X, Y, C):
565565

566566
self.solids = col
567567
if self.drawedges:
568-
self.dividers = collections.LineCollection(self._edges(X,Y),
569-
colors=(mpl.rcParams['axes.edgecolor'],),
570-
linewidths=(0.5*mpl.rcParams['axes.linewidth'],),
571-
)
568+
self.dividers = collections.LineCollection(
569+
self._edges(X,Y),
570+
colors=(mpl.rcParams['axes.edgecolor'],),
571+
linewidths=(0.5*mpl.rcParams['axes.linewidth'],),
572+
)
572573
self.ax.add_collection(self.dividers)
573574
else:
574575
self.dividers = None
@@ -577,22 +578,16 @@ def add_lines(self, levels, colors, linewidths):
577578
'''
578579
Draw lines on the colorbar. It deletes preexisting lines.
579580
'''
580-
del self.lines
581-
582-
N = len(levels)
583-
x = np.array([1.0, 2.0])
584-
X, Y = np.meshgrid(x,levels)
581+
X, Y = np.meshgrid([1, 2], levels)
585582
if self.orientation == 'vertical':
586-
xy = [list(zip(X[i], Y[i])) for i in range(N)]
583+
xy = np.stack([X, Y], axis=-1)
587584
else:
588-
xy = [list(zip(Y[i], X[i])) for i in range(N)]
589-
col = collections.LineCollection(xy, linewidths=linewidths,
590-
)
585+
xy = np.stack([Y, X], axis=-1)
586+
col = collections.LineCollection(xy, linewidths=linewidths)
591587
self.lines = col
592588
col.set_color(colors)
593589
self.ax.add_collection(col)
594590

595-
596591
def _select_locator(self, formatter):
597592
'''
598593
select a suitable locator

0 commit comments

Comments
 (0)