@@ -533,15 +533,52 @@ def _colorbar_tick_values(norm) -> Optional[List[float]]:
533533 return [lo , mid , hi ]
534534
535535
536+ _SUPERSCRIPT_DIGITS = str .maketrans ("0123456789-" , "⁰¹²³⁴⁵⁶⁷⁸⁹⁻" )
537+
538+
539+ def _to_scientific (v : float ) -> Optional [str ]:
540+ """Convert *v* to Unicode scientific notation (e.g. ``4.3×10⁴``).
541+
542+ Returns ``None`` when ``f"{v:.2g}"`` does not produce an exponent (unusual
543+ edge case for certain values near the g-format threshold).
544+ """
545+ s = f"{ v :.2g} "
546+ if "e" not in s :
547+ return None
548+ mantissa , exp = s .split ("e" )
549+ sign = "-" if exp .startswith ("-" ) else ""
550+ exp_num = exp .lstrip ("+-" ).lstrip ("0" ) or "0"
551+ superscript = f"{ sign } { exp_num } " .translate (_SUPERSCRIPT_DIGITS )
552+ return f"{ mantissa } ×10{ superscript } "
553+
554+
536555def _fmt_tick (v : float ) -> str :
537- """Format a single tick value to 2 decimal places without scientific notation."""
556+ """Format a single tick value compactly.
557+
558+ Values with 5 or more digits (abs(v) >= 10000) or very small values
559+ (abs(v) < 0.001) are rendered as compact scientific notation using
560+ Unicode superscripts, e.g. ``4.3×10⁴`` or ``1.2×10⁻⁵``. This avoids
561+ LaTeX expansion that would overflow the colorbar width. Values in
562+ between are rendered with ``:.2f``.
563+ """
564+ abs_v = abs (v )
565+ if abs_v != 0 and (abs_v >= 10000 or abs_v < 0.001 ):
566+ sci = _to_scientific (v )
567+ return sci if sci is not None else f"{ v :.2g} "
538568 return f"{ v :.2f} "
539569
540570
541571def _colorbar_tick_labels (tick_values : List [float ], cb_unit : Optional [str ] = None ) -> List [str ]:
542- """Format tick values without scientific notation, appending *cb_unit* to the middle label.
572+ """Format tick values, appending *cb_unit* to the middle label.
573+
574+ All three labels use a consistent notation style: if any tick is rendered
575+ in scientific notation (``×10ⁿ``), every non-zero tick is forced through
576+ the same format. This prevents the central tick from showing e.g.
577+ ``-5000.00`` when the outer ticks show ``-2×10⁴`` / ``1.5×10⁴`` because
578+ the midpoint happens to fall below the per-value threshold.
543579
544- If *cb_unit* is ``None`` the unit is read from config; pass ``""`` for unitless panels.
580+ If *cb_unit* is ``None`` the unit is read from config; pass ``""`` for
581+ unitless panels.
545582 """
546583 if cb_unit is None :
547584 try :
@@ -551,6 +588,18 @@ def _colorbar_tick_labels(tick_values: List[float], cb_unit: Optional[str] = Non
551588 cb_unit = ""
552589 labels = [_fmt_tick (v ) for v in tick_values ]
553590 mid = len (labels ) // 2
591+
592+ # Enforce consistent notation: if any label uses ×10, convert all others.
593+ if any ("×10" in lbl for lbl in labels ):
594+ for i , (lbl , v ) in enumerate (zip (labels , tick_values )):
595+ if "×10" not in lbl :
596+ if v == 0 :
597+ labels [i ] = "0"
598+ else :
599+ sci = _to_scientific (v )
600+ if sci is not None :
601+ labels [i ] = sci
602+
554603 labels [mid ] = f"{ labels [mid ]} { cb_unit } "
555604 return labels
556605
@@ -569,8 +618,8 @@ def _apply_colorbar(
569618 Override the unit string on the middle tick. Pass ``""`` for unitless panels.
570619 ``None`` reads the unit from config.
571620 is_subplot
572- When ``True`` uses ``labelsize_subplot`` from config (default 22 ) instead of
573- the single-figure ``labelsize`` (default 22 ).
621+ When ``True`` uses ``labelsize_subplot`` from config (default 16 ) instead of
622+ the single-figure ``labelsize`` (default 16 ).
574623 """
575624 tick_values = _colorbar_tick_values (getattr (mappable , "norm" , None ))
576625
@@ -582,7 +631,7 @@ def _apply_colorbar(
582631 ticks = tick_values ,
583632 )
584633 labelsize_key = "labelsize_subplot" if is_subplot else "labelsize"
585- labelsize = float (_conf_colorbar (labelsize_key , 22 ))
634+ labelsize = float (_conf_colorbar (labelsize_key , 16 ))
586635 labelrotation = float (_conf_colorbar ("labelrotation" , 90 ))
587636 if tick_values is not None :
588637 cb .ax .set_yticklabels (
0 commit comments