Skip to content

Correctly pass location when constructing ImageGrid colorbar. #25884

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 1 commit into from
May 14, 2023
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
15 changes: 3 additions & 12 deletions lib/mpl_toolkits/axes_grid1/axes_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,14 @@ def __init__(self, *args, orientation, **kwargs):
self.orientation = orientation
super().__init__(*args, **kwargs)
Copy link
Member

Choose a reason for hiding this comment

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

Semi-OT, but got to this because you deleted cla(): This super().__init__ should not do anything, right? - Likely a leftover from the dedicated axes_grid colorbar classes?

Copy link
Contributor Author

@anntzer anntzer May 14, 2023

Choose a reason for hiding this comment

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

It does (should?), as CbarAxesBase is a mixin and will thus end up in the base classes of the actual colorbar axes class used by ImageGrid, and thus we need to forward arguments via super() (after extracting out orientation).

Copy link
Member

Choose a reason for hiding this comment

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

Indeed. Maybe that should be mentioned somewhere in the class? without that context, it's looks like object is the only parent. Either way, the PR is correct.


def colorbar(self, mappable, *, ticks=None, **kwargs):
orientation = (
"horizontal" if self.orientation in ["top", "bottom"] else
"vertical")
cb = self.figure.colorbar(mappable, cax=self, orientation=orientation,
ticks=ticks, **kwargs)
return cb
def colorbar(self, mappable, **kwargs):
return self.figure.colorbar(
mappable, cax=self, location=self.orientation, **kwargs)

def toggle_label(self, b):
axis = self.axis[self.orientation]
axis.toggle(ticklabels=b, label=b)

def cla(self):
orientation = self.orientation
super().cla()
self.orientation = orientation


_cbaraxes_class_factory = cbook._make_class_factory(CbarAxesBase, "Cbar{}")

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 6 additions & 10 deletions lib/mpl_toolkits/axes_grid1/tests/test_axes_grid1.py
Original file line number Diff line number Diff line change
Expand Up @@ -701,11 +701,7 @@ def test_insetposition():
@image_comparison(['imagegrid_cbar_mode.png'],
remove_text=True, style='mpl20', tol=0.3)
def test_imagegrid_cbar_mode_edge():
# Remove this line when this test image is regenerated.
plt.rcParams['pcolormesh.snap'] = False

X, Y = np.meshgrid(np.linspace(0, 6, 30), np.linspace(0, 6, 30))
arr = np.sin(X) * np.cos(Y) + 1j*(np.sin(3*Y) * np.cos(Y/2.))
arr = np.arange(16).reshape((4, 4))

fig = plt.figure(figsize=(18, 9))

Expand All @@ -721,12 +717,12 @@ def test_imagegrid_cbar_mode_edge():
cbar_location=location,
cbar_size='20%',
cbar_mode='edge')
ax1, ax2, ax3, ax4, = grid
ax1, ax2, ax3, ax4 = grid

ax1.imshow(arr.real, cmap='nipy_spectral')
ax2.imshow(arr.imag, cmap='hot')
ax3.imshow(np.abs(arr), cmap='jet')
ax4.imshow(np.arctan2(arr.imag, arr.real), cmap='hsv')
ax1.imshow(arr, cmap='nipy_spectral')
ax2.imshow(arr.T, cmap='hot')
ax3.imshow(np.hypot(arr, arr.T), cmap='jet')
ax4.imshow(np.arctan2(arr, arr.T), cmap='hsv')

# In each row/column, the "first" colorbars must be overwritten by the
# "second" ones. To achieve this, clear out the axes first.
Expand Down