8000 Fix figure.colorbar() with axes keywords by timhoffm · Pull Request #10000 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Fix figure.colorbar() with axes keywords #10000

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 18, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 6 additions & 1 deletion lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -1891,7 +1891,12 @@ def colorbar(self, mappable, cax=None, ax=None, use_gridspec=True, **kw):
else:
cax, kw = cbar.make_axes(ax, **kw)
cax._hold = True
cb = cbar.colorbar_factory(cax, mappable, **kw)

# need to remove kws that cannot be passed to Colorbar
NON_COLORBAR_KEYS = ['fraction', 'pad', 'shrink', 'aspect', 'anchor',
'panchor']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the remove of these kwargs mean the docstring was wrong/needs changing now?

Copy link
Member Author
@timhoffm timhoffm Dec 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. The notes section in the docs says says:

Additional keyword arguments are of two kinds:

  • axes properties
  • colorbar properties

We receive a mixture, which AFAICS is intended. But we have to remove the kwargs, that Colorbar does not understand.

cb_kw = {k: v for k, v in kw.items() if k not in NON_COLORBAR_KEYS}
cb = cbar.colorbar_factory(cax, mappable, **cb_kw)

self.sca(current_ax)
self.stale = True
Expand Down
9 changes: 9 additions & 0 deletions lib/matplotlib/tests/test_colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,12 @@ def test_colorbar_lognorm_extension():
cb = ColorbarBase(ax, norm=LogNorm(vmin=0.1, vmax=1000.0),
orientation='vertical', extend='both')
assert cb._values[0] >= 0.0


def test_colorbar_axes_kw():
# test fix for #8493: This does only test, that axes-related keywords pass
# and do not raise an exception.
plt.figure()
plt.imshow(([[1, 2], [3, 4]]))
plt.colorbar(orientation='horizontal', fraction=0.2, pad=0.2, shrink=0.5,
aspect=10, anchor=(0., 0.), panchor=(0., 1.))
0