In the code below I tried to use pcolormesh to make a grid intensity plot. When thethe default option shading = 'flat' was written in the method, the code couldn't run. Either when I omitted writing this default configuration in the code or when I changed the code to shading = 'nearest', or other listed options in the documentation, the code worked smoothly. Note that the shape of x, y and z matched but the error was trying to say they didn't match.
Code for reproduction
importmatplotlob.pyplotaspltimportnumpyasnpx, y=np.meshgrid(np.arange(5), np.arange(3))
z=x+yprint(np.shape(z))
print(np.shape(x))
print(np.shape(y))
plt.pcolormesh(x, y, z, shading='flat')
Actual outcome
(3, 5)
(3, 5)
(3, 5)
TypeError Traceback (most recent call last)
Cell In [32], line 13
11 print(np.shape(x))
12 print(np.shape(y))
---> 13 plt.pcolormesh(x, y, z, shading = 'flat')
This is the expected behavior, flat shading is assuming that the colors are the centers of the mesh so we need one more x and y to be able to draw it. Unfortunately for historical reasons both "face" and "edge" data were taken by pcolormesh. The default shading will guess and pick the correct type between flat and nearest, but if you specify one of the three specific shadings we will picky about making sure the right shapes are passed in.
add a note about "N vs N+1" to the error message in the exception raised by this example
add a test that verifies that new text is in the error message via pytest.raises(...., match=...)
It is a good first issue and easy because it involves no API design, very limited code changes, and we have a number of similar tests that can be adapted.
Activity
jklymak commented on Dec 9, 2022
This is expected behaviour. Please see: https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.pcolormesh.html. You may specify shading='auto' (the default) or shading='nearest' which is what 'auto' defaults to in this case.
tacaswell commented on Dec 9, 2022
This is the expected behavior, flat shading is assuming that the colors are the centers of the mesh so we need one more x and y to be able to draw it. Unfortunately for historical reasons both "face" and "edge" data were taken by pcolormesh. The default shading will guess and pick the correct type between flat and nearest, but if you specify one of the three specific shadings we will picky about making sure the right shapes are passed in.
Please see the description of the shading parameter in the documentation you link and https://matplotlib.org/devdocs/gallery/images_contours_and_fields/pcolormesh_grids.html for more details (and diagrams!)
One thing we should improve here is to make sure the error message says what the expect shape is
jklymak commented on Dec 9, 2022
Fair enough. Thats pretty straight forward, and marked as a good first issue
tacaswell commented on Dec 9, 2022
The exact work here:
It is a good first issue and easy because it involves no API design, very limited code changes, and we have a number of similar tests that can be adapted.
casesyh commented on Dec 9, 2022
Got you. Thanks a lot for the explanations.
jklymak commented on Dec 9, 2022
Let's keep open for the documentation/error checking improvement. Thanks
casesyh commented on Dec 9, 2022
Ok. Sounds good. Thanks.
vallmeister commented on Dec 10, 2022
Is there still work to be done on this issue? If so, I'd like to give it a try.
rcomer commented on Dec 10, 2022
@vallmeister please do! The work required is outlined at #24678 (comment).
vallmeister commented on Dec 10, 2022
Alright, I'll do it!
vivekvedant commented on Jan 23, 2023
Sir, This is my first contribution please review it and let me know if any changes is required