FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Clarify error for colorbar with unparented mappable by QuLogic · Pull Request #23740 · matplotlib/matplotlib · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
5 changes: 4 additions & 1 deletion doc/api/next_api_changes/removals/22081-AL.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
colorbar defaults to stealing space from the mappable's axes rather than the current axes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Pass ``ax=plt.gca()`` to restore the previous behavior.
If the mappable does not have an Axes, then an error will be raised.

Pass the *cax* or *ax* argument to be explicit about where the colorbar will be
placed. Passing ``ax=plt.gca()`` will restore the previous behavior.

Removal of deprecated ``colorbar`` APIs
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
8 changes: 7 additions & 1 deletion lib/matplotlib/figure.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1245,13 +1245,19 @@ def colorbar(
"""

if ax is None:
ax = getattr(mappable, "axes", self.gca())
ax = getattr(mappable, "axes", None)

if (self.get_layout_engine() is not None and
not self.get_layout_engine().colorbar_gridspec):
use_gridspec = False
# Store the value of gca so that we can set it back later on.
if cax is None:
if ax is None:
raise ValueError(
'Unable to determine Axes to steal space for Colorbar. '
'Either provide the *cax* argument to use as the Axes for '
'the Colorbar, provide the *ax* argument to steal space '
'from it, or add *mappable* to an Axes.')
current_ax = self.gca()
userax = False
if (use_gridspec and isinstance(ax, SubplotBase)):
Expand Down
10 changes: 9 additions & 1 deletion lib/matplotlib/tests/test_colorbar.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,14 @@ def test_colorbarbase():
Colorbar(ax, cmap=plt.cm.bone)


def test_parentless_mappable():
pc = mpl.collections.PatchCollection([], cmap=plt.get_cmap('viridis'))
pc.set_array([])

with pytest.raises(ValueError, match='Unable to determine Axes to steal'):
plt.colorbar(pc)


@image_comparison(['colorbar_closed_patch.png'], remove_text=True)
def test_colorbar_closed_patch():
# Remove this line when this test image is regenerated.
Expand Down Expand Up @@ -675,7 +683,7 @@ def test_colorbar_inverted_ticks():
def test_mappable_no_alpha():
fig, ax = plt.subplots()
sm = cm.ScalarMappable(norm=mcolors.Normalize(), cmap='viridis')
fig.colorbar(sm)
fig.colorbar(sm, ax=ax)
sm.set_cmap('plasma')
plt.draw()

Expand Down

Back | FazBrowse Home | New Git URL