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

Skip to content

Commit 0e339a1

Browse files
committed
Made colorbar.py accept numpy array input, compatible with output from subplots
1 parent b07fbb8 commit 0e339a1

File tree

1 file changed

+32
-60
lines changed

1 file changed

+32
-60
lines changed

lib/matplotlib/colorbar.py

Lines changed: 32 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
import matplotlib.patches as mpatches
4040
import matplotlib.path as mpath
4141
import matplotlib.ticker as ticker
42-
import matplotlib.transforms as mtransforms
42+
import matplotlib.transforms as mtrans
4343

4444
from matplotlib import docstring
4545

@@ -256,8 +256,6 @@ class ColorbarBase(cm.ScalarMappable):
256256
'min': slice(1, None),
257257
'max': slice(0, -1)}
258258

259-
n_rasterize = 50 # rasterize solids if number of colors >= n_rasterize
260-
261259
def __init__(self, ax, cmap=None,
262260
norm=None,
263261
alpha=None,
@@ -311,13 +309,10 @@ def __init__(self, ax, cmap=None,
311309
self.locator = ticks # Handle default in _ticker()
312310
if format is None:
313311
if isinstance(self.norm, colors.LogNorm):
314-
self.formatter = ticker.LogFormatterSciNotation()
315-
elif isinstance(self.norm, colors.SymLogNorm):
316-
self.formatter = ticker.LogFormatterSciNotation(
317-
linthresh=self.norm.linthresh)
312+
self.formatter = ticker.LogFormatterMathtext()
318313
else:
319314
self.formatter = ticker.ScalarFormatter()
320-
elif isinstance(format, six.string_types):
315+
elif cbook.is_string_like(format):
321316
self.formatter = ticker.FormatStrFormatter(format)
322317
else:
323318
self.formatter = format # Assume it is a Formatter
@@ -344,7 +339,6 @@ def draw_all(self):
344339
Calculate any free parameters based on the current cmap and norm,
345340
and do all the drawing.
346341
'''
347-
348342
self._process_values()
349343
self._find_range()
350344
X, Y = self._mesh()
@@ -400,10 +394,6 @@ def set_ticks(self, ticks, update_ticks=True):
400394
self.update_ticks()
401395
self.stale = True
402396

403-
def get_ticks(self, minor=False):
404-
"""Return the x ticks as a list of locations"""
405-
return self._tick_data_values
406-
407397
def set_ticklabels(self, ticklabels, update_ticks=True):
408398
"""
409399
set tick labels. Tick labels are updated immediately unless
@@ -509,10 +499,10 @@ def _add_solids(self, X, Y, C):
509499
# Save, set, and restore hold state to keep pcolor from
510500
# clearing the axes. Ordinarily this will not be needed,
511501
# since the axes object should already have hold set.
512-
_hold = self.ax._hold
513-
self.ax._hold = True
502+
_hold = self.ax.ishold()
503+
self.ax.hold(True)
514504
col = self.ax.pcolormesh(*args, **kw)
515-
self.ax._hold = _hold
505+
self.ax.hold(_hold)
516506
#self.add_observer(col) # We should observe, not be observed...
517507

518508
if self.solids is not None:
@@ -527,8 +517,6 @@ def _add_solids(self, X, Y, C):
527517
colors=(mpl.rcParams['axes.edgecolor'],),
528518
linewidths=linewidths)
529519
self.ax.add_collection(self.dividers)
530-
elif len(self._y) >= self.n_rasterize:
531-
self.solids.set_rasterized(True)
532520

533521
def add_lines(self, levels, colors, linewidths, erase=True):
534522
'''
@@ -582,23 +570,13 @@ def _ticker(self):
582570
b = self.norm.boundaries
583571
locator = ticker.FixedLocator(b, nbins=10)
584572
elif isinstance(self.norm, colors.LogNorm):
585-
locator = ticker.LogLocator(subs='all')
586-
elif isinstance(self.norm, colors.SymLogNorm):
587-
# The subs setting here should be replaced
588-
# by logic in the locator.
589-
locator = ticker.SymmetricalLogLocator(
590-
subs=np.arange(1, 10),
591-
linthresh=self.norm.linthresh,
592-
base=10)
573+
locator = ticker.LogLocator()
593574
else:
594-
if mpl.rcParams['_internal.classic_mode']:
595-
locator = ticker.MaxNLocator()
596-
else:
597-
locator = ticker.AutoLocator()
575+
locator = ticker.MaxNLocator()
598576
else:
599577
b = self._boundaries[self._inside]
600578
locator = ticker.FixedLocator(b, nbins=10)
601-
if isinstance(self.norm, colors.NoNorm) and self.boundaries is None:
579+
if isinstance(self.norm, colors.NoNorm):
602580
intv = self._values[0], self._values[-1]
603581
else:
604582
intv = self.vmin, self.vmax
@@ -616,7 +594,6 @@ def _ticker(self):
616594
else:
617595
eps = (intv[1] - intv[0]) * 1e-10
618596
b = b[(b <= intv[1] + eps) & (b >= intv[0] - eps)]
619-
self._tick_data_values = b
620597
ticks = self._locate(b)
621598
formatter.set_locs(b)
622599
ticklabels = [formatter(t, i) for i, t in enumerate(b)]
@@ -687,24 +664,15 @@ def _process_values(self, b=None):
687664
self.norm.vmin = 0
688665
self.norm.vmax = 1
689666

690-
self.norm.vmin, self.norm.vmax = mtransforms.nonsingular(
691-
self.norm.vmin,
692-
self.norm.vmax,
693-
expander=0.1)
667+
self.norm.vmin, self.norm.vmax = mtrans.nonsingular(self.norm.vmin,
668+
self.norm.vmax,
669+
expander=0.1)
694670

695671
b = self.norm.inverse(self._uniform_y(self.cmap.N + 1))
696-
697-
if isinstance(self.norm, colors.LogNorm):
698-
# If using a lognorm, ensure extensions don't go negative
699-
if self._extend_lower():
700-
b[0] = 0.9 * b[0]
701-
if self._extend_upper():
702-
b[-1] = 1.1 * b[-1]
703-
else:
704-
if self._extend_lower():
705-
b[0] = b[0] - 1
706-
if self._extend_upper():
707-
b[-1] = b[-1] + 1
672+
if self._extend_lower():
673+
b[0] = b[0] - 1
674+
if self._extend_upper():
675+
b[-1] = b[-1] + 1
708676
self._process_values(b)
709677

710678
def _find_range(self):
@@ -1050,7 +1018,9 @@ def make_axes(parents, location=None, orientation=None, fraction=0.15,
10501018
shrink=1.0, aspect=20, **kw):
10511019
'''
10521020
Resize and reposition parent axes, and return a child
1053-
axes suitable for a colorbar.
1021+
axes suitable for a colorbar::
1022+
1023+
cax, kw = make_axes(parent, **kw)
10541024
10551025
Keyword arguments may include the following (with defaults):
10561026
@@ -1117,18 +1087,18 @@ def make_axes(parents, location=None, orientation=None, fraction=0.15,
11171087
parent_anchor = kw.pop('panchor', loc_settings['panchor'])
11181088
pad = kw.pop('pad', loc_settings['pad'])
11191089

1120-
# turn parents into a list if it is not already
1121-
if not isinstance(parents, (list, tuple)):
1122-
parents = [parents]
1090+
# turn parents into a list if it is not already. Using numpy because
1091+
# `fig.subplots` returns an ndarray...
1092+
parents = np.atleast_1d(parents).ravel().tolist()
11231093

11241094
fig = parents[0].get_figure()
11251095
if not all(fig is ax.get_figure() for ax in parents):
11261096
raise ValueError('Unable to create a colorbar axes as not all '
11271097
'parents share the same figure.')
11281098

11291099
# take a bounding box around all of the given axes
1130-
parents_bbox = mtransforms.Bbox.union(
1131-
[ax.get_position(original=True).frozen() for ax in parents])
1100+
parents_bbox = mtrans.Bbox.union([ax.get_position(original=True).frozen()
1101+
for ax in parents])
11321102

11331103
pb = parents_bbox
11341104
if location in ('left', 'right'):
@@ -1149,12 +1119,12 @@ def make_axes(parents, location=None, orientation=None, fraction=0.15,
11491119

11501120
# define a transform which takes us from old axes coordinates to
11511121
# new axes coordinates
1152-
shrinking_trans = mtransforms.BboxTransform(parents_bbox, pb1)
1122+
shrinking_trans = mtrans.BboxTransform(parents_bbox, pb1)
11531123

11541124
# transform each of the axes in parents using the new transform
11551125
for ax in parents:
11561126
new_posn = shrinking_trans.transform(ax.get_position())
1157-
new_posn = mtransforms.Bbox(new_posn)
1127+
new_posn = mtrans.Bbox(new_posn)
11581128
ax.set_position(new_posn)
11591129
if parent_anchor is not False:
11601130
ax.set_anchor(parent_anchor)
@@ -1184,7 +1154,9 @@ def make_axes_gridspec(parent, **kw):
11841154
of the parent with a new one.
11851155
11861156
While this function is meant to be compatible with *make_axes*,
1187-
there could be some minor differences.
1157+
there could be some minor differences.::
1158+
1159+
cax, kw = make_axes_gridspec(parent, **kw)
11881160
11891161
Keyword arguments may include the following (with defaults):
11901162
@@ -1287,8 +1259,8 @@ def _add_solids(self, X, Y, C):
12871259
# Save, set, and restore hold state to keep pcolor from
12881260
# clearing the axes. Ordinarily this will not be needed,
12891261
# since the axes object should already have hold set.
1290-
_hold = self.ax._hold
1291-
self.ax._hold = True
1262+
_hold = self.ax.ishold()
1263+
self.ax.hold(True)
12921264

12931265
kw = {'alpha': self.alpha, }
12941266

@@ -1334,7 +1306,7 @@ def _add_solids(self, X, Y, C):
13341306
linewidths=(0.5 * mpl.rcParams['axes.linewidth'],))
13351307
self.ax.add_collection(self.dividers)
13361308

1337-
self.ax._hold = _hold
1309+
self.ax.hold(_hold)
13381310

13391311

13401312
def colorbar_factory(cax, mappable, **kwargs):

0 commit comments

Comments
 (0)