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

Skip to content

Commit 9250eed

Browse files
committed
FIX
1 parent d680c81 commit 9250eed

File tree

2 files changed

+116
-113
lines changed

2 files changed

+116
-113
lines changed

lib/matplotlib/colorbar.py

Lines changed: 116 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ def __init__(self, ax, cmap=None,
393393
_api.check_in_list(
394394
['uniform', 'proportional'], spacing=spacing)
395395

396+
# wrap the axes so that it can be positioned as an inset axes:
396397
ax = ColorbarAxes(ax)
397398
self.ax = ax
398399
# Bind some methods to the axes to warn users against using them.
@@ -477,87 +478,19 @@ def _extend_upper(self):
477478
return self.extend in ('both', 'max')
478479

479480
def _long_axis(self):
481+
"""Return the long axis"""
480482
if self.orientation == 'vertical':
481483
return self.ax.yaxis
482484
else:
483485
return self.ax.xaxis
484486

485487
def _short_axis(self):
488+
"""Return the short axis"""
486489
if self.orientation == 'vertical':
487490
return self.ax.xaxis
488491
else:
489492
return self.ax.yaxis
490493

491-
def _do_extends(self, extendlen):
492-
"""
493-
Make adjustments for the extend triangles (or rectanges)
494-
"""
495-
# extend lengths are fraction of the *inner* part of colorbar,
496-
# not the total colorbar:
497-
el = extendlen[0] if self._extend_lower() else 0
498-
eu = extendlen[1] if self._extend_upper() else 0
499-
tot = eu + el + 1
500-
el = el / tot
501-
eu = eu / tot
502-
width = 1 / tot
503-
504-
bounds = np.array([0.0, 0.0, 1.0, 1.0])
505-
bounds[1] = el
506-
bounds[3] = width
507-
508-
# make the inner axes smaller to make room for the extend rectangle
509-
top = bounds[1] + bounds[3]
510-
if not self.extendrect:
511-
xyout = np.array([[0, bounds[1]], [0.5, 0], [1, bounds[1]],
512-
[1, top], [0.5, 1], [0, top], [0, bounds[1]]])
513-
else:
514-
xyout = np.array([[0, bounds[1]], [0, 0], [1, 0], [1, bounds[1]],
515-
[1, top], [1, 1], [0, 1], [0, top],
516-
[0, bounds[1]]])
517-
if self.orientation == 'horizontal':
518-
bounds = bounds[[1, 0, 3, 2]]
519-
xyout = xyout[:, ::-1]
520-
self.ax._set_inner_bounds(bounds)
521-
522-
if not self.filled:
523-
return xyout
524-
# Make extend triangles or rectangles. These are defined in the
525-
# outer parent axes' co-ordinates:
526-
mappable = getattr(self, 'mappable', None)
527-
if (isinstance(mappable, contour.ContourSet)
528-
and any(hatch is not None for hatch in mappable.hatches)):
529-
hatches = mappable.hatches
530-
else:
531-
hatches = [None]
532-
if self._extend_lower:
533-
534-
if not self.extendrect:
535-
xy = np.array([[0.5, 0], [1, el], [0, el]])
536-
else:
537-
xy = np.array([[0, 0], [1., 0], [1, el], [0, el]])
538-
if self.orientation == 'horizontal':
539-
xy = xy[:, ::-1]
540-
color = self.cmap(self.norm(self._values[0]))
541-
patch = mpatches.PathPatch(
542-
mpath.Path(xy), facecolor=color, linewidth=0,
543-
antialiased=False, transform=self.ax.parent_ax.transAxes,
544-
hatch=hatches[0])
545-
self.ax.parent_ax.add_patch(patch)
546-
if self._extend_upper:
547-
if not self.extendrect:
548-
xy = np.array([[0.5, 1], [1, 1-eu], [0, 1-eu]])
549-
else:
550-
xy = np.array([[0, 1], [1, 1], [1, 1-eu], [0, 1-eu]])
551-
if self.orientation == 'horizontal':
552-
xy = xy[:, ::-1]
553-
color = self.cmap(self.norm(self._values[-1]))
554-
patch = mpatches.PathPatch(
555-
mpath.Path(xy), facecolor=color,
556-
linewidth=0, antialiased=False,
557-
transform=self.ax.parent_ax.transAxes, hatch=hatches[-1])
558-
self.ax.parent_ax.add_patch(patch)
559-
return xyout
560-
561494
def draw_all(self):
562495
"""
563496
Calculate any free parameters based on the current cmap and norm,
@@ -615,48 +548,45 @@ def _get_ticker_locator_formatter(self):
615548
"""
616549
Return the ``locator`` and ``formatter`` of the colorbar.
617550
618-
If they have not been defined (i.e. are *None*), suitable formatter
619-
and locator instances will be created, attached to the respective
620-
attributes and returned.
551+
If they have not been defined (i.e. are *None*), the formatter and
552+
locator are retrieved from the axis, or from the value of the
553+
boundaries for a boundary norm.
554+
555+
Called by update_ticks...
621556
"""
622557
locator = self.locator
623558
formatter = self.formatter
624559
minorlocator = self.minorlocator
625-
if locator is None:
626-
if (self.boundaries is None and
627-
not isinstance(self.norm, colors.BoundaryNorm)):
628-
if locator is None:
629-
locator = self._long_axis().get_major_locator()
630-
if minorlocator is None:
631-
minorlocator = self._long_axis().get_minor_locator()
632-
633-
if isinstance(self.norm, colors.NoNorm):
634-
nv = len(self._values)
635-
base = 1 + int(nv / 10)
636-
locator = ticker.IndexLocator(base=base, offset=0)
637-
elif isinstance(self.norm, colors.BoundaryNorm):
638-
b = self.norm.boundaries
560+
if (self.boundaries is None and
561+
not isinstance(self.norm, colors.BoundaryNorm)):
562+
if locator is None:
563+
locator = self._long_axis().get_major_locator()
564+
if minorlocator is None:
565+
minorlocator = self._long_axis().get_minor_locator()
566+
if isinstance(self.norm, colors.NoNorm):
567+
# default locator:
568+
nv = len(self._values)
569+
base = 1 + int(nv / 10)
570+
locator = ticker.IndexLocator(base=base, offset=0)
571+
elif isinstance(self.norm, colors.BoundaryNorm):
572+
b = self.norm.boundaries
573+
if locator is None:
639574
locator = ticker.FixedLocator(b, nbins=10)
640-
else:
641-
b = self._boundaries[self._inside]
575+
else:
576+
b = self._boundaries[self._inside]
577+
if locator is None:
642578
locator = ticker.FixedLocator(b, nbins=10)
643579

580+
if minorlocator is None:
581+
minorlocator = ticker.NullLocator()
582+
644583
if formatter is None:
645-
if isinstance(self.norm, colors.LogNorm):
646-
formatter = ticker.LogFormatterSciNotation()
647-
elif isinstance(self.norm, colors.SymLogNorm):
648-
formatter = ticker.LogFormatterSciNotation(
649-
linthresh=self.norm.linthresh)
650-
else:
651-
formatter = ticker.ScalarFormatter()
652-
else:
653-
formatter = self.formatter
584+
formatter = self._long_axis().get_major_formatter()
654585

655586
self.locator = locator
656587
self.formatter = formatter
657588
self.minorlocator = minorlocator
658589
_log.debug('locator: %r', locator)
659-
return locator, formatter
660590

661591
def _use_auto_colorbar_locator(self):
662592
"""
@@ -699,18 +629,22 @@ def update_ticks(self):
699629
"""
700630
ax = self.ax
701631
# Get the locator and formatter; defaults to self.locator if not None.
702-
locator, formatter = self._get_ticker_locator_formatter()
703-
long_axis = ax.yaxis if self.orientation == 'vertical' else ax.xaxis
632+
self._get_ticker_locator_formatter()
633+
704634
if self._use_auto_colorbar_locator():
705-
_log.debug('Using auto colorbar locator %r on colorbar', locator)
706-
long_axis.set_major_locator(locator)
707-
long_axis.set_major_formatter(formatter)
635+
_log.debug('Using auto colorbar locator %r on colorbar',
636+
self.locator)
637+
self._long_axis().set_major_locator(self.locator)
638+
self._long_axis().set_minor_locator(self.minorlocator)
639+
self._long_axis().set_major_formatter(self.formatter)
708640
else:
709641
_log.debug('Using fixed locator on colorbar')
710-
ticks, ticklabels, offset_string = self._ticker(locator, formatter)
711-
long_axis.set_ticks(ticks)
712-
long_axis.set_ticklabels(ticklabels)
713-
long_axis.get_major_formatter().set_offset_string(offset_string)
642+
ticks, ticklabels, offset_string = self._ticker(self.locator,
643+
self.formatter)
644+
self._long_axis().set_ticks(ticks)
645+
self._long_axis().set_ticklabels(ticklabels)
646+
fmt = self._long_axis().get_major_formatter()
647+
fmt.set_offset_string(offset_string)
714648

715649
def set_ticks(self, ticks, update_ticks=True):
716650
"""
@@ -766,16 +700,16 @@ def set_ticklabels(self, ticklabels, update_ticks=True):
766700

767701
def minorticks_on(self):
768702
"""
769-
Turn the minor ticks of the colorbar on without extruding
770-
into the "extend regions".
703+
Turn the minor ticks of the colorbar.
771704
"""
772705
self.ax.minorticks_on()
706+
self.minorlocator = self._long_axis().get_minor_locator()
773707
self._short_axis().set_minor_locator(ticker.NullLocator())
774708

775709
def minorticks_off(self):
776710
"""Turn the minor ticks of the colorbar off."""
777-
778-
self._long_axis().set_minor_locator(ticker.NullLocator())
711+
self.minorlocator = ticker.NullLocator()
712+
self._long_axis().set_minor_locator(self.minorlocator)
779713

780714
def set_label(self, label, *, loc=None, **kwargs):
781715
"""
@@ -847,6 +781,76 @@ def _add_solids_patches(self, X, Y, C, mappable):
847781
patches.append(patch)
848782
self.solids_patches = patches
849783

784+
def _do_extends(self, extendlen):
785+
"""
786+
Make adjustments for the extend triangles (or rectanges)
787+
"""
788+
# extend lengths are fraction of the *inner* part of colorbar,
789+
# not the total colorbar:
790+
el = extendlen[0] if self._extend_lower() else 0
791+
eu = extendlen[1] if self._extend_upper() else 0
792+
tot = eu + el + 1
793+
el = el / tot
794+
eu = eu / tot
795+
width = 1 / tot
796+
797+
bounds = np.array([0.0, 0.0, 1.0, 1.0])
798+
bounds[1] = el
799+
bounds[3] = width
800+
801+
# make the inner axes smaller to make room for the extend rectangle
802+
top = bounds[1] + bounds[3]
803+
if not self.extendrect:
804+
xyout = np.array([[0, bounds[1]], [0.5, 0], [1, bounds[1]],
805+
[1, top], [0.5, 1], [0, top], [0, bounds[1]]])
806+
else:
807+
xyout = np.array([[0, bounds[1]], [0, 0], [1, 0], [1, bounds[1]],
808+
[1, top], [1, 1], [0, 1], [0, top],
809+
[0, bounds[1]]])
810+
if self.orientation == 'horizontal':
811+
bounds = bounds[[1, 0, 3, 2]]
812+
xyout = xyout[:, ::-1]
813+
self.ax._set_inner_bounds(bounds)
814+
815+
if not self.filled:
816+
return xyout
817+
# Make extend triangles or rectangles. These are defined in the
818+
# outer parent axes' co-ordinates:
819+
mappable = getattr(self, 'mappable', None)
820+
if (isinstance(mappable, contour.ContourSet)
821+
and any(hatch is not None for hatch in mappable.hatches)):
822+
hatches = mappable.hatches
823+
else:
824+
hatches = [None]
825+
if self._extend_lower:
826+
827+
if not self.extendrect:
828+
xy = np.array([[0.5, 0], [1, el], [0, el]])
829+
else:
830+
xy = np.array([[0, 0], [1., 0], [1, el], [0, el]])
831+
if self.orientation == 'horizontal':
832+
xy = xy[:, ::-1]
833+
color = self.cmap(self.norm(self._values[0]))
834+
patch = mpatches.PathPatch(
835+
mpath.Path(xy), facecolor=color, linewidth=0,
836+
antialiased=False, transform=self.ax.parent_ax.transAxes,
837+
hatch=hatches[0])
838+
self.ax.parent_ax.add_patch(patch)
839+
if self._extend_upper:
840+
if not self.extendrect:
841+
xy = np.array([[0.5, 1], [1, 1-eu], [0, 1-eu]])
842+
else:
843+
xy = np.array([[0, 1], [1, 1], [1, 1-eu], [0, 1-eu]])
844+
if self.orientation == 'horizontal':
845+
xy = xy[:, ::-1]
846+
color = self.cmap(self.norm(self._values[-1]))
847+
patch = mpatches.PathPatch(
848+
mpath.Path(xy), facecolor=color,
849+
linewidth=0, antialiased=False,
850+
transform=self.ax.parent_ax.transAxes, hatch=hatches[-1])
851+
self.ax.parent_ax.add_patch(patch)
852+
return xyout
853+
850854
def add_lines(self, levels, colors, linewidths, erase=True):
851855
"""
852856
Draw lines on the colorbar.

lib/matplotlib/tests/test_colorbar.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,6 @@ def test_colorbar_minorticks_on_off():
309309
im.set_clim(vmin=-1.2, vmax=1.2)
310310
cbar.minorticks_on()
311311
#fig.canvas.draw()
312-
print('here', cbar.ax.yaxis.get_minorticklocs())
313312
np.testing.assert_almost_equal(
314313
cbar.ax.yaxis.get_minorticklocs(),
315314
[-1.1, -0.9, -0.8, -0.7, -0.6, -0.4, -0.3, -0.2, -0.1,

0 commit comments

Comments
 (0)