Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Clarify error for colorbar with unparented mappable#23740

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
timhoffm merged 1 commit intomatplotlib:mainfromQuLogic:parentless-mappable
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletiondoc/api/next_api_changes/removals/22081-AL.rst
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionlib/matplotlib/figure.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionlib/matplotlib/tests/test_colorbar.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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

[8]ページ先頭

©2009-2025 Movatter.jp