Skip to content

Commit b5c3ec4

Browse files
committed
Fix #195, support histogram legend labs, fix box/violinplot bug
1 parent ddfdef7 commit b5c3ec4

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

proplot/axes/plot.py

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -495,23 +495,22 @@ def standardize_1d(self, func, *args, autoformat=None, **kwargs):
495495
)
496496

497497
# Auto formatting
498-
xi = None # index version of 'x'
498+
x_index = None # index version of 'x'
499499
if not hasattr(self, 'projection'):
500500
# First handle string-type x-coordinates
501501
kw = {}
502502
xname = 'y' if orientation == 'horizontal' else 'x'
503503
yname = 'x' if xname == 'y' else 'y'
504504
if _is_string(x):
505-
xi = np.arange(len(x))
506-
kw[xname + 'locator'] = mticker.FixedLocator(xi)
507-
kw[xname + 'formatter'] = mticker.IndexFormatter(x)
508-
kw[xname + 'minorlocator'] = mticker.NullLocator()
509-
if name == 'boxplot':
510-
kwargs['labels'] = x
511-
elif name == 'violinplot':
512-
kwargs['positions'] = xi
513-
if name in ('boxplot', 'violinplot'):
514-
kwargs['positions'] = xi
505+
if name in ('hist',):
506+
kwargs.setdefault('labels', list(x))
507+
else:
508+
x_index = np.arange(len(x))
509+
kw[xname + 'locator'] = mticker.FixedLocator(x_index)
510+
kw[xname + 'formatter'] = mticker.IndexFormatter(x)
511+
kw[xname + 'minorlocator'] = mticker.NullLocator()
512+
if name == 'boxplot': # otherwise IndexFormatter is overridden
513+
kwargs['labels'] = x
515514

516515
# Next handle labels if 'autoformat' is on
517516
# NOTE: Do not overwrite existing labels!
@@ -529,18 +528,19 @@ def standardize_1d(self, func, *args, autoformat=None, **kwargs):
529528
kw[xname + 'label'] = label
530529
# Reversed axis
531530
if name not in ('scatter',):
532-
if len(x) > 1 and xi is None and x[1] < x[0]:
531+
if x_index is None and len(x) > 1 and x[1] < x[0]:
533532
kw[xname + 'reverse'] = True
534533

535534
# Appply
536535
if kw:
537536
self.format(**kw)
538537

539538
# Standardize args
540-
if xi is not None:
541-
x = xi
539+
if x_index is not None:
540+
x = x_index
542541
if name in ('boxplot', 'violinplot'):
543542
ys = [_to_ndarray(yi) for yi in ys] # store naked array
543+
kwargs['positions'] = x
544544

545545
# Basemap shift x coordiantes without shifting y, we fix this!
546546
if getattr(self, 'name', '') == 'basemap' and kwargs.get('latlon', None):
@@ -2375,6 +2375,9 @@ def cycle_changer(
23752375
if label is None and (colorbar_legend_label or isinstance(ilabel, str)):
23762376
labels[i] = ilabel
23772377

2378+
# Sanitize labels
2379+
labels = [_not_none(label, '') for label in labels]
2380+
23782381
# Get step size for bar plots
23792382
# WARNING: This will fail for non-numeric non-datetime64 singleton
23802383
# datatypes but this is good enough for vast majority of most cases.
@@ -2424,7 +2427,7 @@ def cycle_changer(
24242427

24252428
# Get y coordinates and labels
24262429
if name in ('pie', 'boxplot', 'violinplot'):
2427-
# Only ever have one y value, cannot have legend labs
2430+
# Only ever have one y value, cannot have legend labels
24282431
iys = (y1,)
24292432

24302433
else:
@@ -2443,11 +2446,7 @@ def cycle_changer(
24432446
y_i if y_i.ndim == 1 else _to_indexer(y_i)[:, i]
24442447
for y_i in ys
24452448
)
2446-
2447-
# Add label for artist
2448-
label = labels[i]
2449-
if label is not None:
2450-
kw['label'] = label
2449+
kw['label'] = labels[i] or ''
24512450

24522451
# Build coordinate arguments
24532452
ixy = ()
@@ -2496,10 +2495,14 @@ def cycle_changer(
24962495
if errobjs_join:
24972496
legobjs = [(*legobjs, *errobjs_join)[::-1]]
24982497
legobjs.extend(errobjs_separate)
2499-
labels = [
2500-
(obj[-1] if type(obj) in (list, tuple) else obj).get_label()
2501-
for obj in legobjs
2502-
]
2498+
for _ in range(3):
2499+
# Account for (1) multiple columns of data, (2) functions that return
2500+
# multiple values (e.g. hist() returns (bins, values, patches)), and
2501+
# (3) matplotlib.Collection list subclasses.
2502+
legobjs = [
2503+
obj[-1] if isinstance(obj, (list, tuple)) else obj
2504+
for obj in legobjs
2505+
]
25032506

25042507
# Add handles and labels
25052508
# NOTE: Important to add labels as *keyword* so users can override

0 commit comments

Comments
 (0)