FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[Bug]: pcolormesh doesn't allow shading = 'flat' in the option · Issue #24678 · matplotlib/matplotlib · GitHub

Repository navigation

[Bug]: pcolormesh doesn't allow shading = 'flat' in the option #24678

Closed

Description

Bug summary

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

import matplotlob.pyplot as plt
import numpy as np

x, y = np.meshgrid(np.arange(5), np.arange(3))
z = x + y
print(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')

File /opt/homebrew/lib/python3.10/site-packages/matplotlib/pyplot.py:2728, in pcolormesh(alpha, norm, cmap, vmin, vmax, shading, antialiased, data, *args, **kwargs)
2723 @_copy_docstring_and_deprecators(Axes.pcolormesh)
2724 def pcolormesh(
2725 *args, alpha=None, norm=None, cmap=None, vmin=None,
2726 vmax=None, shading=None, antialiased=False, data=None,
2727 **kwargs):
-> 2728 __ret = gca().pcolormesh(
2729 *args, alpha=alpha, norm=norm, cmap=cmap, vmin=vmin,
2730 vmax=vmax, shading=shading, antialiased=antialiased,
2731 **({"data": data} if data is not None else {}), **kwargs)
2732 sci(__ret)
2733 return __ret

File /opt/homebrew/lib/python3.10/site-packages/matplotlib/init.py:1414, in _preprocess_data..inner(ax, data, *args, **kwargs)
1411 @functools.wraps(func)
1412 def inner(ax, *args, data=None, **kwargs):
1413 if data is None:
-> 1414 return func(ax, *map(sanitize_sequence, args), **kwargs)
1416 bound = new_sig.bind(ax, *args, **kwargs)
1417 auto_label = (bound.arguments.get(label_namer)
1418 or bound.kwargs.get(label_namer))

File /opt/homebrew/lib/python3.10/site-packages/matplotlib/axes/_axes.py:6064, in Axes.pcolormesh(self, alpha, norm, cmap, vmin, vmax, shading, antialiased, *args, **kwargs)
6061 shading = shading.lower()
6062 kwargs.setdefault('edgecolors', 'none')
-> 6064 X, Y, C, shading = self._pcolorargs('pcolormesh', *args,
6065 shading=shading, kwargs=kwargs)
6066 coords = np.stack([X, Y], axis=-1)
6067 # convert to one dimensional array

File /opt/homebrew/lib/python3.10/site-packages/matplotlib/axes/_axes.py:5574, in Axes._pcolorargs(self, funcname, shading, *args, **kwargs)
5572 if shading == 'flat':
5573 if (Nx, Ny) != (ncols + 1, nrows + 1):
-> 5574 raise TypeError('Dimensions of C %s are incompatible with'
5575 ' X (%d) and/or Y (%d); see help(%s)' % (
5576 C.shape, Nx, Ny, funcname))
5577 else: # ['nearest', 'gouraud']:
5578 if (Nx, Ny) != (ncols, nrows):

TypeError: Dimensions of C (3, 5) are incompatible with X (5) and/or Y (3); see help(pcolormesh)

Expected outcome

No error

Additional information

I think the problem is in the section when shading = 'flat' in line 5574 in _axes.py as shown in the error message

Operating system

OS 12.4

Matplotlib Version

3.5.3

Matplotlib Backend

No response

Python version

No response

Jupyter version

6.4.12

Installation

pip

Activity

  1. jklymak commented on Dec 9, 2022

    Member

    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.

  2. tacaswell commented on Dec 9, 2022

    Member

    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

  3. reopened this on Dec 9, 2022
  4. jklymak commented on Dec 9, 2022

    Member

    One thing we should improve here is to make sure the error message says what the expect shape is

    Fair enough. Thats pretty straight forward, and marked as a good first issue

  5. added
    🌱 Good first issueOpen a pull request against these issues if there are no active ones!
    on Dec 9, 2022
  6. tacaswell commented on Dec 9, 2022

    Member

    The exact work here:

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

  7. added
    Difficulty: Easyhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issues
    on Dec 9, 2022
  8. added this to the future releases milestone on Dec 9, 2022
  9. casesyh commented on Dec 9, 2022

    Author

    Got you. Thanks a lot for the explanations.

  10. reopened this on Dec 9, 2022
  11. jklymak commented on Dec 9, 2022

    Member

    Let's keep open for the documentation/error checking improvement. Thanks

  12. casesyh commented on Dec 9, 2022

    Author

    Ok. Sounds good. Thanks.

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

  14. rcomer commented on Dec 10, 2022

    Member

    @vallmeister please do! The work required is outlined at #24678 (comment).

  15. vallmeister commented on Dec 10, 2022

    Alright, I'll do it!

  16. vivekvedant commented on Jan 23, 2023

    Contributor

    Sir, This is my first contribution please review it and let me know if any changes is required

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    API: argument checkingValidation of acceptable argument valuesCommunity supportUsers in need of help.Difficulty: Easyhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issuestopic: pcolor/pcolormesh🌱 Good first issueOpen a pull request against these issues if there are no active ones!

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL