Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Corrected cax attributes of ImageGrid axes #3957

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 7 commits into from
Feb 19, 2015

Conversation

inclement
Copy link
Contributor

Following the documentation, each axis of an ImageGrid should have a cax attribute pointing to the axis to be used for its colorbar. However, when the axes of a row or column share a colorbar, this attribute is not set correctly - colorbar axes are assigned as if every axis has its own colorbar, so most of the cax attributes are not visible axes, and the axes do not match the correct row or column.

This PR changes the behaviour to correctly associate axes of a row/column with the colorbar at the end of that row/column.

Example code demonsrating the problem is:

from mpl_toolkits.axes_grid1 import ImageGrid
import matplotlib.pyplot as plt
import numpy as n


X, Y = n.meshgrid(n.linspace(0, 6, 30), n.linspace(0, 6, 30))
arr = n.sin(X) * n.cos(Y) + 1j*(n.sin(3*Y) * n.cos(Y/2.))

fig = plt.figure(figsize=(6, 6))

grid = ImageGrid(fig, 111,
                      nrows_ncols = (2, 2),
                      direction='row',
                      cbar_location='right',
                      cbar_mode='edge',
                 )
ax1, ax2, ax3, ax4, = grid

im1 = ax1.imshow(arr.real, cmap='spectral')
im2 = ax2.imshow(arr.imag, cmap='hot')
im3 = ax3.imshow(n.abs(arr), cmap='jet')
im4 = ax4.imshow(n.arctan2(arr.imag, arr.real), cmap='hsv')

ax2.cax.colorbar(im2)
ax4.cax.colorbar(im4)

fig.savefig('colorbar_failure.png')

Before this patch, the top-right colorbar will be empty while the bottom-right colorbar will show colours from one of the top-row plots. After this patch, the colorbars updated are those in the correct row/column to match the axis whose cax attribute was accessed.

This would be a breaking change for any users currently using the workaround of accessing the cax attributes or cbar_axes dircetly in order of their creation, which corresponds to the order in which they are visible. I'm not sure what the matplotlib policy is on such changes, let me know if it is inappropriate!

Edit: added results of the above script

before:
colorbar-current_behaviour

and after:
colorbar-new_behaviour

@tacaswell tacaswell modified the milestones: v1.5.x, v1.4.x Dec 30, 2014
@tacaswell
Copy link
Member

cc @leejjoon

This should probably be back-ported to 1.4.x

@tacaswell tacaswell changed the title Corrected cax attirbutes of ImageGrid axes Corrected cax attirbutes of ImageGrid axes [backport to 1.4.x] Dec 30, 2014
@inclement inclement changed the title Corrected cax attirbutes of ImageGrid axes [backport to 1.4.x] Corrected cax attributes of ImageGrid axes [backport to 1.4.x] Dec 30, 2014
@tacaswell
Copy link
Member

@inclement Would you mind adding a test?

Currently axes_grid has zero test coverage so anything is an improvement.

If you are willing follow the directions here : http://matplotlib.org/devel/testing.html#writing-an-image-comparison-test

@inclement
Copy link
Contributor Author

Yes, I can certainly do that.

@tacaswell
Copy link
Member

Great! Ping me if you have any issues.

@inclement
Copy link
Contributor Author

I've added a simple test focused on this specific issue.

@inclement
Copy link
Contributor Author

I've just realised this PR doesn't correctly handle the cbar edge, confusing direction with cbar_location. I'll add a fix and tests for it tomorrow.

@inclement
Copy link
Contributor Author

Okay, I've fixed all the issues I'm aware of, assuming my test format is correct.

@tacaswell
Copy link
Member

The tests look good to me.

I am not sure I fully understand the underlying code, I would like to get @leejjoon 's thoughts on this before merging.

@WeatherGod
Copy link
Member

I think the test still needs to be white-listed

@inclement
Copy link
Contributor Author

@WeatherGod does that mean to add it to default_test_modules in lib/matplotlib/__init__.py?

@tacaswell
Copy link
Member

Yes, sorry I forgot to mention that before

On Tue, Dec 30, 2014, 20:31 Alexander Taylor [email protected]
wrote:

@WeatherGod https://github.com/WeatherGod does that mean to add it to
default_test_modules in lib/matplotlib/init.py?


Reply to this email directly or view it on GitHub
#3957 (comment)
.

@inclement
Copy link
Contributor Author

I merged the master branch into this one to fix a merge conflict that appeared, but now realise this was probably the wrong way to do it since it messes up the diff. Should I rebase a084e80 on the new master instead, or is there a better way?

Sorry for the mistake, and thanks for the help!

Edit: I reset the merge, but am still not sure if a rebase is the right way to resolve the merge conflict.

@inclement inclement force-pushed the imagegrid_edge_cbar branch from d91c893 to a084e80 Compare January 1, 2015 03:01
@WeatherGod
Copy link
Member

Rebase and force push. Feature branches are OK to force push because they
are understood to be in flux.
On Dec 31, 2014 9:56 PM, "Alexander Taylor" [email protected]
wrote:

I merged the master branch into this one to fix a merge conflict that
appeared, but now realise this was probably the wrong way to do it since it
messes up the diff. Should I rebase a084e80
a084e80
on the new master instead, or is there a better way? And is it okay to fix
by force pushing to this branch, or should I make a new pr?

Sorry for the mistake, and thanks for the help!


Reply to this email directly or view it on GitHub
#3957 (comment)
.

@inclement inclement force-pushed the imagegrid_edge_cbar branch from a084e80 to 1ec309f Compare January 2, 2015 00:19
@tacaswell tacaswell modified the milestones: v1.5.x, v1.4.x Jan 2, 2015
@tacaswell tacaswell changed the title Corrected cax attributes of ImageGrid axes [backport to 1.4.x] Corrected cax attributes of ImageGrid axes Jan 2, 2015
@tacaswell
Copy link
Member

Could you also update the documentation on this function to explain the rules for sharing the color bars?

Am I understanding that the colors that end up in the color bar are selected based on the order in which they are called (so the calls that look like no-ops in the final figure are actually just getting replaced)?

re-milestoned as 1.5.x as this is seeming like a 'big' change to me now, but I am open to being (re)convinced it should be back-ported.

@inclement
Copy link
Contributor Author

The core of the problem is that (by default), each axis of the ImageGrid receives a cax attribute that is supposed to refer to the axis on which its colorbar should be drawn. Calling colorbar() on this axis has the normal matplotlib behaviour in that repeat callings override the previous one. I can certainly add a note in the doc about this, but (if I understand right) it follows naturally from the normal colorbar behaviour.

I only used the overriding in the test because it demonstrates the problem well - without this PR being applied, the colorbars will be displayed on the wrong rows/columns or not displayed at all. Following this PR, the visible colorbar on each row/column is the most recent one called by a plotting axis in that row/column.

As for backporting, I think this is probably a legitimate bug based on the documentation and existing code (which seems to imply that the cax attribute of every axis should be the actual visible colorbar axis in the given row/column), but I can't say for sure and I don't know why this behaviour was omitted if so. It is a significant change in that it will break existing code that works around the problem. I'm not sure what the matplotlib policy is on such changes, but I hope this explanation makes my perspective clear on its nature.



@image_comparison(baseline_images=['imagegrid_cbar_mode'],
remove_text=True)
Copy link
Member

Choose a reason for hiding this comment

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

I think we only need to test one format here. PNG is a good candidate.

@inclement
Copy link
Contributor Author

@pelson Okay, I've set the test to png only

@tacaswell
Copy link
Member

Can you re-base it to expunge the pdf/svg files from history?

@inclement inclement force-pushed the imagegrid_edge_cbar branch from b89d40c to 4045a79 Compare January 31, 2015 00:47
@inclement
Copy link
Contributor Author

I finally worked out how to drive an interactive rebase and I think I've successfully removed the pdf and svg from history.

@tacaswell
Copy link
Member

Almost, it looks like 07a336e removes an image added by 695f90e

Rebasing is tricky, but worth the learning curve (and wreckage along the way) ;).

@inclement
Copy link
Contributor Author

Will it suffice to remove that image from the commit at which it was added but not change anything else, so the test will fail for those couple of commits (due to no reference image) but everything will be fixed by the later ones that modify the test and add the final image?

@tacaswell
Copy link
Member

Yes. We don't worry about tests passing on every commit, only on the
merges to master/maintenance branches.

On Fri Jan 30 2015 at 8:13:34 PM Alexander Taylor [email protected]
wrote:

Will it suffice to remove that image from the commit at which it was added
but not change anything else, so the test will fail for those couple of
commits (due to no reference image) but everything will be fixed by the
later ones that modify the test and add the final image?


Reply to this email directly or view it on GitHub
#3957 (comment)
.

@inclement inclement force-pushed the imagegrid_edge_cbar branch from 4045a79 to 53da33c Compare February 6, 2015 23:31
@inclement
Copy link
Contributor Author

Okay, I think I've removed that image.

@tacaswell
Copy link
Member

Looks good to me. Would like feed back from @leejjoon before merging.

@tacaswell tacaswell modified the milestones: proposed next point release, next point release Feb 19, 2015
tacaswell added a commit that referenced this pull request Feb 19, 2015
BUG : Corrected cax attributes of ImageGrid axes
@tacaswell tacaswell merged commit a2f33a3 into matplotlib:master Feb 19, 2015
@tacaswell
Copy link
Member

@inclement Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants