Skip to content

Commit ed8e131

Browse files
committed
Use artist labels for cbar ticks when making artist-list cbars
1 parent 2d835f2 commit ed8e131

1 file changed

Lines changed: 25 additions & 11 deletions

File tree

proplot/axes/plot.py

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3641,7 +3641,7 @@ def colorbar_wrapper(
36413641
locator = _not_none(ticks=ticks, locator=locator)
36423642
minorlocator = _not_none(minorticks=minorticks, minorlocator=minorlocator)
36433643
ticklocation = _not_none(tickloc=tickloc, ticklocation=ticklocation)
3644-
formatter = _not_none(ticklabels=ticklabels, formatter=formatter, default='auto')
3644+
formatter = _not_none(ticklabels=ticklabels, formatter=formatter)
36453645

36463646
# Colorbar kwargs
36473647
# WARNING: PathCollection scatter objects have an extend method!
@@ -3704,8 +3704,9 @@ def colorbar_wrapper(
37043704
# and PolyCollection matplotlib classes are iterable.
37053705
cmap = None
37063706
if not isinstance(mappable, (martist.Artist, mcontour.ContourSet)):
3707-
# Any colormap spec, including a list of colors, colormap name, or
3708-
# colormap instance.
3707+
# A colormap instance
3708+
# TODO: Pass remaining arguments through Colormap()? This is really
3709+
# niche usage so maybe not necessary.
37093710
if isinstance(mappable, mcolors.Colormap):
37103711
# NOTE: 'Values' makes no sense if this is just a colormap. Just
37113712
# use unique color for every segmentdata / colors color.
@@ -3724,11 +3725,12 @@ def colorbar_wrapper(
37243725
locator = _not_none(locator, values) # tick *all* values by default
37253726

37263727
# List of artists
3728+
# NOTE: Do not check for isinstance(Artist) in case it is an mpl collection
37273729
elif np.iterable(mappable) and all(
37283730
hasattr(obj, 'get_color') or hasattr(obj, 'get_facecolor')
37293731
for obj in mappable
37303732
):
3731-
# Generate colormap from colors
3733+
# Generate colormap from colors and infer tick labels
37323734
colors = []
37333735
for obj in mappable:
37343736
if hasattr(obj, 'get_color'):
@@ -3745,17 +3747,27 @@ def colorbar_wrapper(
37453747
colors.append(to_rgb(color))
37463748
cmap = mcolors.ListedColormap(colors, '_no_name')
37473749

3748-
# Try to infer values from labels
3750+
# Try to infer tick values and tick labels from Artist labels
37493751
if values is None:
3752+
labs = []
37503753
values = []
37513754
for obj in mappable:
3752-
val = obj.get_label()
3755+
if hasattr(obj, 'get_label'):
3756+
lab = obj.get_label()
3757+
else:
3758+
lab = None
37533759
try:
3754-
val = float(val)
3760+
value = float(lab)
37553761
except ValueError:
3756-
values = np.arange(len(colors))
3757-
break
3758-
values.append(val)
3762+
value = None
3763+
labs.append(lab)
3764+
values.append(value)
3765+
if any(value is None for value in values):
3766+
values = np.arange(len(mappable))
3767+
if formatter is None and any(lab is not None for lab in labs):
3768+
formatter = labs # use these fixed values for ticks
3769+
if orientation == 'horizontal':
3770+
kw_ticklabels.setdefault('rotation', 90)
37593771
locator = _not_none(locator, values) # tick *all* values by default
37603772

37613773
else:
@@ -3833,8 +3845,10 @@ def colorbar_wrapper(
38333845
extendsize = extendsize / (scale - 2 * extendsize)
38343846

38353847
# Draw the colorbar
3848+
# NOTE: Set default formatter here because we optionally apply a FixedFormatter
3849+
# using *labels* from handle input.
38363850
locator = constructor.Locator(locator, **locator_kw)
3837-
formatter = constructor.Formatter(formatter, **formatter_kw)
3851+
formatter = constructor.Formatter(_not_none(formatter, 'auto'), **formatter_kw)
38383852
kwargs.update({
38393853
'ticks': locator,
38403854
'format': formatter,

0 commit comments

Comments
 (0)