Skip to content

BUG: Fix _extent not set in PcolorImage #9442

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
Oct 23, 2017
Merged

BUG: Fix _extent not set in PcolorImage #9442

merged 1 commit into from
Oct 23, 2017

Conversation

AtharvaKhare
Copy link
Contributor

@AtharvaKhare AtharvaKhare commented Oct 16, 2017

PcolorImage now sets _extent of AxesImage on its creation.

PR Summary

Depending on bins argument of hist2d, style is set to either image or pcolorimage in _axes.pcolorfast. If the style is image, AxesImage object is created and is supplied with extents. The issue arose when style was pcolorimage. The _extent is never set in super class AxesImage of PcolorImage.
The issue is solved by setting _extent after PcolorImage creation.

This PR fixes #8426.

PR Checklist

  • Has Pytest style unit tests
  • Code is PEP 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

@jklymak
Copy link
Member

jklymak commented Oct 18, 2017

Thanks for this! Can you supply a test that doesn't work w/ master but does work with your suggested change? It'd help (me?) to get context for it.

@AtharvaKhare
Copy link
Contributor Author

@jklymak Sample example from issue:

import pylab as plt
x = plt.hist2d([1,2,3], [3,5,6], bins=[[0,3,7], [1,2,3]])
im = x[-1]
print(type(im))
print(im.get_window_extent())

The issue arose when plt.hist2d returns object of matplotlib.image.PcolorImage.
hist2d returns either matplotlib.image.AxesImage or matplotlib.image.PcolorImage depending on the parameter bins. The condition for selection is:

elif len(args) == 3:
x, y = args[:2]
x = np.asarray(x)
y = np.asarray(y)
if x.ndim == 1 and y.ndim == 1:
if x.size == 2 and y.size == 2:
style = "image"
else:
dx = np.diff(x)
dy = np.diff(y)
if (np.ptp(dx) < 0.01 * np.abs(dx.mean()) and
np.ptp(dy) < 0.01 * np.abs(dy.mean())):
style = "image"
else:
style = "pcolorimage"

Followed by creation of object:
if style == "image":
im = mimage.AxesImage(self, cmap, norm,
interpolation='nearest',
origin='lower',
extent=(xl, xr, yb, yt),
**kwargs)

or
elif style == "pcolorimage":
im = mimage.PcolorImage(self, x, y, C,
cmap=cmap,
norm=norm,
alpha=alpha,
**kwargs)

Here extent was not specified while creation of the PcolorImage object.
super(PcolorImage, self).__init__(ax, norm=norm, cmap=cmap)

PcolorImage calls AxesImage constructor with extent=None, which causes error when referenced.

In short, to recreate error, create any PcolorImage object and reference its parent's(AxesImage) extents. To do this, elements of bins must be of dim 1 and size >=3, with intervals such that the do not satisfy:

dx = np.diff(x)
dy = np.diff(y)
if (np.ptp(dx) < 0.01 * np.abs(dx.mean()) and
np.ptp(dy) < 0.01 * np.abs(dy.mean())):
style = "image"
else:
style = "pcolorimage"

Basically some non-equally spaced bins.

@dstansby
Copy link
Member

Could you add the test in lib/matplotlib/tests/test_image.py? It doesn't need to be an image test, but just a test to check that the code doesn't fail.

Fixes #8426. Added test to check `extent`:
`lib/matplotlib/tests/test_image.py::test_pcolorimage_extent()`
@AtharvaKhare
Copy link
Contributor Author

Changed the way _extent is set.

@dstansby Added test.

@tacaswell tacaswell added this to the v2.1.1 milestone Oct 23, 2017
@tacaswell tacaswell merged commit 5a4cf14 into matplotlib:master Oct 23, 2017
NelleV added a commit that referenced this pull request Oct 23, 2017
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.

PcolorImage does not set _extent
4 participants