Skip to content

2-D array RGB and RGBA values not understood in plt.plot() #18423

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 4 commits into from
Sep 16, 2020

Conversation

danuo
Copy link
Contributor

@danuo danuo commented Sep 6, 2020

closes #18411

PR Summary

This fixes the problem discussed in the linked issue in the most simple and general way.

PR Checklist

  • [NA] Has pytest style unit tests (and pytest passes).
  • Is Flake 8 compliant (run flake8 on changed files to check).
  • [NA] New features are documented, with examples if plot related.
  • [NA] Documentation is sphinx and numpydoc compliant (the docs should build without error).
  • Conforms to Matplotlib style conventions (install flake8-docstrings and pydocstyle<4 and run flake8 --docstring-convention=all).
  • [NA] New features have an entry in doc/users/next_whats_new/ (follow instructions in README.rst there).
  • [NA] API changes documented in doc/api/next_api_changes/ (follow instructions in README.rst there).

@jklymak
Copy link
Member

jklymak commented Sep 6, 2020

A couple of suggestions:

  • say that this "closes 2-D array RGB and RGBA values not understood in plt.plot() #18411" and then it will be automatically closed...
  • have a bit more description in the PR. I appreciate you describe the problem in the Issue, but its nicer not to have to click around
  • can you add a test? Otherwise next time this all gets optimized by someone, they will forget this case, and it will need to be fixed again.

@danuo
Copy link
Contributor Author

danuo commented Sep 9, 2020

A couple of suggestions:

* say that this "closes #18411" and then it will be automatically closed...

* have a bit more description in the PR. I appreciate you describe the problem in the Issue, but its nicer not to have to click around

* can you add a test?  Otherwise next time this all gets optimized by someone, they will forget this case, and it will need to be fixed again.

I am currently trying to create a test. The issue is as follows: The error is produced only when plt.show() is executed. However, it seems that plt.show() never gets executed in any of the existing tests. How would I check for the error when not using plt.show() ?

@tacaswell
Copy link
Member

fig.canvas.draw() will force the rendering.

@tacaswell tacaswell added this to the v3.4.0 milestone Sep 9, 2020
@danuo
Copy link
Contributor Author

danuo commented Sep 9, 2020

fig.canvas.draw() will force the rendering.

Thank you, it is working great. Currently I am testing for

3*, 4*, 1D-color-array
13, 14 2D-color-array

on plot() and scatter() functions. Which other cases do you suppose should be included in the test? I was thinking about hist() for example.

@tacaswell
Copy link
Member

Using https://matplotlib.org/3.3.0/api/testing_api.html#matplotlib.testing.decorators.check_figures_equal may be very useful here so you can pass the ndim==1 and ndim==2 versions of the same color to the same function and then assert that they generate identical output.

@jklymak
Copy link
Member

jklymak commented Sep 9, 2020

@danuo overall this looks good - be sure to ping us for review if it gets buried...

@danuo
Copy link
Contributor Author

danuo commented Sep 9, 2020

I have all the tests fully working with the check_figures_equal decorator. All tests fail with current release and all pass with my commit. Would test_axes.py be the right place for these tests? One test looks like this

@check_figures_equal()
def test_2dcolor_plot(fig_test, fig_ref):
    color = np.array([0.1,0.2,0.3])
    fig_test.subplots().plot([1,2],[1,2],c=color.reshape((-1)))
    fig_ref.subplots().plot([1,2],[1,2], c=color.reshape((1,-1)))

@tacaswell
Copy link
Member

test_axes is a reasonable place. Maybe merge them all into one figure with N axes instead of N individual tests (should run marginally faster)

Probably worth also adding a test in test_colors of the conversion function.

I have a slight concern that we are going to find a different case where projecting down here is going to give the wrong answer, but apparently we don't have any test that exercises this code path at all...

@danuo danuo force-pushed the 2d_array_color_support branch 3 times, most recently from 4dbcb89 to 05f1359 Compare September 9, 2020 23:38
@danuo
Copy link
Contributor Author

danuo commented Sep 9, 2020

@tacaswell

Probably worth also adding a test in test_colors of the conversion function.

I am not sure what you mean by this. I have implemented all of your other proposals

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.

Thanks @danuo!

@jklymak
Copy link
Member

jklymak commented Sep 10, 2020

@danuo this needs two reviews, so be sure to ping if you don't get a second one. @tacaswell may or may not clarify what he meant 😉

@tacaswell
Copy link
Member

Putting a test in test_colors

assert mcolors.to_rbga([r, g, b]) == mcolors.to_rgba([[r, g, b]])

or similar to directly test the change (rather than implicitly via the plot / scatter behavior).

@danuo danuo force-pushed the 2d_array_color_support branch from 05f1359 to 33d2069 Compare September 10, 2020 16:58
@danuo
Copy link
Contributor Author

danuo commented Sep 10, 2020

Putting a test in test_colors

assert mcolors.to_rbga([r, g, b]) == mcolors.to_rgba([[r, g, b]])

or similar to directly test the change (rather than implicitly via the plot / scatter behavior).

@tacaswell Done, I have implemented such a test in test_colors. Again, the test fails with current matplotlib version and succeeds with this fix. Can you make a second review?

@danuo danuo force-pushed the 2d_array_color_support branch from d63fb4f to aaac772 Compare September 11, 2020 23:43
@QuLogic
Copy link
Member

QuLogic commented Sep 16, 2020

This will need a rebase due to the conflicts.

with 1d color arrays against one with 2d color arrays.
They should be identical if 2d arrays are read correctly.

+ Rebase with master
+ Change .reshape((-1)) to .reshape(-1)
colors.to_rgba()

+ Rebase with master
+ Change .reshape((-1)) to .reshape(-1)
@danuo danuo force-pushed the 2d_array_color_support branch from 75389ad to 369ba34 Compare September 16, 2020 13:42
@QuLogic QuLogic merged commit 8676c27 into matplotlib:master Sep 16, 2020
@QuLogic
Copy link
Member

QuLogic commented Sep 16, 2020

Thanks @danuo! Congratulations on your first PR to Matplotlib 🎉 We hope to hear from you again.

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

Successfully merging this pull request may close these issues.

2-D array RGB and RGBA values not understood in plt.plot()
4 participants