8000 FIX: add support for imshow extent to have units by pratimugale · Pull Request #22230 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

FIX: add support for imshow extent to have units #22230

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
Nov 4, 2022

Conversation

pratimugale
Copy link
Contributor
@pratimugale pratimugale commented Jan 14, 2022

PR Summary

This PR allows the imshow extent parameter to be expressed in units.
It is with reference to the Bug: #22105

PR Checklist

Tests and Styling

  • Has pytest style unit tests (and pytest passes).
  • Is Flake 8 compliant (install flake8-docstrings and run flake8 --docstring-convention=all).

Documentation

  • New features are documented, with examples if plot related.
  • New features have an entry in doc/users/next_whats_new/ (follow instructions in README.rst there).
  • API changes documented in doc/api/next_api_changes/ (follow instructions in README.rst there).
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).

Copy link
@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a while, please feel free to ping @matplotlib/developers or anyone who has commented on the PR. Most of our reviewers are volunteers and sometimes things fall through the cracks.

You can also join us on gitter for real-time discussion.

For details on testing, writing docs, and our review process, please see the developer guide

We strive to be a welcoming and open project. Please follow our Code of Conduct.

@pratimugale pratimugale marked this pull request as draft January 14, 2022 06:07
@QuLogic QuLogic added the status: needs workflow approval For PRs from new contributors, from which GitHub blocks workflows by default. label Jan 14, 2022
@pratimugale pratimugale force-pushed the bugfix-for-issue-22105 branch from f10736c to ceadcea Compare January 14, 2022 17:36
@t
8000
acaswell tacaswell added this to the v3.6.0 milestone Jan 14, 2022
@pratimugale pratimugale force-pushed the bugfix-for-issue-22105 branch from ceadcea to cb3d385 Compare January 15, 2022 14:11
@pratimugale pratimugale marked this pull request as ready for review January 15, 2022 14:13
@jklymak
Copy link
Member
jklymak commented Jan 16, 2022

Looks like you forgot to commit an svg?

@pratimugale
Copy link
Contributor Author

Looks like you forgot to commit an svg?

Yes I realized that, but pytest generated only pdf and png for me. Is there any explicit command using which I can get the svg?

@jklymak
Copy link
Member
jklymak commented Jan 17, 2022

Yeah you need Inkscape. I think it is also possible to get the test artifacts and use that svg. However I always forget how to do that 😀

@@ -7387,3 +7387,12 @@ def test_clim():
clim = (7, 8)
norm = plot_method(clim=clim).norm
assert (norm.vmin, norm.vmax) == clim


@image_comparison(["extent_units"])
Copy link
Member

Choose a reason for hiding this comment

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

Can you mark this to have the mpl2 style (so we do not test jet + interpolation)?

Copy link
Member

Choose a reason for hiding this comment

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

and I suspect we can get away with only the pdf or svg tests here (they are not affected by the freetype version issues, we do not not really want to test font rendering here, and any issues we are going to have with units are going to happen well above the renderer level so generating the pdf/png/svg does not actually get us more meaningful test coverage.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will make the changes, thank you

@tacaswell
Copy link
Member

👍 This was simpler to implement than I expected :)

@QuLogic
Copy link
Member
QuLogic commented Jan 19, 2022

So this seems simple and makes sense, but I wonder if it's at the right level. If you now call AxesImage.set_extent later, that will not accept units.

@pratimugale pratimugale force-pushed the bugfix-for-issue-22105 branch 2 times, most recently from 2e763a3 to 615c7f6 Compare January 21, 2022 21:58
@pratimugale pratimugale force-pushed the bugfix-for-issue-22105 branch from 592bc4d to bde0f72 Compare February 8, 2022 18:51
@pratimugale pratimugale force-pushed the bugfix-for-issue-22105 branch from bde0f72 to d1b5289 Compare March 12, 2022 13:30
@mylasem
Copy link
mylasem commented Apr 9, 2022

@pratimugale are you still working on this? Anything I can do to help out?

@@ -5374,7 +5374,7 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
See the :doc:`/tutorials/intermediate/imshow_extent` tutorial for
examples and a more detailed description.

extent : floats (left, right, bottom, top), optional
extent : floats or units (left, right, bottom, top), optional
Copy link
Member

Choose a reason for hiding this comment

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

I would leave this as f!oats:

Suggested change
extent : floats or units (left, right, bottom, top), optional
extent : floats (left, right, bottom, top), optional

But add a note in the body that "these values may be unitful and match the units of the Axes'

(xmin, xmax), (ymin, ymax) = self.axes._process_unit_info(
[("x", [extent[0], extent[1]]),
("y", [extent[2], extent[3]])],
kwargs)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
kwargs)
kwargs)
if len(kwargs):
raise ValueError(f"set_extent did not consume all of the kwargs passed {list(kwargs)!r} were unused")

I _process_unit_info is very forgiving and is meant to be used in contexts where something else is going to make sure we do not have any extra kwargs. However in this case we are silently drop them on the floor (which in not great because now a typo will get silently ignored and not do what the wants....bugs where I am sure that xnuints is right but not doing what I want are among the most maddening!)

Because the keywords used depends on the type of axes we are sitting on (😱 ) I think that this is the best option?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

made the changes

Copy link
Contributor Author
@pratimugale pratimugale Oct 11, 2022

Choose a reason for hiding this comment

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

The checks have passed, could you please have a look?

Copy link
Member

Choose a reason for hiding this comment

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

I don't see any check for leftover kwargs?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had added a check for the leftover kwargs. Could you please let me know what is supposed to be done when there are kwargs remaining? For eg, if we raise a ValueError as before, then other test cases break - like when cmap is used as a kwarg

Copy link
Member

Choose a reason for hiding this comment

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

You will need to be more explicit about what gets passed to set_extent from __init__, so that you can raise in the manner noted above.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done. Does imshow even need to pass kwargs to set_extents? I have removed it. So that the user can pass those keyword arguments by calling set_extents and not in imshow. Does that sound correct?

@tacaswell tacaswell removed the status: needs workflow approval For PRs from new contributors, from which GitHub blocks workflows by default. label Apr 30, 2022
@QuLogic QuLogic added this to the v3.7.0 milestone Aug 24, 2022
@pratimugale pratimugale force-pushed the bugfix-for-issue-22105 branch 2 times, most recently from 462ad76 to 2f9ef2a Compare October 8, 2022 18:40
@pratimugale
Copy link
Contributor Author
pratimugale commented Oct 8, 2022

These changes are breaking other things and will need some rethinking. I'm working on that
(edit) just realized a small mistake and have fixed it

@pratimugale pratimugale force-pushed the bugfix-for-issue-22105 branch from 2f9ef2a to e8c5e4a Compare October 8, 2022 19:28
@pratimugale pratimugale marked this pull request as ready for review October 8, 2022 20:18
@pratimugale
Copy link
Contributor Author

@tacaswell @jklymak @QuLogic Could you please review?

(xmin, xmax), (ymin, ymax) = self.axes._process_unit_info(
[("x", [extent[0], extent[1]]),
("y", [extent[2], extent[3]])],
kwargs)
Copy link
Member

Choose a reason for hiding this comment

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

I don't see any check for leftover kwargs?

@@ -8272,3 +8272,15 @@ def test_bar_all_nan(fig_test, fig_ref):

ax_ref.bar([1], [1]).remove()
ax_ref.bar([1], [1])


@image_comparison(["extent_units.pdf"], style="mpl20")
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 probably need somewhat more extensive testing than this - at least test that the x-axes works as expected? Otherwise looks good.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi, could you please let me know what as expected would mean here?

F42D
Copy link
Member

Choose a reason for hiding this comment

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

You are only checking that dates work for the yaxis, not the xaxis

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah okay, will add the checks

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Comment on lines 8281 to 8282
dateFirst = np.datetime64('2020-01-01', 'D')
dateLast = np.datetime64('2020-01-11', 'D')
Copy link
Member

Choose a reason for hiding this comment

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

Also should be using snake_case, not camelCase.

Suggested change
dateFirst = np.datetime64('2020-01-01', 'D')
dateLast = np.datetime64('2020-01-11', 'D')
date_first = np.datetime64('2020-01-01', 'D')
date_last = np.datetime64('2020-01-11', 'D')

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@pratimugale pratimugale force-pushed the bugfix-for-issue-22105 branch 2 times, most recently from d9f4438 to db70db2 Compare November 3, 2022 19:40
@pratimugale
Copy link
Contributor Author

The changes have been made and the build is green. Requesting a review.

Copy link
Member
@jklymak jklymak left a comment

Choose a reason for hiding this comment

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

Sorry, should have noted you were adding these as PDFs. Unless there is a good reason, I would use png

You are also adding a pdf that is no longer used.

@@ -8272,3 +8272,27 @@ def test_bar_all_nan(fig_test, fig_ref):

ax_ref.bar([1], [1]).remove()
ax_ref.bar([1], [1])


@image_comparison(["extent_units_y_axis.pdf"], style="mpl20")
Copy link
Member

Choose a reason for hiding this comment

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

I would change these to png - many tests are not run if the pdf converter is not found on an architecture. While you are changing it, I would also just merge these two images on one figure as separate subplots.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

subplots sound good :)
have made the changes.

@pratimugale pratimugale force-pushed the bugfix-for-issue-22105 branch from db70db2 to c9e9b16 Compare November 4, 2022 14:55

@image_comparison(["extent_units.png"], style="mpl20")
def test_extent_units():
mpl.style.use("mpl20")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
mpl.style.use("mpl20")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done 👍

Copy link
Member
@jklymak jklymak left a comment

Choose a reason for hiding this comment

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

Looks good aside from the small suggestion...

@pratimugale pratimugale force-pushed the bugfix-for-issue-22105 branch from c9e9b16 to 90a4899 Compare November 4, 2022 16:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

[Bug]: imshow extents can't have units?
6 participants
0