Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8695000

Browse filesBrowse files
committed
Add location keyword argument to Colorbar
1 parent 806d407 commit 8695000
Copy full SHA for 8695000

File tree

Expand file treeCollapse file tree

5 files changed

+385
-4
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+385
-4
lines changed
+24Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
``colorbar`` now has a ``location`` keyword argument
2+
====================================================
3+
4+
The ``colorbar`` method now supports a ``location`` keyword argument
5+
to more easily position the color bar. This is useful when providing
6+
your own inset axes using the ``cax`` keyword argument and behaves
7+
similar to the case where axes are not provided (where the ``location``
8+
keyword is passed on). The ``orientation`` and ``ticklocation`` are no
9+
longer required to be provided as they are determined by ``location``.
10+
``ticklocation`` can still be provided if the automatic setting is not
11+
preferred. (``orientation`` can also be provided but must be compatible
12+
with the ``location``.)
13+
14+
An example is:
15+
16+
.. code-block:: python
17+
import matplotlib.pyplot as plt
18+
import numpy as np
19+
rng = np.random.default_rng(19680801)
20+
imdata = rng.random((10, 10))
21+
fig, ax = plt.subplots()
22+
im = ax.imshow(imdata)
23+
fig.colorbar(im, cax=ax.inset_axes([0, 1.05, 1, 0.05]),
24+
location='top')

‎lib/matplotlib/colorbar.py

Copy file name to clipboardExpand all lines: lib/matplotlib/colorbar.py
+30-4Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@
4242
of the colorbar, as that also determines the *orientation*; passing
4343
incompatible values for *location* and *orientation* raises an exception.
4444
45+
ticklocation : {'auto', 'left', 'right', 'top', 'bottom'}
46+
The location of the colorbar ticks. The *ticklocation* location value must
47+
match *orientation*. For example, a horizontal colorbar can only have ticks
48+
at the top or the bottom.
49+
4550
fraction : float, default: 0.15
4651
Fraction of original axes to use for colorbar.
4752
@@ -321,13 +326,17 @@ class Colorbar:
321326
alpha : float
322327
The colorbar transparency between 0 (transparent) and 1 (opaque).
323328
324-
orientation : {'vertical', 'horizontal'}
329+
orientation : None or {'vertical', 'horizontal'}
330+
If None, use the value determined by *location*. If both
331+
*orientation* and *location* are None, 'vertical'.
325332
326333
ticklocation : {'auto', 'left', 'right', 'top', 'bottom'}
327334
328335
drawedges : bool
329336
330337
filled : bool
338+
339+
location : None or {'left', 'right', 'top', 'bottom'}
331340
%s
332341
"""
333342

@@ -339,7 +348,7 @@ def __init__(self, ax, mappable=None, *, cmap=None,
339348
alpha=None,
340349
values=None,
341350
boundaries=None,
342-
orientation='vertical',
351+
orientation=None,
343352
ticklocation='auto',
344353
extend=None,
345354
spacing='uniform', # uniform or proportional
@@ -350,6 +359,7 @@ def __init__(self, ax, mappable=None, *, cmap=None,
350359
extendfrac=None,
351360
extendrect=False,
352361
label='',
362+
location=None,
353363
):
354364

355365
if mappable is None:
@@ -380,14 +390,26 @@ def __init__(self, ax, mappable=None, *, cmap=None,
380390
mappable.colorbar_cid = mappable.callbacks.connect(
381391
'changed', self.update_normal)
382392

393+
location_orientation = _api.check_getitem(
394+
{None: None, "left": "vertical", "right": "vertical",
395+
"top": "horizontal", "bottom": "horizontal"},
396+
location=location)
397+
383398
_api.check_in_list(
384-
['vertical', 'horizontal'], orientation=orientation)
399+
[None, 'vertical', 'horizontal'], orientation=orientation)
385400
_api.check_in_list(
386401
['auto', 'left', 'right', 'top', 'bottom'],
387402
ticklocation=ticklocation)
388403
_api.check_in_list(
389404
['uniform', 'proportional'], spacing=spacing)
390405

406+
if location_orientation is not None and orientation is not None:
407+
if location_orientation != orientation:
408+
raise TypeError(
409+
"location and orientation are mutually exclusive")
410+
else:
411+
orientation = orientation or location_orientation or "vertical"
412+
391413
self.ax = ax
392414
self.ax._axes_locator = _ColorbarAxesLocator(self)
393415

@@ -445,7 +467,11 @@ def __init__(self, ax, mappable=None, *, cmap=None,
445467
self.__scale = None # linear, log10 for now. Hopefully more?
446468

447469
if ticklocation == 'auto':
448-
ticklocation = 'bottom' if orientation == 'horizontal' else 'right'
470+
if location is None:
471+
ticklocation = (
472+
'bottom' if orientation == 'horizontal' else 'right')
473+
else:
474+
ticklocation = location
449475
self.ticklocation = ticklocation
450476

451477
self.set_label(label)
Loading

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.