Skip to content

Fix dash offset bug in Patch #23412

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
Aug 5, 2022
Merged

Conversation

AnnaMastori
Copy link
Contributor

PR Summary

Pass the given offest in the draw method of the Patch class. In this way, the Patch class no longer ignores the dash offset. (closes #22977 )

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).

@AnnaMastori AnnaMastori changed the title Add passed offset in draw method of Patch Fix dash offset bug in Patch Jul 11, 2022
@jklymak
Copy link
Member

jklymak commented Jul 11, 2022

This sounds like a breaking change. Do you have a test or a what's new image to explain why this is the right thing to do?

@anntzer
Copy link
Contributor

anntzer commented Jul 11, 2022

The whole setattr_cm is now a no-op and can go away.
See discussion in particular at #22977 (comment). I agree this qualifies more as a bugfix than as an API changes (it would only affect users who 1) explicitly set a dashoffset to a patch and 2) expected it to be ignored), but at least a whatsnew entry would be nice.
The current behavior came in in https://github.com/matplotlib/matplotlib/pull/6710/files#diff-03b74bfefbb7d4eca2804f274825899d191f8bacdbe99ea973d88925f15d1169R525 (@tacaswell).
Also, when I last looked at this (#11741), I noted that ignoring the dashoffsets was necessary to make tests pass; it would be nice to figure out why this is no longer the case.

@AnnaMastori
Copy link
Contributor Author

The whole setattr_cm is now a no-op and can go away. See discussion in particular at #22977 (comment). I agree this qualifies more as a bugfix than as an API changes (it would only affect users who 1) explicitly set a dashoffset to a patch and 2) expected it to be ignored), but at least a whatsnew entry would be nice. The current behavior came in in https://github.com/matplotlib/matplotlib/pull/6710/files#diff-03b74bfefbb7d4eca2804f274825899d191f8bacdbe99ea973d88925f15d1169R525 (@tacaswell). Also, when I last looked at this (#11741), I noted that ignoring the dashoffsets was necessary to make tests pass; it would be nice to figure out why this is no longer the case.

I am not very familiar with context managers but I guess that the removal of the setattr_cm should not be addressed within this PR. If that is not the case, I can remove it.

@oscargus
Copy link
Member

This sounds like a breaking change

One may indeed think so, but please see the argument from @timhoffm why it is maybe/probably not: #22977 (comment)

@oscargus
Copy link
Member

I think a test is required here, as suggested in #22977 (comment). One way is to make an image comparison test, see e.g.

@check_figures_equal(extensions=['png'])
def test_rotate_rect_draw(fig_test, fig_ref):

where, roughly, relevant parts of the code in #22977 (comment) is used for the reference image and then do the similar thing but with the new feature, (i.e., replacing the last linestyle with linestyle = (6, [6, 6]),, (probably the first can be linestyle = (0, [6, 6]), for both cases).

@AnnaMastori
Copy link
Contributor Author

Any feedback on the test ? @oscargus

Comment on lines 159 to 165
edgecolor = 'b'
linestyle = (0, [6, 6])
linestyle_hacked = (0, [0, 6, 6, 0])
rect_ref = Rectangle(loc, width, height, edgecolor=edgecolor,
linestyle=linestyle)
rect_ref2 = Rectangle(loc, width, height, edgecolor=edgecolor,
linestyle=linestyle_hacked)
Copy link
Member

Choose a reason for hiding this comment

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

I first thought: Why do we need two reference rectangles at all? We have two test rectangles that are carefully crafted to lie on top of each other, but have shifted dashes. Can't we have one single solid rectangle as reference?

Apparently the two dashed lines don't add up to a fully solid line because of antialiasing.

If we can't get a solid rectangle anyway, let's not even try: I propose to leave the rectangles on top of each other, but use different colors.

Suggested change
edgecolor = 'b'
linestyle = (0, [6, 6])
linestyle_hacked = (0, [0, 6, 6, 0])
rect_ref = Rectangle(loc, width, height, edgecolor=edgecolor,
linestyle=linestyle)
rect_ref2 = Rectangle(loc, width, height, edgecolor=edgecolor,
linestyle=linestyle_hacked)
rect_ref = Rectangle(loc, width, height, linewdith=3,
edgecolor='b','linestyle=(0, [6, 6]))
# fill the line gaps using a linestyle (0, [0, 6, 6, 0]), which is
# equivalent to (6, [6, 6]) but has 0 dash offset
rect_ref2 = Rectangle(loc, width, height, linewidth=3,
edgecolor='r', linestyle=(0, [0, 6, 6, 0]))

Note that I have inlined the edgecolor and linestyle variables. They are only used once and IMHO defining them explicitly leads to another indirection and additional lines, which don't help readability. I've also increased the linewidth to 3, which makes the dash pattern better visible.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the comments on code readability. It's really useful.

@tacaswell
Copy link
Member

@AnnaMastori Are you willing to follow up with Tim's comments?

@tacaswell tacaswell added this to the v3.6.0 milestone Aug 3, 2022
@AnnaMastori
Copy link
Contributor Author

@AnnaMastori Are you willing to follow up with Tim's comments?

Yes, I will get to it ASAP.

@timhoffm timhoffm merged commit d7ffff2 into matplotlib:main Aug 5, 2022
@timhoffm
Copy link
Member

timhoffm commented Aug 5, 2022

Thanks @AnnaMastori!

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.

[Bug]: offset dash linestyle has no effect in patch objects
7 participants